Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Digest::SHA

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224, and SHA-512/256 digest functions via both functional and object-oriented interfaces.

Synopsis

use Digest::SHA qw(sha256_hex sha512_hex);
my $hex256 = sha256_hex("some data");
my $hex512 = sha512_hex("some data");
# OO interface for incremental hashing
my $ctx = Digest::SHA->new(256);
$ctx->add("first chunk");
$ctx->add("second chunk");
my $digest = $ctx->hexdigest;
# HMAC
use Digest::SHA 'hmac_sha256_hex';
my $mac = hmac_sha256_hex($data, $key);

Functions

Functional Interface – HMAC

Functional Interface – One-shot Hashing

OO Interface

add

Append data to the digest context. Can be called multiple times for incremental hashing.

$ctx->add("hello");
$ctx->add(" world");

add_bits

Add partial-byte data to the digest context (bit-level granularity).

addfile

Read a file and add its contents to the digest context.

algorithm

Return the algorithm number of this context.

b64digest

Finalize and return the base64 digest string (no padding). Resets the context.

clone

Clone the current digest context (for computing intermediate digests).

digest

Finalize and return the binary digest. Resets the context.

dump

Serialize and restore the internal state of a digest context.

Perl equivalent: Digest::SHA

getstate

Serialize and restore the internal state of a digest context.

Perl equivalent: Digest::SHA

hashsize

Return the digest size in bits for this context’s algorithm.

hexdigest

Finalize and return the hexadecimal digest string. Resets the context.

hmac_sha1

Compute an HMAC-SHA keyed hash and return the binary digest.

use Digest::SHA 'hmac_sha256';
my $mac = hmac_sha256($data, $key);

hmac_sha1_base64

Compute an HMAC-SHA keyed hash and return the base64 digest (no padding).

hmac_sha1_hex

Compute an HMAC-SHA keyed hash and return the hex digest.

hmac_sha224

Compute an HMAC-SHA keyed hash and return the binary digest.

use Digest::SHA 'hmac_sha256';
my $mac = hmac_sha256($data, $key);

hmac_sha224_base64

Compute an HMAC-SHA keyed hash and return the base64 digest (no padding).

hmac_sha224_hex

Compute an HMAC-SHA keyed hash and return the hex digest.

hmac_sha256

Compute an HMAC-SHA keyed hash and return the binary digest.

use Digest::SHA 'hmac_sha256';
my $mac = hmac_sha256($data, $key);

hmac_sha256_base64

Compute an HMAC-SHA keyed hash and return the base64 digest (no padding).

hmac_sha256_hex

Compute an HMAC-SHA keyed hash and return the hex digest.

hmac_sha384

Compute an HMAC-SHA keyed hash and return the binary digest.

use Digest::SHA 'hmac_sha256';
my $mac = hmac_sha256($data, $key);

hmac_sha384_base64

Compute an HMAC-SHA keyed hash and return the base64 digest (no padding).

hmac_sha384_hex

Compute an HMAC-SHA keyed hash and return the hex digest.

hmac_sha512

Compute an HMAC-SHA keyed hash and return the binary digest.

use Digest::SHA 'hmac_sha256';
my $mac = hmac_sha256($data, $key);

hmac_sha512_base64

Compute an HMAC-SHA keyed hash and return the base64 digest (no padding).

hmac_sha512_hex

Compute an HMAC-SHA keyed hash and return the hex digest.

load

Serialize and restore the internal state of a digest context.

Perl equivalent: Digest::SHA

new

Create a new Digest::SHA context. Accepts an algorithm number (1, 224, 256, 384, 512).

use Digest::SHA;
my $ctx = Digest::SHA->new(256);

putstate

Serialize and restore the internal state of a digest context.

Perl equivalent: Digest::SHA

reset

Reset the context for reuse with the same algorithm.

sha1

Compute a SHA digest of the given data and return the result as a binary string.

use Digest::SHA 'sha256';
my $digest = sha256("hello");

sha1_base64

Compute a SHA digest and return the result as a base64 string (without padding, matching Perl’s Digest::SHA convention).

use Digest::SHA 'sha256_base64';
my $b64 = sha256_base64("hello");

sha1_hex

Compute a SHA digest and return the result as a lowercase hexadecimal string.

use Digest::SHA 'sha256_hex';
my $hex = sha256_hex("hello");

sha224

Compute a SHA digest of the given data and return the result as a binary string.

use Digest::SHA 'sha256';
my $digest = sha256("hello");

sha224_base64

Compute a SHA digest and return the result as a base64 string (without padding, matching Perl’s Digest::SHA convention).

use Digest::SHA 'sha256_base64';
my $b64 = sha256_base64("hello");

sha224_hex

Compute a SHA digest and return the result as a lowercase hexadecimal string.

use Digest::SHA 'sha256_hex';
my $hex = sha256_hex("hello");

sha256

Compute a SHA digest of the given data and return the result as a binary string.

use Digest::SHA 'sha256';
my $digest = sha256("hello");

sha256_base64

Compute a SHA digest and return the result as a base64 string (without padding, matching Perl’s Digest::SHA convention).

use Digest::SHA 'sha256_base64';
my $b64 = sha256_base64("hello");

sha256_hex

Compute a SHA digest and return the result as a lowercase hexadecimal string.

use Digest::SHA 'sha256_hex';
my $hex = sha256_hex("hello");

sha384

Compute a SHA digest of the given data and return the result as a binary string.

use Digest::SHA 'sha256';
my $digest = sha256("hello");

sha384_base64

Compute a SHA digest and return the result as a base64 string (without padding, matching Perl’s Digest::SHA convention).

use Digest::SHA 'sha256_base64';
my $b64 = sha256_base64("hello");

sha384_hex

Compute a SHA digest and return the result as a lowercase hexadecimal string.

use Digest::SHA 'sha256_hex';
my $hex = sha256_hex("hello");

sha512

Compute a SHA digest of the given data and return the result as a binary string.

use Digest::SHA 'sha256';
my $digest = sha256("hello");

sha512224

Compute a SHA digest of the given data and return the result as a binary string.

use Digest::SHA 'sha256';
my $digest = sha256("hello");

sha512224_base64

Compute a SHA digest and return the result as a base64 string (without padding, matching Perl’s Digest::SHA convention).

use Digest::SHA 'sha256_base64';
my $b64 = sha256_base64("hello");

sha512224_hex

Compute a SHA digest and return the result as a lowercase hexadecimal string.

use Digest::SHA 'sha256_hex';
my $hex = sha256_hex("hello");

sha512256

Compute a SHA digest of the given data and return the result as a binary string.

use Digest::SHA 'sha256';
my $digest = sha256("hello");

sha512256_base64

Compute a SHA digest and return the result as a base64 string (without padding, matching Perl’s Digest::SHA convention).

use Digest::SHA 'sha256_base64';
my $b64 = sha256_base64("hello");

sha512256_hex

Compute a SHA digest and return the result as a lowercase hexadecimal string.

use Digest::SHA 'sha256_hex';
my $hex = sha256_hex("hello");

sha512_base64

Compute a SHA digest and return the result as a base64 string (without padding, matching Perl’s Digest::SHA convention).

use Digest::SHA 'sha256_base64';
my $b64 = sha256_base64("hello");

sha512_hex

Compute a SHA digest and return the result as a lowercase hexadecimal string.

use Digest::SHA 'sha256_hex';
my $hex = sha256_hex("hello");