4 use vars qw($VERSION %MMAP $AUTOLOAD);
9 "SHA-1" => ["Digest::SHA1", ["Digest::SHA", 1], ["Digest::SHA2", 1]],
10 "SHA-224" => [["Digest::SHA", 224]],
11 "SHA-256" => [["Digest::SHA", 256], ["Digest::SHA2", 256]],
12 "SHA-384" => [["Digest::SHA", 384], ["Digest::SHA2", 384]],
13 "SHA-512" => [["Digest::SHA", 512], ["Digest::SHA2", 512]],
14 "HMAC-MD5" => "Digest::HMAC_MD5",
15 "HMAC-SHA-1" => "Digest::HMAC_SHA1",
16 "CRC-16" => [["Digest::CRC", type => "crc16"]],
17 "CRC-32" => [["Digest::CRC", type => "crc32"]],
18 "CRC-CCITT" => [["Digest::CRC", type => "crcccitt"]],
23 shift; # class ignored
24 my $algorithm = shift;
25 my $impl = $MMAP{$algorithm} || do {
26 $algorithm =~ s/\W+//;
29 $impl = [$impl] unless ref($impl);
34 ($class, @args) = @$class if ref($class);
36 unless (exists ${"$class\::"}{"VERSION"}) {
37 eval "require $class";
43 return $class->new(@args, @_);
51 my $algorithm = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2);
52 $class->new($algorithm, @_);
61 Digest - Modules that calculate message digests
65 $md5 = Digest->new("MD5");
66 $sha1 = Digest->new("SHA-1");
67 $sha256 = Digest->new("SHA-256");
68 $sha384 = Digest->new("SHA-384");
69 $sha512 = Digest->new("SHA-512");
71 $hmac = Digest->HMAC_MD5($key);
75 The C<Digest::> modules calculate digests, also called "fingerprints"
76 or "hashes", of some data, called a message. The digest is (usually)
77 some small/fixed size string. The actual size of the digest depend of
78 the algorithm used. The message is simply a sequence of arbitrary
81 An important property of the digest algorithms is that the digest is
82 I<likely> to change if the message change in some way. Another
83 property is that digest functions are one-way functions, i.e. it
84 should be I<hard> to find a message that correspond to some given
85 digest. Algorithms differ in how "likely" and how "hard", as well as
86 how efficient they are to compute.
88 All C<Digest::> modules provide the same programming interface. A
89 functional interface for simple use, as well as an object oriented
90 interface that can handle messages of arbitrary length and which can
93 The digest can be delivered in three formats:
99 This is the most compact form, but it is not well suited for printing
100 or embedding in places that can't handle arbitrary data.
104 A twice as long string of lowercase hexadecimal digits.
108 A string of portable printable characters. This is the base64 encoded
109 representation of the digest with any trailing padding removed. The
110 string will be about 30% longer than the binary version.
111 L<MIME::Base64> tells you more about this encoding.
116 The functional interface is simply importable functions with the same
117 name as the algorithm. The functions take the message as argument and
118 return the digest. Example:
120 use Digest::MD5 qw(md5);
121 $digest = md5($message);
123 There are also versions of the functions with "_hex" or "_base64"
124 appended to the name, which returns the digest in the indicated form.
128 The following methods are available for all C<Digest::> modules:
132 =item $ctx = Digest->XXX($arg,...)
134 =item $ctx = Digest->new(XXX => $arg,...)
136 =item $ctx = Digest::XXX->new($arg,...)
138 The constructor returns some object that encapsulate the state of the
139 message-digest algorithm. You can add data to the object and finally
140 ask for the digest. The "XXX" should of course be replaced by the proper
141 name of the digest algorithm you want to use.
143 The two first forms are simply syntactic sugar which automatically
144 load the right module on first use. The second form allow you to use
145 algorithm names which contains letters which are not legal perl
146 identifiers, e.g. "SHA-1". If no implementation for the given algorithm
147 can be found, then an exception is raised.
149 If new() is called as an instance method (i.e. $ctx->new) it will just
150 reset the state the object to the state of a newly created object. No
151 new object is created in this case, and the return value is the
152 reference to the object (i.e. $ctx).
154 =item $other_ctx = $ctx->clone
156 The clone method creates a copy of the digest state object and returns
157 a reference to the copy.
161 This is just an alias for $ctx->new.
163 =item $ctx->add( $data, ... )
165 The $data provided as argument are appended to the message we
166 calculate the digest for. The return value is the $ctx object itself.
168 =item $ctx->addfile( $io_handle )
170 The $io_handle is read until EOF and the content is appended to the
171 message we calculate the digest for. The return value is the $ctx
174 =item $ctx->add_bits( $data, $nbits )
176 =item $ctx->add_bits( $bitstring )
178 The bits provided are appended to the message we calculate the digest
179 for. The return value is the $ctx object itself.
181 The two argument form of add_bits() will add the first $nbits bits
182 from data. For the last potentially partial byte only the high order
183 C<< $nbits % 8 >> bits are used. If $nbits is greater than C<<
184 length($data) * 8 >>, then this method would do the same as C<<
185 $ctx->add($data) >>, i.e. $nbits is silently ignored.
187 The one argument form of add_bits() takes a $bitstring of "1" and "0"
188 chars as argument. It's a shorthand for C<< $ctx->add_bits(pack("B*",
189 $bitstring), length($bitstring)) >>.
191 This example shows two calls that should have the same effect:
193 $ctx->add_bits("111100001010");
194 $ctx->add_bits("\xF0\xA0", 12);
196 Most digest algorithms are byte based. For those it is not possible
197 to add bits that are not a multiple of 8, and the add_bits() method
198 will croak if you try.
202 Return the binary digest for the message.
204 Note that the C<digest> operation is effectively a destructive,
205 read-once operation. Once it has been performed, the $ctx object is
206 automatically C<reset> and can be used to calculate another digest
207 value. Call $ctx->clone->digest if you want to calculate the digest
208 without resetting the digest state.
210 =item $ctx->hexdigest
212 Same as $ctx->digest, but will return the digest in hexadecimal form.
214 =item $ctx->b64digest
216 Same as $ctx->digest, but will return the digest as a base64 encoded
223 This table should give some indication on the relative speed of
224 different algorithms. It is sorted by throughput based on a benchmark
225 done with of some implementations of this API:
227 Algorithm Size Implementation MB/s
229 MD4 128 Digest::MD4 v1.3 165.0
230 MD5 128 Digest::MD5 v2.33 98.8
231 SHA-256 256 Digest::SHA2 v1.1.0 66.7
232 SHA-1 160 Digest::SHA v4.3.1 58.9
233 SHA-1 160 Digest::SHA1 v2.10 48.8
234 SHA-256 256 Digest::SHA v4.3.1 41.3
235 Haval-256 256 Digest::Haval256 v1.0.4 39.8
236 SHA-384 384 Digest::SHA2 v1.1.0 19.6
237 SHA-512 512 Digest::SHA2 v1.1.0 19.3
238 SHA-384 384 Digest::SHA v4.3.1 19.2
239 SHA-512 512 Digest::SHA v4.3.1 19.2
240 Whirlpool 512 Digest::Whirlpool v1.0.2 13.0
241 MD2 128 Digest::MD2 v2.03 9.5
243 Adler-32 32 Digest::Adler32 v0.03 1.3
244 CRC-16 16 Digest::CRC v0.05 1.1
245 CRC-32 32 Digest::CRC v0.05 1.1
246 MD5 128 Digest::Perl::MD5 v1.5 1.0
247 CRC-CCITT 16 Digest::CRC v0.05 0.8
249 These numbers was achieved Apr 2004 with ActivePerl-5.8.3 running
250 under Linux on a P4 2.8 GHz CPU. The last 5 entries differ by being
251 pure perl implementations of the algorithms, which explains why they
256 L<Digest::Adler32>, L<Digest::CRC>, L<Digest::Haval256>,
257 L<Digest::HMAC>, L<Digest::MD2>, L<Digest::MD4>, L<Digest::MD5>,
258 L<Digest::SHA>, L<Digest::SHA1>, L<Digest::SHA2>, L<Digest::Whirlpool>
260 New digest implementations should consider subclassing from L<Digest::base>.
266 Gisle Aas <gisle@aas.no>
268 The C<Digest::> interface is based on the interface originally
269 developed by Neil Winton for his C<MD5> module.
271 This library is free software; you can redistribute it and/or
272 modify it under the same terms as Perl itself.
274 Copyright 1998-2001,2003-2004 Gisle Aas.
275 Copyright 1995-1996 Neil Winton.