sha1#
Compute a SHA digest of the concatenated arguments in one call.
Also available as Digest::SHA::sha1_base64, Digest::SHA::sha1_hex, Digest::SHA::sha224, Digest::SHA::sha224_base64, Digest::SHA::sha224_hex, Digest::SHA::sha256, Digest::SHA::sha256_base64, Digest::SHA::sha256_hex, Digest::SHA::sha384, Digest::SHA::sha384_base64, Digest::SHA::sha384_hex, Digest::SHA::sha512, Digest::SHA::sha512224, Digest::SHA::sha512224_base64, Digest::SHA::sha512224_hex, Digest::SHA::sha512256, Digest::SHA::sha512256_base64, Digest::SHA::sha512256_hex, Digest::SHA::sha512_base64, Digest::SHA::sha512_hex.
There are 21 functional entry points: 7 algorithms crossed with 3 output encodings. The name you call selects both the algorithm and the encoding: the plain name returns the raw binary digest (a fixed-length byte string), _hex the lowercase hex digest, _base64 the unpadded Base64 digest.
Algorithm | Raw binary | Hex | Base64 |
|---|---|---|---|
SHA-1 |
|
|
|
SHA-224 |
|
|
|
SHA-256 |
|
|
|
SHA-384 |
|
|
|
SHA-512 |
|
|
|
SHA-512/224 |
|
|
|
SHA-512/256 |
|
|
|
All arguments are concatenated byte-wise before hashing, so sha256_hex("foo", "bar") is identical to sha256_hex("foobar").
Synopsis#
use Digest::SHA qw(sha256_hex sha512_base64);
my $hex = sha256_hex($data);
my $b64 = sha512_base64(@chunks);
What you get back#
A plain scalar. For the raw-binary variants it is a byte string of fixed length (20 bytes for SHA-1, 28 for SHA-224, 32 for SHA-256, 48 for SHA-384, 64 for SHA-512). For _hex variants it is twice that length. For _base64 variants it is the unpadded Base64 encoding (no trailing = characters).
Examples#
use Digest::SHA qw(sha256_hex);
print sha256_hex("abc");
# e.g. ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
use Digest::SHA qw(sha1_hex);
my $digest = sha1_hex("The quick brown fox ", "jumps over the lazy dog");
# identical to sha1_hex("The quick brown fox jumps over the lazy dog")
use Digest::SHA qw(sha512_base64);
my $tag = sha512_base64($payload);
# 86-character unpadded Base64 string
use Digest::SHA qw(sha256);
my $raw = sha256(""); # 32 raw bytes
print length $raw; # 32
Edge cases#
No arguments: hashes the empty string.
sha256_hex()returns the well-knowne3b0c442...empty-input digest.Wide characters (codepoint above
255) croak withWide character in subroutine entry- encode to UTF-8 first if you want to hash text above Latin-1.
Differences from upstream#
Fully compatible with upstream Digest::SHA 6.04.
See also#
hmac_sha256_hex- keyed variant for message authentication.Digest::SHA->new(256)- incremental interface for large or streamed input.sha256- raw-binary counterpart ofsha256_hex.