4 use vars qw($VERSION @ISA @EXPORT_OK);
6 $VERSION = '2.25'; # $Date: 2003/07/05 05:25:37 $
9 *import = \&Exporter::import;
10 @EXPORT_OK = qw(md5 md5_hex md5_base64);
16 Digest::MD5->bootstrap($VERSION);
21 # Try to load the pure perl version
22 require Digest::Perl::MD5;
24 Digest::Perl::MD5->import(qw(md5 md5_hex md5_base64));
25 push(@ISA, "Digest::Perl::MD5"); # make OO interface work
28 # restore the original error
41 Digest::MD5 - Perl interface to the MD5 Algorithm
46 use Digest::MD5 qw(md5 md5_hex md5_base64);
49 $digest = md5_hex($data);
50 $digest = md5_base64($data);
55 $ctx = Digest::MD5->new;
60 $digest = $ctx->digest;
61 $digest = $ctx->hexdigest;
62 $digest = $ctx->b64digest;
66 The C<Digest::MD5> module allows you to use the RSA Data Security
67 Inc. MD5 Message Digest algorithm from within Perl programs. The
68 algorithm takes as input a message of arbitrary length and produces as
69 output a 128-bit "fingerprint" or "message digest" of the input.
71 The C<Digest::MD5> module provide a procedural interface for simple
72 use, as well as an object oriented interface that can handle messages
73 of arbitrary length and which can read files directly.
77 The following functions are provided by the C<Digest::MD5> module.
78 None of these functions are exported by default.
84 This function will concatenate all arguments, calculate the MD5 digest
85 of this "message", and return it in binary form. The returned string
86 will be 16 bytes long.
88 The result of md5("a", "b", "c") will be exactly the same as the
91 =item md5_hex($data,...)
93 Same as md5(), but will return the digest in hexadecimal form. The
94 length of the returned string will be 32 and it will only contain
95 characters from this set: '0'..'9' and 'a'..'f'.
97 =item md5_base64($data,...)
99 Same as md5(), but will return the digest as a base64 encoded string.
100 The length of the returned string will be 22 and it will only contain
101 characters from this set: 'A'..'Z', 'a'..'z', '0'..'9', '+' and
104 Note that the base64 encoded string returned is not padded to be a
105 multiple of 4 bytes long. If you want interoperability with other
106 base64 encoded md5 digests you might want to append the redundant
107 string redundant "==" to the result.
113 The object oriented interface to C<Digest::MD5> is described in this
114 section. After a C<Digest::MD5> object has been created, you will add
115 data to it and finally ask for the digest in a suitable format. A
116 single object can be used to calculate multiple digests.
118 The following methods are provided:
122 =item $md5 = Digest::MD5->new
124 The constructor returns a new C<Digest::MD5> object which encapsulate
125 the state of the MD5 message-digest algorithm.
127 If called as an instance method (i.e. $md5->new) it will just reset the
128 state the object to the state of a newly created object. No new
129 object is created in this case.
133 This is just an alias for $md5->new.
137 This a copy of the $md5 object. It is useful when you do not want to
138 destroy the digests state, but need an intermediate value of the
139 digest, e.g. when calculating digests iteratively on a continuous data
142 my $md5 = Digest::MD5->new;
145 print "Line $.: ", $md5->clone->hexdigest, "\n";
148 =item $md5->add($data,...)
150 The $data provided as argument are appended to the message we
151 calculate the digest for. The return value is the $md5 object itself.
153 All these lines will have the same effect on the state of the $md5
156 $md5->add("a"); $md5->add("b"); $md5->add("c");
157 $md5->add("a")->add("b")->add("c");
158 $md5->add("a", "b", "c");
161 =item $md5->addfile($io_handle)
163 The $io_handle will be read until EOF and its content appended to the
164 message we calculate the digest for. The return value is the $md5
167 The addfile() method will croak() if it fails reading data for some
168 reason. If it croaks it is unpredictable what the state of the $md5
169 object will be in. The addfile() method might have been able to read
170 the file partially before it failed. It is probably wise to discard
171 or reset the $md5 object if this occurs.
173 In most cases you want to make sure that the $io_handle is in
174 C<binmode> before you pass it as argument to the addfile() method.
178 Return the binary digest for the message. The returned string will be
181 Note that the C<digest> operation is effectively a destructive,
182 read-once operation. Once it has been performed, the C<Digest::MD5>
183 object is automatically C<reset> and can be used to calculate another
184 digest value. Call $md5->clone->digest if you want to calculate the
185 digest without reseting the digest state.
187 =item $md5->hexdigest
189 Same as $md5->digest, but will return the digest in hexadecimal
190 form. The length of the returned string will be 32 and it will only
191 contain characters from this set: '0'..'9' and 'a'..'f'.
193 =item $md5->b64digest
195 Same as $md5->digest, but will return the digest as a base64 encoded
196 string. The length of the returned string will be 22 and it will only
197 contain characters from this set: 'A'..'Z', 'a'..'z', '0'..'9', '+'
201 The base64 encoded string returned is not padded to be a multiple of 4
202 bytes long. If you want interoperability with other base64 encoded
203 md5 digests you might want to append the string "==" to the result.
210 The simplest way to use this library is to import the md5_hex()
211 function (or one of its cousins):
213 use Digest::MD5 qw(md5_hex);
214 print "Digest is ", md5_hex("foobarbaz"), "\n";
216 The above example would print out the message:
218 Digest is 6df23dc03f9b54cc38a0fc1483df6e21
220 The same checksum can also be calculated in OO style:
224 $md5 = Digest::MD5->new;
225 $md5->add('foo', 'bar');
227 $digest = $md5->hexdigest;
229 print "Digest is $digest\n";
231 With OO style you can break the message arbitrary. This means that we
232 are no longer limited to have space for the whole message in memory, i.e.
233 we can handle messages of any size.
235 This is useful when calculating checksum for files:
239 my $file = shift || "/etc/passwd";
240 open(FILE, $file) or die "Can't open '$file': $!";
243 $md5 = Digest::MD5->new;
248 print $md5->b64digest, " $file\n";
250 Or we can use the builtin addfile method for more efficient reading of
255 my $file = shift || "/etc/passwd";
256 open(FILE, $file) or die "Can't open '$file': $!";
259 print Digest::MD5->new->addfile(*FILE)->hexdigest, " $file\n";
261 Perl 5.8 support Unicode characters in strings. Since the MD5
262 algorithm is only defined for strings of bytes, it can not be used on
263 strings that contains chars with ordinal number above 255. The MD5
264 functions and methods will croak if you try to feed them such input
267 use Digest::MD5 qw(md5_hex);
269 my $str = "abc\x{300}";
270 print md5_hex($str), "\n"; # croaks
271 # Wide character in subroutine entry
273 What you can do is calculate the MD5 checksum of the UTF-8
274 representation of such strings. This is achieved by filtering the
275 string through encode_utf8() function:
277 use Digest::MD5 qw(md5_hex);
278 use Encode qw(encode_utf8);
280 my $str = "abc\x{300}";
281 print md5_hex(encode_utf8($str)), "\n";
282 # 8c2d46911f3f5a326455f0ed7a8ed3b3
297 This library is free software; you can redistribute it and/or
298 modify it under the same terms as Perl itself.
300 Copyright 1998-2003 Gisle Aas.
301 Copyright 1995-1996 Neil Winton.
302 Copyright 1991-1992 RSA Data Security, Inc.
304 The MD5 algorithm is defined in RFC 1321. This implementation is
305 derived from the reference C code in RFC 1321 which is covered by
306 the following copyright statement:
312 Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
315 License to copy and use this software is granted provided that it
316 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
317 Algorithm" in all material mentioning or referencing this software
320 License is also granted to make and use derivative works provided
321 that such works are identified as "derived from the RSA Data
322 Security, Inc. MD5 Message-Digest Algorithm" in all material
323 mentioning or referencing the derived work.
325 RSA Data Security, Inc. makes no representations concerning either
326 the merchantability of this software or the suitability of this
327 software for any particular purpose. It is provided "as is"
328 without express or implied warranty of any kind.
330 These notices must be retained in any copies of any part of this
331 documentation and/or software.
335 This copyright does not prohibit distribution of any version of Perl
336 containing this extension under the terms of the GNU or Artistic
341 The original C<MD5> interface was written by Neil Winton
342 (C<N.Winton@axion.bt.co.uk>).
344 The C<Digest::MD5> module is written by Gisle Aas <gisle@ActiveState.com>.