1Digest::MD5(3pm) Perl Programmers Reference Guide Digest::MD5(3pm)
2
3
4
6 Digest::MD5 - Perl interface to the MD5 Algorithm
7
9 # Functional style
10 use Digest::MD5 qw(md5 md5_hex md5_base64);
11
12 $digest = md5($data);
13 $digest = md5_hex($data);
14 $digest = md5_base64($data);
15
16 # OO style
17 use Digest::MD5;
18
19 $ctx = Digest::MD5->new;
20
21 $ctx->add($data);
22 $ctx->addfile(*FILE);
23
24 $digest = $ctx->digest;
25 $digest = $ctx->hexdigest;
26 $digest = $ctx->b64digest;
27
29 The "Digest::MD5" module allows you to use the RSA Data Security Inc.
30 MD5 Message Digest algorithm from within Perl programs. The algorithm
31 takes as input a message of arbitrary length and produces as output a
32 128-bit "fingerprint" or "message digest" of the input.
33
34 Note that the MD5 algorithm is not as strong as it used to be. It has
35 since 2005 been easy to generate different messages that produce the
36 same MD5 digest. It still seems hard to generate messages that produce
37 a given digest, but it is probably wise to move to stronger algorithms
38 for applications that depend on the digest to uniquely identify a
39 message.
40
41 The "Digest::MD5" module provide a procedural interface for simple use,
42 as well as an object oriented interface that can handle messages of
43 arbitrary length and which can read files directly.
44
46 The following functions are provided by the "Digest::MD5" module. None
47 of these functions are exported by default.
48
49 md5($data,...)
50 This function will concatenate all arguments, calculate the MD5
51 digest of this "message", and return it in binary form. The
52 returned string will be 16 bytes long.
53
54 The result of md5("a", "b", "c") will be exactly the same as the
55 result of md5("abc").
56
57 md5_hex($data,...)
58 Same as md5(), but will return the digest in hexadecimal form. The
59 length of the returned string will be 32 and it will only contain
60 characters from this set: '0'..'9' and 'a'..'f'.
61
62 md5_base64($data,...)
63 Same as md5(), but will return the digest as a base64 encoded
64 string. The length of the returned string will be 22 and it will
65 only contain characters from this set: 'A'..'Z', 'a'..'z',
66 '0'..'9', '+' and '/'.
67
68 Note that the base64 encoded string returned is not padded to be a
69 multiple of 4 bytes long. If you want interoperability with other
70 base64 encoded md5 digests you might want to append the redundant
71 string "==" to the result.
72
74 The object oriented interface to "Digest::MD5" is described in this
75 section. After a "Digest::MD5" object has been created, you will add
76 data to it and finally ask for the digest in a suitable format. A
77 single object can be used to calculate multiple digests.
78
79 The following methods are provided:
80
81 $md5 = Digest::MD5->new
82 The constructor returns a new "Digest::MD5" object which
83 encapsulate the state of the MD5 message-digest algorithm.
84
85 If called as an instance method (i.e. $md5->new) it will just reset
86 the state the object to the state of a newly created object. No
87 new object is created in this case.
88
89 $md5->reset
90 This is just an alias for $md5->new.
91
92 $md5->clone
93 This a copy of the $md5 object. It is useful when you do not want
94 to destroy the digests state, but need an intermediate value of the
95 digest, e.g. when calculating digests iteratively on a continuous
96 data stream. Example:
97
98 my $md5 = Digest::MD5->new;
99 while (<>) {
100 $md5->add($_);
101 print "Line $.: ", $md5->clone->hexdigest, "\n";
102 }
103
104 $md5->add($data,...)
105 The $data provided as argument are appended to the message we
106 calculate the digest for. The return value is the $md5 object
107 itself.
108
109 All these lines will have the same effect on the state of the $md5
110 object:
111
112 $md5->add("a"); $md5->add("b"); $md5->add("c");
113 $md5->add("a")->add("b")->add("c");
114 $md5->add("a", "b", "c");
115 $md5->add("abc");
116
117 $md5->addfile($io_handle)
118 The $io_handle will be read until EOF and its content appended to
119 the message we calculate the digest for. The return value is the
120 $md5 object itself.
121
122 The addfile() method will croak() if it fails reading data for some
123 reason. If it croaks it is unpredictable what the state of the
124 $md5 object will be in. The addfile() method might have been able
125 to read the file partially before it failed. It is probably wise
126 to discard or reset the $md5 object if this occurs.
127
128 In most cases you want to make sure that the $io_handle is in
129 "binmode" before you pass it as argument to the addfile() method.
130
131 $md5->add_bits($data, $nbits)
132 $md5->add_bits($bitstring)
133 Since the MD5 algorithm is byte oriented you might only add bits as
134 multiples of 8, so you probably want to just use add() instead.
135 The add_bits() method is provided for compatibility with other
136 digest implementations. See Digest for description of the
137 arguments that add_bits() take.
138
139 $md5->digest
140 Return the binary digest for the message. The returned string will
141 be 16 bytes long.
142
143 Note that the "digest" operation is effectively a destructive,
144 read-once operation. Once it has been performed, the "Digest::MD5"
145 object is automatically "reset" and can be used to calculate
146 another digest value. Call $md5->clone->digest if you want to
147 calculate the digest without resetting the digest state.
148
149 $md5->hexdigest
150 Same as $md5->digest, but will return the digest in hexadecimal
151 form. The length of the returned string will be 32 and it will only
152 contain characters from this set: '0'..'9' and 'a'..'f'.
153
154 $md5->b64digest
155 Same as $md5->digest, but will return the digest as a base64
156 encoded string. The length of the returned string will be 22 and
157 it will only contain characters from this set: 'A'..'Z', 'a'..'z',
158 '0'..'9', '+' and '/'.
159
160 The base64 encoded string returned is not padded to be a multiple
161 of 4 bytes long. If you want interoperability with other base64
162 encoded md5 digests you might want to append the string "==" to the
163 result.
164
166 The simplest way to use this library is to import the md5_hex()
167 function (or one of its cousins):
168
169 use Digest::MD5 qw(md5_hex);
170 print "Digest is ", md5_hex("foobarbaz"), "\n";
171
172 The above example would print out the message:
173
174 Digest is 6df23dc03f9b54cc38a0fc1483df6e21
175
176 The same checksum can also be calculated in OO style:
177
178 use Digest::MD5;
179
180 $md5 = Digest::MD5->new;
181 $md5->add('foo', 'bar');
182 $md5->add('baz');
183 $digest = $md5->hexdigest;
184
185 print "Digest is $digest\n";
186
187 With OO style you can break the message arbitrary. This means that we
188 are no longer limited to have space for the whole message in memory,
189 i.e. we can handle messages of any size.
190
191 This is useful when calculating checksum for files:
192
193 use Digest::MD5;
194
195 my $file = shift || "/etc/passwd";
196 open(FILE, $file) or die "Can't open '$file': $!";
197 binmode(FILE);
198
199 $md5 = Digest::MD5->new;
200 while (<FILE>) {
201 $md5->add($_);
202 }
203 close(FILE);
204 print $md5->b64digest, " $file\n";
205
206 Or we can use the addfile method for more efficient reading of the
207 file:
208
209 use Digest::MD5;
210
211 my $file = shift || "/etc/passwd";
212 open(FILE, $file) or die "Can't open '$file': $!";
213 binmode(FILE);
214
215 print Digest::MD5->new->addfile(*FILE)->hexdigest, " $file\n";
216
217 Perl 5.8 support Unicode characters in strings. Since the MD5
218 algorithm is only defined for strings of bytes, it can not be used on
219 strings that contains chars with ordinal number above 255. The MD5
220 functions and methods will croak if you try to feed them such input
221 data:
222
223 use Digest::MD5 qw(md5_hex);
224
225 my $str = "abc\x{300}";
226 print md5_hex($str), "\n"; # croaks
227 # Wide character in subroutine entry
228
229 What you can do is calculate the MD5 checksum of the UTF-8
230 representation of such strings. This is achieved by filtering the
231 string through encode_utf8() function:
232
233 use Digest::MD5 qw(md5_hex);
234 use Encode qw(encode_utf8);
235
236 my $str = "abc\x{300}";
237 print md5_hex(encode_utf8($str)), "\n";
238 # 8c2d46911f3f5a326455f0ed7a8ed3b3
239
241 Digest, Digest::MD2, Digest::SHA, Digest::HMAC
242
243 md5sum(1)
244
245 RFC 1321
246
247 http://en.wikipedia.org/wiki/MD5
248
249 The paper "How to Break MD5 and Other Hash Functions" by Xiaoyun Wang
250 and Hongbo Yu.
251
253 This library is free software; you can redistribute it and/or modify it
254 under the same terms as Perl itself.
255
256 Copyright 1998-2003 Gisle Aas.
257 Copyright 1995-1996 Neil Winton.
258 Copyright 1991-1992 RSA Data Security, Inc.
259
260 The MD5 algorithm is defined in RFC 1321. This implementation is
261 derived from the reference C code in RFC 1321 which is covered by the
262 following copyright statement:
263
264 ยท Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
265 rights reserved.
266
267 License to copy and use this software is granted provided that it
268 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
269 Algorithm" in all material mentioning or referencing this software
270 or this function.
271
272 License is also granted to make and use derivative works provided
273 that such works are identified as "derived from the RSA Data
274 Security, Inc. MD5 Message-Digest Algorithm" in all material
275 mentioning or referencing the derived work.
276
277 RSA Data Security, Inc. makes no representations concerning either
278 the merchantability of this software or the suitability of this
279 software for any particular purpose. It is provided "as is" without
280 express or implied warranty of any kind.
281
282 These notices must be retained in any copies of any part of this
283 documentation and/or software.
284
285 This copyright does not prohibit distribution of any version of Perl
286 containing this extension under the terms of the GNU or Artistic
287 licenses.
288
290 The original "MD5" interface was written by Neil Winton
291 ("N.Winton@axion.bt.co.uk").
292
293 The "Digest::MD5" module is written by Gisle Aas
294 <gisle@ActiveState.com>.
295
296
297
298perl v5.10.1 2009-06-12 Digest::MD5(3pm)