NAME
Crypt::Digest::SHA256 - Hash function SHA-256 [size: 256 bits]
SYNOPSIS
### Functional interface:
use Crypt::Digest::SHA256 qw( sha256 sha256_hex sha256_b64 sha256_b64u
sha256_file sha256_file_hex sha256_file_b64 sha256_file_b64u );
# calculate digest from string/buffer
my $data = 'data string';
my $sha256_raw = sha256($data);
my $sha256_hex = sha256_hex($data);
my $sha256_b64 = sha256_b64($data);
my $sha256_b64u = sha256_b64u($data);
# or from file
my $sha256_file_raw = sha256_file('filename.dat');
my $sha256_file_hex = sha256_file_hex('filename.dat');
my $sha256_file_b64 = sha256_file_b64('filename.dat');
my $sha256_file_b64u = sha256_file_b64u('filename.dat');
# or from filehandle
my $filehandle = ...; # existing binary-mode filehandle
my $sha256_fh_raw = sha256_file($filehandle);
my $sha256_fh_hex = sha256_file_hex($filehandle);
my $sha256_fh_b64 = sha256_file_b64($filehandle);
my $sha256_fh_b64u = sha256_file_b64u($filehandle);
### OO interface:
use Crypt::Digest::SHA256;
my $d = Crypt::Digest::SHA256->new;
$d->add('any data');
my $result_raw = $d->digest; # raw bytes
my $result_hex = $d->hexdigest; # hexadecimal form
my $result_b64 = $d->b64digest; # Base64 form
my $result_b64u = $d->b64udigest; # Base64 URL-safe form
# or hash a file instead
my $file_result_raw = Crypt::Digest::SHA256->new->addfile('filename.dat')->digest;
DESCRIPTION
Provides an interface to the SHA256 digest algorithm.
EXPORT
Nothing is exported by default.
You can export selected functions:
use Crypt::Digest::SHA256 qw(sha256 sha256_hex sha256_b64 sha256_b64u
sha256_file sha256_file_hex sha256_file_b64 sha256_file_b64u);
Or all of them at once:
use Crypt::Digest::SHA256 ':all';
FUNCTIONS
sha256
Joins all arguments into a single string and returns its SHA256 digest encoded as a binary string.
Data arguments for the functional helpers are converted to byte strings using Perl's usual scalar stringification. Defined scalars, including numbers and string-overloaded objects, are accepted. undef is treated as an empty string and may emit Perl's usual "uninitialized value" warning. The same rules apply to sha256_hex, sha256_b64, and sha256_b64u.
my $sha256_raw = sha256('data string');
#or
my $sha256_raw = sha256('any data', 'more data', 'even more data');
sha256_hex
Joins all arguments into a single string and returns its SHA256 digest encoded as a hexadecimal string.
my $sha256_hex = sha256_hex('data string');
#or
my $sha256_hex = sha256_hex('any data', 'more data', 'even more data');
sha256_b64
Joins all arguments into a single string and returns its SHA256 digest encoded as a Base64 string, with trailing '=' padding.
my $sha256_b64 = sha256_b64('data string');
#or
my $sha256_b64 = sha256_b64('any data', 'more data', 'even more data');
sha256_b64u
Joins all arguments into a single string and returns its SHA256 digest encoded as a Base64 URL-safe string (see RFC 4648 section 5).
my $sha256_b64url = sha256_b64u('data string');
#or
my $sha256_b64url = sha256_b64u('any data', 'more data', 'even more data');
sha256_file
Reads a file given by a filename or filehandle and returns its SHA256 digest encoded as a binary string.
my $sha256_raw = sha256_file('filename.dat');
#or
my $filehandle = ...; # existing binary-mode filehandle
my $sha256_raw = sha256_file($filehandle);
sha256_file_hex
Reads a file given by a filename or filehandle and returns its SHA256 digest encoded as a hexadecimal string.
my $sha256_hex = sha256_file_hex('filename.dat');
#or
my $filehandle = ...; # existing binary-mode filehandle
my $sha256_hex = sha256_file_hex($filehandle);
Note: The filehandle must be in binary mode before you pass it to addfile().
sha256_file_b64
Reads a file given by a filename or filehandle and returns its SHA256 digest encoded as a Base64 string, with trailing '=' padding.
my $sha256_b64 = sha256_file_b64('filename.dat');
#or
my $filehandle = ...; # existing binary-mode filehandle
my $sha256_b64 = sha256_file_b64($filehandle);
sha256_file_b64u
Reads a file given by a filename or filehandle and returns its SHA256 digest encoded as a Base64 URL-safe string (see RFC 4648 section 5).
my $sha256_b64url = sha256_file_b64u('filename.dat');
#or
my $filehandle = ...; # existing binary-mode filehandle
my $sha256_b64url = sha256_file_b64u($filehandle);
METHODS
The OO interface provides the same set of functions as Crypt::Digest. Unless noted otherwise, assume $d is an existing digest object created via new, for example:
my $d = Crypt::Digest::SHA256->new();
new
my $d = Crypt::Digest::SHA256->new();
clone
$d->clone();
reset
$d->reset();
add
Appends data to the message. Returns the object itself (for chaining).
Each argument is converted to bytes using Perl's usual scalar stringification. Defined scalars, including numbers and string-overloaded objects, are accepted. undef is treated as an empty string and may emit Perl's usual "uninitialized value" warning.
$d->add('any data');
#or
$d->add('any data', 'more data', 'even more data');
addfile
Reads the file content and appends it to the message. Returns the object itself (for chaining).
$d->addfile('filename.dat');
#or
my $filehandle = ...; # existing binary-mode filehandle
$d->addfile($filehandle);
hashsize
$d->hashsize;
#or
Crypt::Digest::SHA256->hashsize();
#or
Crypt::Digest::SHA256::hashsize();
digest
Returns the binary digest (raw bytes). The first call finalizes the digest object. Any later add(), addfile(), digest(), hexdigest(), b64digest(), or b64udigest() call will fail until you call reset().
my $result_raw = $d->digest();
hexdigest
Returns the digest encoded as a lowercase hexadecimal string. Like digest(), the first call finalizes the digest object.
my $result_hex = $d->hexdigest();
b64digest
Returns the digest encoded as a Base64 string with trailing = padding. Like digest(), the first call finalizes the digest object.
my $result_b64 = $d->b64digest();
b64udigest
Returns the digest encoded as a Base64 URL-safe string (no trailing =). Like digest(), the first call finalizes the digest object.
my $result_b64url = $d->b64udigest();