2 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns=
"http://www.w3.org/1999/xhtml">
5 <title>Digest::MD5 - Perl interface to the MD5 Algorithm
</title>
6 <meta http-equiv=
"content-type" content=
"text/html; charset=utf-8" />
7 <link rev=
"made" href=
"mailto:" />
10 <body style=
"background-color: white">
11 <table border=
"0" width=
"100%" cellspacing=
"0" cellpadding=
"3">
12 <tr><td class=
"block" style=
"background-color: #cccccc" valign=
"middle">
13 <big><strong><span class=
"block"> Digest::MD5 - Perl interface to the MD5 Algorithm
</span></strong></big>
17 <p><a name=
"__index__"></a></p>
22 <li><a href=
"#name">NAME
</a></li>
23 <li><a href=
"#synopsis">SYNOPSIS
</a></li>
24 <li><a href=
"#description">DESCRIPTION
</a></li>
25 <li><a href=
"#functions">FUNCTIONS
</a></li>
26 <li><a href=
"#methods">METHODS
</a></li>
27 <li><a href=
"#examples">EXAMPLES
</a></li>
28 <li><a href=
"#see_also">SEE ALSO
</a></li>
29 <li><a href=
"#copyright">COPYRIGHT
</a></li>
30 <li><a href=
"#authors">AUTHORS
</a></li>
37 <h1><a name=
"name">NAME
</a></h1>
38 <p>Digest::MD5 - Perl interface to the MD5 Algorithm
</p>
42 <h1><a name=
"synopsis">SYNOPSIS
</a></h1>
45 use Digest::MD5 qw(md5 md5_hex md5_base64);
</pre>
48 $digest = md5_hex($data);
49 $digest = md5_base64($data);
</pre>
52 use Digest::MD5;
</pre>
54 $ctx = Digest::MD5-
>new;
</pre>
57 $ctx-
>addfile(*FILE);
</pre>
59 $digest = $ctx-
>digest;
60 $digest = $ctx-
>hexdigest;
61 $digest = $ctx-
>b64digest;
</pre>
65 <h1><a name=
"description">DESCRIPTION
</a></h1>
66 <p>The
<code>Digest::MD5
</code> 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.
</p>
70 <p>Note that the MD5 algorithm is not as strong as it used to be. It has
71 since
2005 been easy to generate different messages that produce the
72 same MD5 digest. It still seems hard to generate messages that
73 produce a given digest, but it is probably wise to move to stronger
74 algorithms for applications that depend on the digest to uniquely identify
76 <p>The
<code>Digest::MD5
</code> module provide a procedural interface for simple
77 use, as well as an object oriented interface that can handle messages
78 of arbitrary length and which can read files directly.
</p>
82 <h1><a name=
"functions">FUNCTIONS
</a></h1>
83 <p>The following functions are provided by the
<code>Digest::MD5
</code> module.
84 None of these functions are exported by default.
</p>
86 <dt><strong><a name=
"item_md5"><code>md5($data,...)
</code></a></strong>
89 <p>This function will concatenate all arguments, calculate the MD5 digest
90 of this ``message'', and return it in binary form. The returned string
91 will be
16 bytes long.
</p>
94 <p>The result of md5(``a'', ``b'', ``c'') will be exactly the same as the
95 result of md5(``abc'').
</p>
98 <dt><strong><a name=
"item_md5_hex"><code>md5_hex($data,...)
</code></a></strong>
101 <p>Same as md5(), but will return the digest in hexadecimal form. The
102 length of the returned string will be
32 and it will only contain
103 characters from this set: '
0'..'
9' and 'a'..'f'.
</p>
106 <dt><strong><a name=
"item_md5_base64"><code>md5_base64($data,...)
</code></a></strong>
109 <p>Same as md5(), but will return the digest as a base64 encoded string.
110 The length of the returned string will be
22 and it will only contain
111 characters from this set: 'A'..'Z', 'a'..'z', '
0'..'
9', '+' and
115 <p>Note that the base64 encoded string returned is not padded to be a
116 multiple of
4 bytes long. If you want interoperability with other
117 base64 encoded md5 digests you might want to append the redundant
118 string ``=='' to the result.
</p>
125 <h1><a name=
"methods">METHODS
</a></h1>
126 <p>The object oriented interface to
<code>Digest::MD5
</code> is described in this
127 section. After a
<code>Digest::MD5
</code> object has been created, you will add
128 data to it and finally ask for the digest in a suitable format. A
129 single object can be used to calculate multiple digests.
</p>
130 <p>The following methods are provided:
</p>
132 <dt><strong><a name=
"item_new">$md5 = Digest::MD5-
>new
</a></strong>
135 <p>The constructor returns a new
<code>Digest::MD5
</code> object which encapsulate
136 the state of the MD5 message-digest algorithm.
</p>
139 <p>If called as an instance method (i.e. $md5-
>new) it will just reset the
140 state the object to the state of a newly created object. No new
141 object is created in this case.
</p>
144 <dt><strong><a name=
"item_reset">$md5-
>reset
</a></strong>
147 <p>This is just an alias for $md5-
>new.
</p>
150 <dt><strong><a name=
"item_clone">$md5-
>clone
</a></strong>
153 <p>This a copy of the $md5 object. It is useful when you do not want to
154 destroy the digests state, but need an intermediate value of the
155 digest, e.g. when calculating digests iteratively on a continuous data
160 my $md5 = Digest::MD5-
>new;
163 print
"Line $.:
", $md5-
>clone-
>hexdigest,
"\n
";
167 <dt><strong><a name=
"item_add">$md5-
><code>add($data,...)
</code></a></strong>
170 <p>The $data provided as argument are appended to the message we
171 calculate the digest for. The return value is the $md5 object itself.
</p>
174 <p>All these lines will have the same effect on the state of the $md5
179 $md5-
>add(
"a
"); $md5-
>add(
"b
"); $md5-
>add(
"c
");
180 $md5-
>add(
"a
")-
>add(
"b
")-
>add(
"c
");
181 $md5-
>add(
"a
",
"b
",
"c
");
182 $md5-
>add(
"abc
");
</pre>
185 <dt><strong><a name=
"item_addfile">$md5-
><code>addfile($io_handle)
</code></a></strong>
188 <p>The $io_handle will be read until EOF and its content appended to the
189 message we calculate the digest for. The return value is the $md5
193 <p>The
<a href=
"#item_addfile"><code>addfile()
</code></a> method will
<code>croak()
</code> if it fails reading data for some
194 reason. If it croaks it is unpredictable what the state of the $md5
195 object will be in. The
<a href=
"#item_addfile"><code>addfile()
</code></a> method might have been able to read
196 the file partially before it failed. It is probably wise to discard
197 or reset the $md5 object if this occurs.
</p>
200 <p>In most cases you want to make sure that the $io_handle is in
201 <a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_binmode"><code>binmode
</code></a> before you pass it as argument to the
<a href=
"#item_addfile"><code>addfile()
</code></a> method.
</p>
204 <dt><strong><a name=
"item_add_bits">$md5-
>add_bits($data, $nbits)
</a></strong>
206 <dt><strong>$md5-
><code>add_bits($bitstring)
</code></strong>
209 <p>Since the MD5 algorithm is byte oriented you might only add bits as
210 multiples of
8, so you probably want to just use
<a href=
"#item_add"><code>add()
</code></a> instead. The
211 <a href=
"#item_add_bits"><code>add_bits()
</code></a> method is provided for compatibility with other digest
212 implementations. See
<a href=
"file://C|\msysgit\mingw\html/lib/Digest.html">the Digest manpage
</a> for description of the arguments
213 that
<a href=
"#item_add_bits"><code>add_bits()
</code></a> take.
</p>
216 <dt><strong><a name=
"item_digest">$md5-
>digest
</a></strong>
219 <p>Return the binary digest for the message. The returned string will be
223 <p>Note that the
<a href=
"#item_digest"><code>digest
</code></a> operation is effectively a destructive,
224 read-once operation. Once it has been performed, the
<code>Digest::MD5
</code>
225 object is automatically
<a href=
"#item_reset"><code>reset
</code></a> and can be used to calculate another
226 digest value. Call $md5-
>clone-
>digest if you want to calculate the
227 digest without resetting the digest state.
</p>
230 <dt><strong><a name=
"item_hexdigest">$md5-
>hexdigest
</a></strong>
233 <p>Same as $md5-
>digest, but will return the digest in hexadecimal
234 form. The length of the returned string will be
32 and it will only
235 contain characters from this set: '
0'..'
9' and 'a'..'f'.
</p>
238 <dt><strong><a name=
"item_b64digest">$md5-
>b64digest
</a></strong>
241 <p>Same as $md5-
>digest, but will return the digest as a base64 encoded
242 string. The length of the returned string will be
22 and it will only
243 contain characters from this set: 'A'..'Z', 'a'..'z', '
0'..'
9', '+'
247 <p>The base64 encoded string returned is not padded to be a multiple of
4
248 bytes long. If you want interoperability with other base64 encoded
249 md5 digests you might want to append the string ``=='' to the result.
</p>
256 <h1><a name=
"examples">EXAMPLES
</a></h1>
257 <p>The simplest way to use this library is to import the
<a href=
"#item_md5_hex"><code>md5_hex()
</code></a>
258 function (or one of its cousins):
</p>
260 use Digest::MD5 qw(md5_hex);
261 print
"Digest is
", md5_hex(
"foobarbaz
"),
"\n
";
</pre>
262 <p>The above example would print out the message:
</p>
264 Digest is
6df23dc03f9b54cc38a0fc1483df6e21
</pre>
265 <p>The same checksum can also be calculated in OO style:
</p>
269 $md5 = Digest::MD5-
>new;
270 $md5-
>add('foo', 'bar');
272 $digest = $md5-
>hexdigest;
274 print
"Digest is $digest\n
";
</pre>
275 <p>With OO style you can break the message arbitrary. This means that we
276 are no longer limited to have space for the whole message in memory, i.e.
277 we can handle messages of any size.
</p>
278 <p>This is useful when calculating checksum for files:
</p>
280 use Digest::MD5;
</pre>
282 my $file = shift ||
"/etc/passwd
";
283 open(FILE, $file) or die
"Can't open '$file': $!
";
286 $md5 = Digest::MD5-
>new;
287 while (
<FILE
>) {
291 print $md5-
>b64digest,
" $file\n
";
</pre>
292 <p>Or we can use the addfile method for more efficient reading of
295 use Digest::MD5;
</pre>
297 my $file = shift ||
"/etc/passwd
";
298 open(FILE, $file) or die
"Can't open '$file': $!
";
301 print Digest::MD5-
>new-
>addfile(*FILE)-
>hexdigest,
" $file\n
";
</pre>
302 <p>Perl
5.8 support Unicode characters in strings. Since the MD5
303 algorithm is only defined for strings of bytes, it can not be used on
304 strings that contains chars with ordinal number above
255. The MD5
305 functions and methods will croak if you try to feed them such input
308 use Digest::MD5 qw(md5_hex);
</pre>
310 my $str =
"abc\x{
300}
";
311 print md5_hex($str),
"\n
"; # croaks
312 # Wide character in subroutine entry
</pre>
313 <p>What you can do is calculate the MD5 checksum of the UTF-
8
314 representation of such strings. This is achieved by filtering the
315 string through
<code>encode_utf8()
</code> function:
</p>
317 use Digest::MD5 qw(md5_hex);
318 use Encode qw(encode_utf8);
</pre>
320 my $str =
"abc\x{
300}
";
321 print md5_hex(encode_utf8($str)),
"\n
";
322 #
8c2d46911f3f5a326455f0ed7a8ed3b3
</pre>
326 <h1><a name=
"see_also">SEE ALSO
</a></h1>
327 <p><a href=
"file://C|\msysgit\mingw\html/lib/Digest.html">the Digest manpage
</a>,
328 <a href=
"file://C|\msysgit\mingw\html/Digest/MD2.html">the Digest::MD2 manpage
</a>,
329 <a href=
"file://C|\msysgit\mingw\html/Digest/SHA1.html">the Digest::SHA1 manpage
</a>,
330 <a href=
"file://C|\msysgit\mingw\html/Digest/HMAC.html">the Digest::HMAC manpage
</a></p>
331 <p><em>md5sum(
1)
</em></p>
333 <p><a href=
"http://en.wikipedia.org/wiki/MD5">http://en.wikipedia.org/wiki/MD5
</a></p>
334 <p>The paper ``How to Break MD5 and Other Hash Functions'' by Xiaoyun Wang
339 <h1><a name=
"copyright">COPYRIGHT
</a></h1>
340 <p>This library is free software; you can redistribute it and/or
341 modify it under the same terms as Perl itself.
</p>
343 Copyright
1998-
2003 Gisle Aas.
344 Copyright
1995-
1996 Neil Winton.
345 Copyright
1991-
1992 RSA Data Security, Inc.
</pre>
346 <p>The MD5 algorithm is defined in RFC
1321. This implementation is
347 derived from the reference C code in RFC
1321 which is covered by
348 the following copyright statement:
</p>
352 <p>Copyright (C)
1991-
2, RSA Data Security, Inc. Created
1991. All
356 <p>License to copy and use this software is granted provided that it
357 is identified as the ``RSA Data Security, Inc. MD5 Message-Digest
358 Algorithm'' in all material mentioning or referencing this software
359 or this function.
</p>
362 <p>License is also granted to make and use derivative works provided
363 that such works are identified as ``derived from the RSA Data
364 Security, Inc. MD5 Message-Digest Algorithm'' in all material
365 mentioning or referencing the derived work.
</p>
368 <p>RSA Data Security, Inc. makes no representations concerning either
369 the merchantability of this software or the suitability of this
370 software for any particular purpose. It is provided ``as is''
371 without express or implied warranty of any kind.
</p>
374 <p>These notices must be retained in any copies of any part of this
375 documentation and/or software.
</p>
379 <p>This copyright does not prohibit distribution of any version of Perl
380 containing this extension under the terms of the GNU or Artistic
385 <h1><a name=
"authors">AUTHORS
</a></h1>
386 <p>The original
<code>MD5
</code> interface was written by Neil Winton
387 (
<code>N.Winton@axion.bt.co.uk
</code>).
</p>
388 <p>The
<code>Digest::MD5
</code> module is written by Gisle Aas
<<a href=
"mailto:gisle@ActiveState.com">gisle@ActiveState.com
</a>>.
</p>
389 <table border=
"0" width=
"100%" cellspacing=
"0" cellpadding=
"3">
390 <tr><td class=
"block" style=
"background-color: #cccccc" valign=
"middle">
391 <big><strong><span class=
"block"> Digest::MD5 - Perl interface to the MD5 Algorithm
</span></strong></big>