Install Perl 5.8.8
[msysgit.git] / mingw / html / lib / Digest / MD5.html
blobc2e586a3efca5b5b6cdecd4a62402270908569bf
1 <?xml version="1.0" ?>
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">
4 <head>
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:" />
8 </head>
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">&nbsp;Digest::MD5 - Perl interface to the MD5 Algorithm</span></strong></big>
14 </td></tr>
15 </table>
17 <p><a name="__index__"></a></p>
18 <!-- INDEX BEGIN -->
20 <ul>
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>
31 </ul>
32 <!-- INDEX END -->
34 <hr />
35 <p>
36 </p>
37 <h1><a name="name">NAME</a></h1>
38 <p>Digest::MD5 - Perl interface to the MD5 Algorithm</p>
39 <p>
40 </p>
41 <hr />
42 <h1><a name="synopsis">SYNOPSIS</a></h1>
43 <pre>
44 # Functional style
45 use Digest::MD5 qw(md5 md5_hex md5_base64);</pre>
46 <pre>
47 $digest = md5($data);
48 $digest = md5_hex($data);
49 $digest = md5_base64($data);</pre>
50 <pre>
51 # OO style
52 use Digest::MD5;</pre>
53 <pre>
54 $ctx = Digest::MD5-&gt;new;</pre>
55 <pre>
56 $ctx-&gt;add($data);
57 $ctx-&gt;addfile(*FILE);</pre>
58 <pre>
59 $digest = $ctx-&gt;digest;
60 $digest = $ctx-&gt;hexdigest;
61 $digest = $ctx-&gt;b64digest;</pre>
62 <p>
63 </p>
64 <hr />
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
75 a message.</p>
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>
79 <p>
80 </p>
81 <hr />
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>
85 <dl>
86 <dt><strong><a name="item_md5"><code>md5($data,...)</code></a></strong>
88 <dd>
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>
92 </dd>
93 <dd>
94 <p>The result of md5(``a'', ``b'', ``c'') will be exactly the same as the
95 result of md5(``abc'').</p>
96 </dd>
97 </li>
98 <dt><strong><a name="item_md5_hex"><code>md5_hex($data,...)</code></a></strong>
100 <dd>
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>
104 </dd>
105 </li>
106 <dt><strong><a name="item_md5_base64"><code>md5_base64($data,...)</code></a></strong>
108 <dd>
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
112 '/'.</p>
113 </dd>
114 <dd>
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>
119 </dd>
120 </li>
121 </dl>
123 </p>
124 <hr />
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>
131 <dl>
132 <dt><strong><a name="item_new">$md5 = Digest::MD5-&gt;new</a></strong>
134 <dd>
135 <p>The constructor returns a new <code>Digest::MD5</code> object which encapsulate
136 the state of the MD5 message-digest algorithm.</p>
137 </dd>
138 <dd>
139 <p>If called as an instance method (i.e. $md5-&gt;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>
142 </dd>
143 </li>
144 <dt><strong><a name="item_reset">$md5-&gt;reset</a></strong>
146 <dd>
147 <p>This is just an alias for $md5-&gt;new.</p>
148 </dd>
149 </li>
150 <dt><strong><a name="item_clone">$md5-&gt;clone</a></strong>
152 <dd>
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
156 stream. Example:</p>
157 </dd>
158 <dd>
159 <pre>
160 my $md5 = Digest::MD5-&gt;new;
161 while (&lt;&gt;) {
162 $md5-&gt;add($_);
163 print &quot;Line $.: &quot;, $md5-&gt;clone-&gt;hexdigest, &quot;\n&quot;;
164 }</pre>
165 </dd>
166 </li>
167 <dt><strong><a name="item_add">$md5-&gt;<code>add($data,...)</code></a></strong>
169 <dd>
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>
172 </dd>
173 <dd>
174 <p>All these lines will have the same effect on the state of the $md5
175 object:</p>
176 </dd>
177 <dd>
178 <pre>
179 $md5-&gt;add(&quot;a&quot;); $md5-&gt;add(&quot;b&quot;); $md5-&gt;add(&quot;c&quot;);
180 $md5-&gt;add(&quot;a&quot;)-&gt;add(&quot;b&quot;)-&gt;add(&quot;c&quot;);
181 $md5-&gt;add(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;);
182 $md5-&gt;add(&quot;abc&quot;);</pre>
183 </dd>
184 </li>
185 <dt><strong><a name="item_addfile">$md5-&gt;<code>addfile($io_handle)</code></a></strong>
187 <dd>
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
190 object itself.</p>
191 </dd>
192 <dd>
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>
198 </dd>
199 <dd>
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>
202 </dd>
203 </li>
204 <dt><strong><a name="item_add_bits">$md5-&gt;add_bits($data, $nbits)</a></strong>
206 <dt><strong>$md5-&gt;<code>add_bits($bitstring)</code></strong>
208 <dd>
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>
214 </dd>
215 </li>
216 <dt><strong><a name="item_digest">$md5-&gt;digest</a></strong>
218 <dd>
219 <p>Return the binary digest for the message. The returned string will be
220 16 bytes long.</p>
221 </dd>
222 <dd>
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-&gt;clone-&gt;digest if you want to calculate the
227 digest without resetting the digest state.</p>
228 </dd>
229 </li>
230 <dt><strong><a name="item_hexdigest">$md5-&gt;hexdigest</a></strong>
232 <dd>
233 <p>Same as $md5-&gt;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>
236 </dd>
237 </li>
238 <dt><strong><a name="item_b64digest">$md5-&gt;b64digest</a></strong>
240 <dd>
241 <p>Same as $md5-&gt;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', '+'
244 and '/'.</p>
245 </dd>
246 <dd>
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>
250 </dd>
251 </li>
252 </dl>
254 </p>
255 <hr />
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>
259 <pre>
260 use Digest::MD5 qw(md5_hex);
261 print &quot;Digest is &quot;, md5_hex(&quot;foobarbaz&quot;), &quot;\n&quot;;</pre>
262 <p>The above example would print out the message:</p>
263 <pre>
264 Digest is 6df23dc03f9b54cc38a0fc1483df6e21</pre>
265 <p>The same checksum can also be calculated in OO style:</p>
266 <pre>
267 use Digest::MD5;
269 $md5 = Digest::MD5-&gt;new;
270 $md5-&gt;add('foo', 'bar');
271 $md5-&gt;add('baz');
272 $digest = $md5-&gt;hexdigest;
274 print &quot;Digest is $digest\n&quot;;</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>
279 <pre>
280 use Digest::MD5;</pre>
281 <pre>
282 my $file = shift || &quot;/etc/passwd&quot;;
283 open(FILE, $file) or die &quot;Can't open '$file': $!&quot;;
284 binmode(FILE);</pre>
285 <pre>
286 $md5 = Digest::MD5-&gt;new;
287 while (&lt;FILE&gt;) {
288 $md5-&gt;add($_);
290 close(FILE);
291 print $md5-&gt;b64digest, &quot; $file\n&quot;;</pre>
292 <p>Or we can use the addfile method for more efficient reading of
293 the file:</p>
294 <pre>
295 use Digest::MD5;</pre>
296 <pre>
297 my $file = shift || &quot;/etc/passwd&quot;;
298 open(FILE, $file) or die &quot;Can't open '$file': $!&quot;;
299 binmode(FILE);</pre>
300 <pre>
301 print Digest::MD5-&gt;new-&gt;addfile(*FILE)-&gt;hexdigest, &quot; $file\n&quot;;</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
306 data:</p>
307 <pre>
308 use Digest::MD5 qw(md5_hex);</pre>
309 <pre>
310 my $str = &quot;abc\x{300}&quot;;
311 print md5_hex($str), &quot;\n&quot;; # 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>
316 <pre>
317 use Digest::MD5 qw(md5_hex);
318 use Encode qw(encode_utf8);</pre>
319 <pre>
320 my $str = &quot;abc\x{300}&quot;;
321 print md5_hex(encode_utf8($str)), &quot;\n&quot;;
322 # 8c2d46911f3f5a326455f0ed7a8ed3b3</pre>
324 </p>
325 <hr />
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>
332 <p>RFC 1321</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
335 and Hongbo Yu.</p>
337 </p>
338 <hr />
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>
342 <pre>
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>
349 <dl>
350 <dt>
351 <dd>
352 <p>Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
353 rights reserved.</p>
354 </dd>
355 <dd>
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>
360 </dd>
361 <dd>
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>
366 </dd>
367 <dd>
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>
372 </dd>
373 <dd>
374 <p>These notices must be retained in any copies of any part of this
375 documentation and/or software.</p>
376 </dd>
377 </li>
378 </dl>
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
381 licenses.</p>
383 </p>
384 <hr />
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 &lt;<a href="mailto:gisle@ActiveState.com">gisle@ActiveState.com</a>&gt;.</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">&nbsp;Digest::MD5 - Perl interface to the MD5 Algorithm</span></strong></big>
392 </td></tr>
393 </table>
395 </body>
397 </html>