Digest::SHA1#

📦 std

Compute SHA-1 message digests through the interface of Digest::SHA1, the CPAN module older code uses where newer code uses Digest::SHA.

The functional interface (sha1, sha1_hex, sha1_base64) hashes its arguments in one call. The object interface (Digest::SHA1->new, add, addfile, digest) takes the data in pieces. Every digest comes as raw bytes (20), lowercase hex (40) or unpadded Base64 (27). sha1_transform is the bare SHA-1 compression of one block, for the FIPS 186 random-number construction.

The digests are computed by the engine of the built-in Digest::SHA, which uses the processor’s SHA instructions where it has them, so the two modules always agree.

This is a native implementation of Digest::SHA1 2.13’s SHA1.xs; its Digest/SHA1.pm is used unchanged.

Synopsis#

use Digest::SHA1 qw(sha1_hex sha1_base64);

print sha1_hex('abc'), "\n";      # a9993e364706816aba3e25717850c26c9cd0d89d
print sha1_base64('abc'), "\n";   # qZk+NkcGgWq6PiVxeFDCbJzQ2J0

my $ctx = Digest::SHA1->new;
$ctx->add('a', 'b');
$ctx->add('c');
print $ctx->hexdigest, "\n";      # the same as sha1_hex('abc')

Differences from upstream#

Two, each where perl 5.44 with SHA1.xs fails (t/81-xs-native/Digest-SHA1/150-or-better.t):

  • sha1_transform given more than 64 bytes uses the first 64. perl5 copies the whole string into a 64-byte buffer and aborts with “buffer overflow detected”.

  • A method called on the class name instead of an object (Digest::SHA1->add($data)) dies “Not a reference to a Digest::SHA1 object”. perl5 crashes.

Functions#

Functional digests#

sha1#

Compute the SHA-1 digest of the concatenated arguments in one call.

Also available as Digest::SHA1::sha1_base64, Digest::SHA1::sha1_hex.

sha1_transform#

The SHA-1 compression function over one 64-byte block, from the standard initial state: the 20-byte result, with no padding and no length.

OO lifecycle#

new#

Create a Digest::SHA1 object, or reset an existing one.

clone#

Copy a Digest::SHA1 object, the data added so far included, into a new object of the same class.

Synopsis

my $so_far = $ctx->clone->hexdigest;   # $ctx keeps going

OO input#

add#

Append data to the message being hashed.

addfile#

Append everything that can be read from a filehandle to the message.

OO output#

b64digest#

Finish the message and return its digest, then reset the object to the empty message.

Also available as Digest::SHA1::digest, Digest::SHA1::hexdigest.