1*0Sstevel@tonic-gate#!perl -w 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateuse Test qw(plan ok); 4*0Sstevel@tonic-gateplan tests => 12; 5*0Sstevel@tonic-gate 6*0Sstevel@tonic-gate{ 7*0Sstevel@tonic-gate package LenDigest; 8*0Sstevel@tonic-gate require Digest::base; 9*0Sstevel@tonic-gate use vars qw(@ISA); 10*0Sstevel@tonic-gate @ISA = qw(Digest::base); 11*0Sstevel@tonic-gate 12*0Sstevel@tonic-gate sub new { 13*0Sstevel@tonic-gate my $class = shift; 14*0Sstevel@tonic-gate my $str = ""; 15*0Sstevel@tonic-gate bless \$str, $class; 16*0Sstevel@tonic-gate } 17*0Sstevel@tonic-gate 18*0Sstevel@tonic-gate sub add { 19*0Sstevel@tonic-gate my $self = shift; 20*0Sstevel@tonic-gate $$self .= join("", @_); 21*0Sstevel@tonic-gate return $self; 22*0Sstevel@tonic-gate } 23*0Sstevel@tonic-gate 24*0Sstevel@tonic-gate sub digest { 25*0Sstevel@tonic-gate my $self = shift; 26*0Sstevel@tonic-gate my $len = length($$self); 27*0Sstevel@tonic-gate my $first = ($len > 0) ? substr($$self, 0, 1) : "X"; 28*0Sstevel@tonic-gate $$self = ""; 29*0Sstevel@tonic-gate return sprintf "$first%04d", $len; 30*0Sstevel@tonic-gate } 31*0Sstevel@tonic-gate} 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gatemy $ctx = LenDigest->new; 34*0Sstevel@tonic-gateok($ctx->digest, "X0000"); 35*0Sstevel@tonic-gateok($ctx->hexdigest, "5830303030"); 36*0Sstevel@tonic-gateok($ctx->b64digest, "WDAwMDA"); 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gate$ctx->add("foo"); 39*0Sstevel@tonic-gateok($ctx->digest, "f0003"); 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate$ctx->add("foo"); 42*0Sstevel@tonic-gateok($ctx->hexdigest, "6630303033"); 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate$ctx->add("foo"); 45*0Sstevel@tonic-gateok($ctx->b64digest, "ZjAwMDM"); 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gateopen(F, ">xxtest$$") || die; 48*0Sstevel@tonic-gatebinmode(F); 49*0Sstevel@tonic-gateprint F "abc" x 100, "\n"; 50*0Sstevel@tonic-gateclose(F) || die; 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gateopen(F, "xxtest$$") || die; 53*0Sstevel@tonic-gate$ctx->addfile(*F); 54*0Sstevel@tonic-gateclose(F); 55*0Sstevel@tonic-gateunlink("xxtest$$") || warn; 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gateok($ctx->digest, "a0301"); 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gateeval { 60*0Sstevel@tonic-gate $ctx->add_bits("1010"); 61*0Sstevel@tonic-gate}; 62*0Sstevel@tonic-gateok($@ =~ /^Number of bits must be multiple of 8/); 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate$ctx->add_bits("01010101"); 65*0Sstevel@tonic-gateok($ctx->digest, "U0001"); 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gateeval { 68*0Sstevel@tonic-gate $ctx->add_bits("abc", 12); 69*0Sstevel@tonic-gate}; 70*0Sstevel@tonic-gateok($@ =~ /^Number of bits must be multiple of 8/); 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate$ctx->add_bits("abc", 16); 73*0Sstevel@tonic-gateok($ctx->digest, "a0002"); 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate$ctx->add_bits("abc", 32); 76*0Sstevel@tonic-gateok($ctx->digest, "a0003"); 77