1b39c5158Smillertuse strict; 2b39c5158Smillertuse FileHandle; 3*9f11ffb7Safresh1use Digest::SHA; 4b39c5158Smillert 5b39c5158Smillertmy @out = ( 6b39c5158Smillert "ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0", 7b39c5158Smillert "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", 8b39c5158Smillert); 9b39c5158Smillert 10*9f11ffb7Safresh1my $numtests = 6 + scalar @out; 11b39c5158Smillertprint "1..$numtests\n"; 12b39c5158Smillert 13b39c5158Smillert # attempt to use an invalid algorithm, and check for failure 14b39c5158Smillert 15b39c5158Smillertmy $testnum = 1; 16b39c5158Smillertmy $NSA = "SHA-42"; # No Such Algorithm 17*9f11ffb7Safresh1print "not " if Digest::SHA->new($NSA); 18b39c5158Smillertprint "ok ", $testnum++, "\n"; 19b39c5158Smillert 20b39c5158Smillertmy $tempfile = "methods.tmp"; 21*9f11ffb7Safresh1END { unlink $tempfile if $tempfile } 22b39c5158Smillert 23b39c5158Smillert # test OO methods using first two SHA-256 vectors from NIST 24b39c5158Smillert 25b39c5158Smillertmy $fh = FileHandle->new($tempfile, "w"); 26b39c5158Smillertbinmode($fh); 27b39c5158Smillertprint $fh "bcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; 28b39c5158Smillert$fh->close; 29b39c5158Smillert 30*9f11ffb7Safresh1my $sha = Digest::SHA->new()->reset("SHA-256")->new(); 31b39c5158Smillert$sha->add_bits("a", 5)->add_bits("001"); 32b39c5158Smillert 33b39c5158Smillertmy $rsp = shift(@out); 34b39c5158Smillertprint "not " unless $sha->clone->add("b", "c")->b64digest eq $rsp; 35b39c5158Smillertprint "ok ", $testnum++, "\n"; 36b39c5158Smillert 37b39c5158Smillert$rsp = shift(@out); 38b39c5158Smillert 39b39c5158Smillert # test addfile with bareword filehandle 40b39c5158Smillert 41b39c5158Smillertopen(FILE, "<$tempfile"); 42b39c5158Smillertbinmode(FILE); 43b39c5158Smillertprint "not " unless 44b39c5158Smillert $sha->clone->addfile(*FILE)->hexdigest eq $rsp; 45b39c5158Smillertprint "ok ", $testnum++, "\n"; 46b39c5158Smillertclose(FILE); 47b39c5158Smillert 48b39c5158Smillert # test addfile with indirect filehandle 49b39c5158Smillert 50b39c5158Smillert$fh = FileHandle->new($tempfile, "r"); 51b39c5158Smillertbinmode($fh); 52b39c5158Smillertprint "not " unless $sha->clone->addfile($fh)->hexdigest eq $rsp; 53b39c5158Smillertprint "ok ", $testnum++, "\n"; 54b39c5158Smillert$fh->close; 55b39c5158Smillert 56b39c5158Smillert # test addfile using file name instead of handle 57b39c5158Smillert 58b39c5158Smillertprint "not " unless $sha->addfile($tempfile, "b")->hexdigest eq $rsp; 59b39c5158Smillertprint "ok ", $testnum++, "\n"; 60b39c5158Smillert 61b8851fccSafresh1 # test addfile "universal newlines" mode 62b8851fccSafresh1 63b8851fccSafresh1$fh = FileHandle->new($tempfile, "w"); 64b8851fccSafresh1binmode($fh); 65b8851fccSafresh1print $fh "MacOS\r" . "MSDOS\r\n" . "UNIX\n" . "Quirky\r\r\n"; 66b8851fccSafresh1$fh->close; 67b8851fccSafresh1 68b8851fccSafresh1my $d = $sha->new(1)->addfile($tempfile, "U")->hexdigest; 69b8851fccSafresh1if ($d eq "f4c6855783c737c7e224873c90e80a9df5c2bc97") { 70b8851fccSafresh1 print "ok ", $testnum++, "\n"; 71b8851fccSafresh1} 72b8851fccSafresh1elsif ($d eq "42335d4a517a5e31399e948e9d842bafd9194d8f") { 73b8851fccSafresh1 print "ok ", $testnum++, " # skip: flaky -T\n"; 74b8851fccSafresh1} 75b8851fccSafresh1else { 76b8851fccSafresh1 print "not ok ", $testnum++, "\n"; 77b8851fccSafresh1} 78b8851fccSafresh1 79898184e3Ssthen # test addfile BITS mode 80898184e3Ssthen 81898184e3Ssthen$fh = FileHandle->new($tempfile, "w"); 82898184e3Ssthenprint $fh "0100010"; # using NIST 7-bit test vector 83898184e3Ssthen$fh->close; 84898184e3Ssthen 85898184e3Ssthenprint "not " unless $sha->new(1)->addfile($tempfile, "0")->hexdigest eq 86898184e3Ssthen "04f31807151181ad0db278a1660526b0aeef64c2"; 87898184e3Ssthenprint "ok ", $testnum++, "\n"; 88898184e3Ssthen 89898184e3Ssthen$fh = FileHandle->new($tempfile, "w"); 90898184e3Ssthenbinmode($fh); 91898184e3Ssthenprint $fh map(chr, (0..127)); # this is actually NIST 2-bit test 92898184e3Ssthen$fh->close; # vector "01" (other chars ignored) 93898184e3Ssthen 94898184e3Ssthenprint "not " unless $sha->new(1)->addfile($tempfile, "0")->hexdigest eq 95898184e3Ssthen "ec6b39952e1a3ec3ab3507185cf756181c84bbe2"; 96898184e3Ssthenprint "ok ", $testnum++, "\n"; 97