1b39c5158Smillert#!perl -w 2b39c5158Smillert 3*eac174f2Safresh1use strict; 4*eac174f2Safresh1use warnings; 5*eac174f2Safresh1 6*eac174f2Safresh1use Test::More tests => 13; 7*eac174f2Safresh1 8*eac174f2Safresh1use File::Temp 'tempfile'; 9b39c5158Smillert 10b39c5158Smillert{ 11*eac174f2Safresh1 12b39c5158Smillert package LenDigest; 13b39c5158Smillert require Digest::base; 14*eac174f2Safresh1 our @ISA = qw(Digest::base); 15b39c5158Smillert 16b39c5158Smillert sub new { 17b39c5158Smillert my $class = shift; 18b39c5158Smillert my $str = ""; 19b39c5158Smillert bless \$str, $class; 20b39c5158Smillert } 21b39c5158Smillert 22b39c5158Smillert sub add { 23b39c5158Smillert my $self = shift; 24b39c5158Smillert $$self .= join( "", @_ ); 25b39c5158Smillert return $self; 26b39c5158Smillert } 27b39c5158Smillert 28b39c5158Smillert sub digest { 29b39c5158Smillert my $self = shift; 30b39c5158Smillert my $len = length($$self); 31b39c5158Smillert my $first = ( $len > 0 ) ? substr( $$self, 0, 1 ) : "X"; 32b39c5158Smillert $$self = ""; 33b39c5158Smillert return sprintf "$first%04d", $len; 34b39c5158Smillert } 35b39c5158Smillert} 36b39c5158Smillert 37b39c5158Smillertmy $ctx = LenDigest->new; 387469d825Srpointelis( $ctx->digest, "X0000" ); 39b39c5158Smillert 40b39c5158Smillertmy $EBCDIC = ord('A') == 193; 41b39c5158Smillert 42b39c5158Smillertif ($EBCDIC) { 437469d825Srpointel is( $ctx->hexdigest, "e7f0f0f0f0" ); 447469d825Srpointel is( $ctx->b64digest, "5/Dw8PA" ); 45*eac174f2Safresh1 is( $ctx->base64_padded_digest, "5/Dw8PA=" ); 46*eac174f2Safresh1} 47*eac174f2Safresh1else { 487469d825Srpointel is( $ctx->hexdigest, "5830303030" ); 497469d825Srpointel is( $ctx->b64digest, "WDAwMDA" ); 50*eac174f2Safresh1 is( $ctx->base64_padded_digest, "WDAwMDA=" ); 51b39c5158Smillert} 52b39c5158Smillert 53b39c5158Smillert$ctx->add("foo"); 547469d825Srpointelis( $ctx->digest, "f0003" ); 55b39c5158Smillert 56b39c5158Smillert$ctx->add("foo"); 577469d825Srpointelis( $ctx->hexdigest, $EBCDIC ? "86f0f0f0f3" : "6630303033" ); 58b39c5158Smillert 59b39c5158Smillert$ctx->add("foo"); 607469d825Srpointelis( $ctx->b64digest, $EBCDIC ? "hvDw8PM" : "ZjAwMDM" ); 61b39c5158Smillert 62*eac174f2Safresh1{ 63*eac174f2Safresh1 my ( $fh, $tempfile ) = tempfile( UNLINK => 1 ); 64*eac174f2Safresh1 binmode($fh); 65*eac174f2Safresh1 print $fh "abc" x 100, "\n"; 66*eac174f2Safresh1 close($fh) || die; 67b39c5158Smillert 68*eac174f2Safresh1 open( my $fh2, $tempfile ) || die; 69*eac174f2Safresh1 $ctx->addfile($fh2); 70*eac174f2Safresh1 close($fh2); 71b39c5158Smillert 727469d825Srpointel is( $ctx->digest, "a0301" ); 73*eac174f2Safresh1} 74b39c5158Smillert 75*eac174f2Safresh1eval { $ctx->add_bits("1010"); }; 767469d825Srpointellike( $@, '/^Number of bits must be multiple of 8/' ); 77b39c5158Smillert 78b39c5158Smillert$ctx->add_bits( $EBCDIC ? "11100100" : "01010101" ); 797469d825Srpointelis( $ctx->digest, "U0001" ); 80b39c5158Smillert 81*eac174f2Safresh1eval { $ctx->add_bits( "abc", 12 ); }; 827469d825Srpointellike( $@, '/^Number of bits must be multiple of 8/' ); 83b39c5158Smillert 84b39c5158Smillert$ctx->add_bits( "abc", 16 ); 857469d825Srpointelis( $ctx->digest, "a0002" ); 86b39c5158Smillert 87b39c5158Smillert$ctx->add_bits( "abc", 32 ); 887469d825Srpointelis( $ctx->digest, "a0003" ); 89