1 # Test against SHA-1 Sample Vectors from NIST
3 # ref: http://csrc.nist.gov/cryptval/shs.html
5 # Uses files "nist/bit-messages.sha1" and "nist/bit-hashes.sha1"
11 use File::Basename qw(dirname);
16 if ($ENV{PERL_CORE}) {
25 my $file = File::Spec->catfile(dirname($0), "nist", "bit-hashes.sha1");
26 open(my $fh, q{<}, $file);
28 next unless (/^[0-9A-F]/);
36 plan tests => scalar(@hashes);
41 my $str = pack("B*", shift);
42 my $ctx = Digest::SHA->new(1);
43 for (my $j = 0; $j <= 99; $j++) {
44 for (my $i = 1; $i <= 50000; $i++) {
45 $str .= chr(0x00) x (int($j/4)+3);
46 $str .= pack("N", $i);
47 $str = $ctx->add($str)->digest;
49 ok(uc(unpack("H*", $str)), $hash = shift(@hashes));
60 my $ctx = Digest::SHA->new(1);
62 my $file = File::Spec->catfile(dirname($0), "nist", "bit-messages.sha1");
63 open(my $fh, q{<}, $file);
65 $type3 = 1 if (/Type 3/);
66 $type3 = 0 if (/^<D/);
67 next unless (/^[0-9^ ]/);
72 @cnts = split(/\s+/, $line);
75 for (my $i = 0; $i < $cnts[0]; $i++) {
76 $bitstr .= $bitval x $cnts[$i+2];
77 $bitval = $bitval eq "1" ? "0" : "1";
80 uc($ctx->add_bits($bitstr)->hexdigest),
81 $hash = shift(@hashes)
83 doType3($bitstr) if ($type3);