1*0Sstevel@tonic-gatepackage Digest; 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateuse strict; 4*0Sstevel@tonic-gateuse vars qw($VERSION %MMAP $AUTOLOAD); 5*0Sstevel@tonic-gate 6*0Sstevel@tonic-gate$VERSION = "1.06"; 7*0Sstevel@tonic-gate 8*0Sstevel@tonic-gate%MMAP = ( 9*0Sstevel@tonic-gate "SHA-1" => ["Digest::SHA1", ["Digest::SHA", 1], ["Digest::SHA2", 1]], 10*0Sstevel@tonic-gate "SHA-256" => [["Digest::SHA", 256], ["Digest::SHA2", 256]], 11*0Sstevel@tonic-gate "SHA-384" => [["Digest::SHA", 384], ["Digest::SHA2", 384]], 12*0Sstevel@tonic-gate "SHA-512" => [["Digest::SHA", 512], ["Digest::SHA2", 512]], 13*0Sstevel@tonic-gate "HMAC-MD5" => "Digest::HMAC_MD5", 14*0Sstevel@tonic-gate "HMAC-SHA-1" => "Digest::HMAC_SHA1", 15*0Sstevel@tonic-gate); 16*0Sstevel@tonic-gate 17*0Sstevel@tonic-gatesub new 18*0Sstevel@tonic-gate{ 19*0Sstevel@tonic-gate shift; # class ignored 20*0Sstevel@tonic-gate my $algorithm = shift; 21*0Sstevel@tonic-gate my $impl = $MMAP{$algorithm} || do { 22*0Sstevel@tonic-gate $algorithm =~ s/\W+//; 23*0Sstevel@tonic-gate "Digest::$algorithm"; 24*0Sstevel@tonic-gate }; 25*0Sstevel@tonic-gate $impl = [$impl] unless ref($impl); 26*0Sstevel@tonic-gate my $err; 27*0Sstevel@tonic-gate for (@$impl) { 28*0Sstevel@tonic-gate my $class = $_; 29*0Sstevel@tonic-gate my @args; 30*0Sstevel@tonic-gate ($class, @args) = @$class if ref($class); 31*0Sstevel@tonic-gate no strict 'refs'; 32*0Sstevel@tonic-gate unless (exists ${"$class\::"}{"VERSION"}) { 33*0Sstevel@tonic-gate eval "require $class"; 34*0Sstevel@tonic-gate if ($@) { 35*0Sstevel@tonic-gate $err ||= $@; 36*0Sstevel@tonic-gate next; 37*0Sstevel@tonic-gate } 38*0Sstevel@tonic-gate } 39*0Sstevel@tonic-gate return $class->new(@args, @_); 40*0Sstevel@tonic-gate } 41*0Sstevel@tonic-gate die $err; 42*0Sstevel@tonic-gate} 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gatesub AUTOLOAD 45*0Sstevel@tonic-gate{ 46*0Sstevel@tonic-gate my $class = shift; 47*0Sstevel@tonic-gate my $algorithm = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2); 48*0Sstevel@tonic-gate $class->new($algorithm, @_); 49*0Sstevel@tonic-gate} 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate1; 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate__END__ 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate=head1 NAME 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gateDigest - Modules that calculate message digests 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate=head1 SYNOPSIS 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate $md5 = Digest->new("MD5"); 62*0Sstevel@tonic-gate $sha1 = Digest->new("SHA-1"); 63*0Sstevel@tonic-gate $sha256 = Digest->new("SHA-256"); 64*0Sstevel@tonic-gate $sha384 = Digest->new("SHA-384"); 65*0Sstevel@tonic-gate $sha512 = Digest->new("SHA-512"); 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate $hmac = Digest->HMAC_MD5($key); 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate=head1 DESCRIPTION 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gateThe C<Digest::> modules calculate digests, also called "fingerprints" 72*0Sstevel@tonic-gateor "hashes", of some data, called a message. The digest is (usually) 73*0Sstevel@tonic-gatesome small/fixed size string. The actual size of the digest depend of 74*0Sstevel@tonic-gatethe algorithm used. The message is simply a sequence of arbitrary 75*0Sstevel@tonic-gatebytes or bits. 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gateAn important property of the digest algorithms is that the digest is 78*0Sstevel@tonic-gateI<likely> to change if the message change in some way. Another 79*0Sstevel@tonic-gateproperty is that digest functions are one-way functions, i.e. it 80*0Sstevel@tonic-gateshould be I<hard> to find a message that correspond to some given 81*0Sstevel@tonic-gatedigest. Algorithms differ in how "likely" and how "hard", as well as 82*0Sstevel@tonic-gatehow efficient they are to compute. 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gateAll C<Digest::> modules provide the same programming interface. A 85*0Sstevel@tonic-gatefunctional interface for simple use, as well as an object oriented 86*0Sstevel@tonic-gateinterface that can handle messages of arbitrary length and which can 87*0Sstevel@tonic-gateread files directly. 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gateThe digest can be delivered in three formats: 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate=over 8 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate=item I<binary> 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gateThis is the most compact form, but it is not well suited for printing 96*0Sstevel@tonic-gateor embedding in places that can't handle arbitrary data. 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate=item I<hex> 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gateA twice as long string of lowercase hexadecimal digits. 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate=item I<base64> 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gateA string of portable printable characters. This is the base64 encoded 105*0Sstevel@tonic-gaterepresentation of the digest with any trailing padding removed. The 106*0Sstevel@tonic-gatestring will be about 30% longer than the binary version. 107*0Sstevel@tonic-gateL<MIME::Base64> tells you more about this encoding. 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate=back 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gateThe functional interface is simply importable functions with the same 113*0Sstevel@tonic-gatename as the algorithm. The functions take the message as argument and 114*0Sstevel@tonic-gatereturn the digest. Example: 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate use Digest::MD5 qw(md5); 117*0Sstevel@tonic-gate $digest = md5($message); 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gateThere are also versions of the functions with "_hex" or "_base64" 120*0Sstevel@tonic-gateappended to the name, which returns the digest in the indicated form. 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate=head1 OO INTERFACE 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gateThe following methods are available for all C<Digest::> modules: 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate=over 4 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gate=item $ctx = Digest->XXX($arg,...) 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate=item $ctx = Digest->new(XXX => $arg,...) 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate=item $ctx = Digest::XXX->new($arg,...) 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gateThe constructor returns some object that encapsulate the state of the 135*0Sstevel@tonic-gatemessage-digest algorithm. You can add data to the object and finally 136*0Sstevel@tonic-gateask for the digest. The "XXX" should of course be replaced by the proper 137*0Sstevel@tonic-gatename of the digest algorithm you want to use. 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gateThe two first forms are simply syntactic sugar which automatically 140*0Sstevel@tonic-gateload the right module on first use. The second form allow you to use 141*0Sstevel@tonic-gatealgorithm names which contains letters which are not legal perl 142*0Sstevel@tonic-gateidentifiers, e.g. "SHA-1". If no implementation for the given algorithm 143*0Sstevel@tonic-gatecan be found, then an exception is raised. 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gateIf new() is called as an instance method (i.e. $ctx->new) it will just 146*0Sstevel@tonic-gatereset the state the object to the state of a newly created object. No 147*0Sstevel@tonic-gatenew object is created in this case, and the return value is the 148*0Sstevel@tonic-gatereference to the object (i.e. $ctx). 149*0Sstevel@tonic-gate 150*0Sstevel@tonic-gate=item $other_ctx = $ctx->clone 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gateThe clone method creates a copy of the digest state object and returns 153*0Sstevel@tonic-gatea reference to the copy. 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate=item $ctx->reset 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gateThis is just an alias for $ctx->new. 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gate=item $ctx->add( $data, ... ) 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gateThe $data provided as argument are appended to the message we 162*0Sstevel@tonic-gatecalculate the digest for. The return value is the $ctx object itself. 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate=item $ctx->addfile( $io_handle ) 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gateThe $io_handle is read until EOF and the content is appended to the 167*0Sstevel@tonic-gatemessage we calculate the digest for. The return value is the $ctx 168*0Sstevel@tonic-gateobject itself. 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate=item $ctx->add_bits( $data, $nbits ) 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gate=item $ctx->add_bits( $bitstring ) 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gateThe bits provided are appended to the message we calculate the digest 175*0Sstevel@tonic-gatefor. The return value is the $ctx object itself. 176*0Sstevel@tonic-gate 177*0Sstevel@tonic-gateThe two argument form of add_bits() will add the first $nbits bits 178*0Sstevel@tonic-gatefrom data. For the last potentially partial byte only the high order 179*0Sstevel@tonic-gateC<< $nbits % 8 >> bits are used. If $nbits is greater than C<< 180*0Sstevel@tonic-gatelength($data) * 8 >>, then this method would do the same as C<< 181*0Sstevel@tonic-gate$ctx->add($data) >>, i.e. $nbits is silently ignored. 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gateThe one argument form of add_bits() takes a $bitstring of "1" and "0" 184*0Sstevel@tonic-gatechars as argument. It's a shorthand for C<< $ctx->add_bits(pack("B*", 185*0Sstevel@tonic-gate$bitstring), length($bitstring)) >>. 186*0Sstevel@tonic-gate 187*0Sstevel@tonic-gateThis example shows two calls that should have the same effect: 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate $ctx->add_bits("111100001010"); 190*0Sstevel@tonic-gate $ctx->add_bits("\xF0\xA0", 12); 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gateMost digest algorithms are byte based. For those it is not possible 193*0Sstevel@tonic-gateto add bits that are not a multiple of 8, and the add_bits() method 194*0Sstevel@tonic-gatewill croak if you try. 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gate=item $ctx->digest 197*0Sstevel@tonic-gate 198*0Sstevel@tonic-gateReturn the binary digest for the message. 199*0Sstevel@tonic-gate 200*0Sstevel@tonic-gateNote that the C<digest> operation is effectively a destructive, 201*0Sstevel@tonic-gateread-once operation. Once it has been performed, the $ctx object is 202*0Sstevel@tonic-gateautomatically C<reset> and can be used to calculate another digest 203*0Sstevel@tonic-gatevalue. Call $ctx->clone->digest if you want to calculate the digest 204*0Sstevel@tonic-gatewithout reseting the digest state. 205*0Sstevel@tonic-gate 206*0Sstevel@tonic-gate=item $ctx->hexdigest 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gateSame as $ctx->digest, but will return the digest in hexadecimal form. 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gate=item $ctx->b64digest 211*0Sstevel@tonic-gate 212*0Sstevel@tonic-gateSame as $ctx->digest, but will return the digest as a base64 encoded 213*0Sstevel@tonic-gatestring. 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gate=back 216*0Sstevel@tonic-gate 217*0Sstevel@tonic-gate=head1 Digest speed 218*0Sstevel@tonic-gate 219*0Sstevel@tonic-gateThis table should give some indication on the relative speed of 220*0Sstevel@tonic-gatedifferent algorithms. It is sorted by throughput based on a benchmark 221*0Sstevel@tonic-gatedone with of some implementations of this API: 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gate Algorithm Size Implementation MB/s 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gate MD4 128 Digest::MD4 v1.1 24.9 226*0Sstevel@tonic-gate MD5 128 Digest::MD5 v2.30 18.7 227*0Sstevel@tonic-gate Haval-256 256 Digest::Haval256 v1.0.4 17.0 228*0Sstevel@tonic-gate SHA-1 160 Digest::SHA1 v2.06 15.3 229*0Sstevel@tonic-gate SHA-1 160 Digest::SHA v4.0.0 10.1 230*0Sstevel@tonic-gate SHA-256 256 Digest::SHA2 v1.0.0 7.6 231*0Sstevel@tonic-gate SHA-256 256 Digest::SHA v4.0.0 6.5 232*0Sstevel@tonic-gate SHA-384 384 Digest::SHA2 v1.0.0 2.7 233*0Sstevel@tonic-gate SHA-384 384 Digest::SHA v4.0.0 2.7 234*0Sstevel@tonic-gate SHA-512 512 Digest::SHA2 v1.0.0 2.7 235*0Sstevel@tonic-gate SHA-512 512 Digest::SHA v4.0.0 2.7 236*0Sstevel@tonic-gate Whirlpool 512 Digest::Whirlpool v1.0.2 1.4 237*0Sstevel@tonic-gate MD2 128 Digest::MD2 v2.03 1.1 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate Adler-32 32 Digest::Adler32 v0.03 0.2 240*0Sstevel@tonic-gate MD5 128 Digest::Perl::MD5 v1.5 0.1 241*0Sstevel@tonic-gate 242*0Sstevel@tonic-gateThese numbers was achieved Nov 2003 with ActivePerl-5.8.1 running 243*0Sstevel@tonic-gateunder Linux on a P-II 350 MHz CPU. The last 2 entries differ by being 244*0Sstevel@tonic-gatepure perl implementations of the algorithms, which explains why they 245*0Sstevel@tonic-gateare so slow. 246*0Sstevel@tonic-gate 247*0Sstevel@tonic-gate=head1 SEE ALSO 248*0Sstevel@tonic-gate 249*0Sstevel@tonic-gateL<Digest::Adler32>, L<Digest::Haval256>, L<Digest::HMAC>, L<Digest::MD2>, L<Digest::MD4>, L<Digest::MD5>, L<Digest::SHA>, L<Digest::SHA1>, L<Digest::SHA2>, L<Digest::Whirlpool> 250*0Sstevel@tonic-gate 251*0Sstevel@tonic-gateNew digest implementations should consider subclassing from L<Digest::base>. 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gateL<MIME::Base64> 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gate=head1 AUTHOR 256*0Sstevel@tonic-gate 257*0Sstevel@tonic-gateGisle Aas <gisle@aas.no> 258*0Sstevel@tonic-gate 259*0Sstevel@tonic-gateThe C<Digest::> interface is based on the interface originally 260*0Sstevel@tonic-gatedeveloped by Neil Winton for his C<MD5> module. 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gateThis library is free software; you can redistribute it and/or 263*0Sstevel@tonic-gatemodify it under the same terms as Perl itself. 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate Copyright 1998-2001,2003-2004 Gisle Aas. 266*0Sstevel@tonic-gate Copyright 1995-1996 Neil Winton. 267*0Sstevel@tonic-gate 268*0Sstevel@tonic-gate=cut 269