Digest::SHA2 - A variable-length one-way hash function |
Digest::SHA2 - A variable-length one-way hash function (deprecated in favor of the Digest::SHA manpage)
This module has numerious known bugs, is not compatable with the Digest interface and its functionality is a subset of the functionality of the Digest::SHA manpage (which is in perl core as of 5.9.3).
Please use the Digest::SHA manpage instead of this module in new and old code.
Digest::SHA2 - A Perl interface for SHA-256, SHA-384 and SHA-512, collectively known as SHA-2
SHA-2 is the collective name of one-way hash functions developed by the NIST. SHA-256, SHA-384, and SHA-512 pertain to hashes whose outputs are 256 bits, 384 bits and 512 bits, respectively.
This Perl implementation is meant to be a replacement for the older SHA256 by Rafael R. Sevilla. His module has a bug in the SHA-256 implementation.
This new implementation uses the C source of Aaron Gifford.
use Digest::SHA2;
$sha2obj = new Digest::SHA2 [$hashlength]; $sha2obj->add(LIST); $sha2obj->addfile(*HANDLE); $sha2obj->reset();
$sha_clone = $sha2obj->clone(); $digest = $sha2obj->digest(); $digest = $sha2obj->hexdigest(); $digest = $sha2obj->b64digest(); $digest = $sha2obj->base64digest(); # deprecated
$digest = $sha2obj->hashsize(); $digest = $sha2obj->rounds();
SHA-2 supports the following functions:
For example, to specify SHA-512, use
$sha2obj = new Digest::SHA2 512;
To specify SHA-256, use
$sha2obj = new Digest::SHA2 256;
or just simply
$sha2obj = new Digest::SHA2;
Haval can uses 3, 4, or 5 rounds, with 5 different output lengths: 128, 160, 192, 224, and 256 bits, thereby, having 15 variants. Tiger, on the other hand, outputs 192 bits of digest, using 3 rounds; but for added security, can also use 4 rounds.
For example,
$sha2obj->add($string1); $sha2obj->add($string2, $string3, $string4);
For example,
my $clone = $sha2obj->clone(); # clone SHA-2 object
my $sig = $sha2obj->clone->hexdigest; print "partial digest: $sig\n";
#!/usr/local/bin/perl
use diagnostics; use strict; use warnings; use Digest::SHA2;
my $string1 = "This is a string."; my $string2 = "This is another string."; my $string3 = "This is a string.This is another string.";
my $sha2obj = new Digest::SHA2 512; print "hash size = ", $sha2obj->hashsize, " bits\n";
$sha2obj->add($string1); my $digest = $sha2obj->hexdigest(); print "Hash string1 only\n"; print "$digest\n\n";
$sha2obj->reset(); $sha2obj->add($string1, $string2); my $digest2 = $sha2obj->hexdigest(); print "Hash string1 and then hash string2\n"; print "$digest2\n\n";
$sha2obj->reset(); $sha2obj->add($string1); $sha2obj->add($string2); my $digest3 = $sha2obj->hexdigest(); print "Hash string1 and then hash string2\n"; print "$digest3\n\n";
$sha2obj->reset(); $sha2obj->add($string3); print "Hash the two concatenated strings\n"; my $digest4 = $sha2obj->hexdigest(); print "$digest4\n";
#!/usr/local/bin/perl
use diagnostics; use strict; use warnings; use MIME::Base64; use Digest::SHA2;
my $file = "strings"; open INFILE, $file or die "$file not found";
my $sha2obj = new Digest::SHA2; # defaults to 256-bit output $sha2obj->addfile(*INFILE); my $hex_output = $sha2obj->hexdigest(); my $base64_output = $sha2obj->b64digest(); close INFILE; print "$file\n"; print "$hex_output\n"; print "$base64_output\n";
See the ``examples'' and ``t'' directories for more examples.
Please consult the file, COMPARISON, for comparison of various one-way hash functions with digest lengths of 256, 384, and 512 bits.
I used the C source of Aaron Gifford as backend for this Perl implementation.
Copyright (C) 2003 Julius C. Duque. Please read contact.html that comes with this distribution for details on how to contact the author.
This library is free software; you can redistribute it and/or modify it under the same terms as the GNU General Public License.
Digest::SHA2 - A variable-length one-way hash function |