sha_functional#

Compute a SHA digest of the concatenated arguments in one call.

This is the dispatcher behind every functional hash entry point — all 21 shapes (7 algorithms × 3 encodings) route through it. The Perl-level name selects both the algorithm and the output encoding:

  • sha1, sha224, sha256, sha384, sha512, sha512224, sha512256 return the raw binary digest (a fixed-length byte string).

  • sha1_hex, sha224_hex, … return the lowercase hex digest.

  • sha1_base64, sha224_base64, … return the unpadded Base64 digest.

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-known e3b0c442… empty-input digest.

  • Wide characters (codepoint above 255) croak with Wide 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 of sha256_hex.