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 mes‐
39 sage.
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 sin‐
77 gle 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 encapsu‐
83 late 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 cal‐
106 culate the digest for. The return value is the $md5 object itself.
107
108 All these lines will have the same effect on the state of the $md5
109 object:
110
111 $md5->add("a"); $md5->add("b"); $md5->add("c");
112 $md5->add("a")->add("b")->add("c");
113 $md5->add("a", "b", "c");
114 $md5->add("abc");
115
116 $md5->addfile($io_handle)
117 The $io_handle will be read until EOF and its content appended to
118 the message we calculate the digest for. The return value is the
119 $md5 object itself.
120
121 The addfile() method will croak() if it fails reading data for some
122 reason. If it croaks it is unpredictable what the state of the
123 $md5 object will be in. The addfile() method might have been able
124 to read the file partially before it failed. It is probably wise
125 to discard or reset the $md5 object if this occurs.
126
127 In most cases you want to make sure that the $io_handle is in "bin‐
128 mode" before you pass it as argument to the addfile() method.
129
130 $md5->add_bits($data, $nbits)
131 $md5->add_bits($bitstring)
132 Since the MD5 algorithm is byte oriented you might only add bits as
133 multiples of 8, so you probably want to just use add() instead.
134 The add_bits() method is provided for compatibility with other
135 digest implementations. See Digest for description of the argu‐
136 ments that add_bits() take.
137
138 $md5->digest
139 Return the binary digest for the message. The returned string will
140 be 16 bytes long.
141
142 Note that the "digest" operation is effectively a destructive,
143 read-once operation. Once it has been performed, the "Digest::MD5"
144 object is automatically "reset" and can be used to calculate
145 another digest value. Call $md5->clone->digest if you want to cal‐
146 culate the digest without resetting the digest state.
147
148 $md5->hexdigest
149 Same as $md5->digest, but will return the digest in hexadecimal
150 form. The length of the returned string will be 32 and it will only
151 contain characters from this set: '0'..'9' and 'a'..'f'.
152
153 $md5->b64digest
154 Same as $md5->digest, but will return the digest as a base64
155 encoded string. The length of the returned string will be 22 and
156 it will only contain characters from this set: 'A'..'Z', 'a'..'z',
157 '0'..'9', '+' and '/'.
158
159 The base64 encoded string returned is not padded to be a multiple
160 of 4 bytes long. If you want interoperability with other base64
161 encoded md5 digests you might want to append the string "==" to the
162 result.
163
165 The simplest way to use this library is to import the md5_hex() func‐
166 tion (or one of its cousins):
167
168 use Digest::MD5 qw(md5_hex);
169 print "Digest is ", md5_hex("foobarbaz"), "\n";
170
171 The above example would print out the message:
172
173 Digest is 6df23dc03f9b54cc38a0fc1483df6e21
174
175 The same checksum can also be calculated in OO style:
176
177 use Digest::MD5;
178
179 $md5 = Digest::MD5->new;
180 $md5->add('foo', 'bar');
181 $md5->add('baz');
182 $digest = $md5->hexdigest;
183
184 print "Digest is $digest\n";
185
186 With OO style you can break the message arbitrary. This means that we
187 are no longer limited to have space for the whole message in memory,
188 i.e. we can handle messages of any size.
189
190 This is useful when calculating checksum for files:
191
192 use Digest::MD5;
193
194 my $file = shift ⎪⎪ "/etc/passwd";
195 open(FILE, $file) or die "Can't open '$file': $!";
196 binmode(FILE);
197
198 $md5 = Digest::MD5->new;
199 while (<FILE>) {
200 $md5->add($_);
201 }
202 close(FILE);
203 print $md5->b64digest, " $file\n";
204
205 Or we can use the addfile method for more efficient reading of the
206 file:
207
208 use Digest::MD5;
209
210 my $file = shift ⎪⎪ "/etc/passwd";
211 open(FILE, $file) or die "Can't open '$file': $!";
212 binmode(FILE);
213
214 print Digest::MD5->new->addfile(*FILE)->hexdigest, " $file\n";
215
216 Perl 5.8 support Unicode characters in strings. Since the MD5 algo‐
217 rithm is only defined for strings of bytes, it can not be used on
218 strings that contains chars with ordinal number above 255. The MD5
219 functions and methods will croak if you try to feed them such input
220 data:
221
222 use Digest::MD5 qw(md5_hex);
223
224 my $str = "abc\x{300}";
225 print md5_hex($str), "\n"; # croaks
226 # Wide character in subroutine entry
227
228 What you can do is calculate the MD5 checksum of the UTF-8 representa‐
229 tion of such strings. This is achieved by filtering the string through
230 encode_utf8() function:
231
232 use Digest::MD5 qw(md5_hex);
233 use Encode qw(encode_utf8);
234
235 my $str = "abc\x{300}";
236 print md5_hex(encode_utf8($str)), "\n";
237 # 8c2d46911f3f5a326455f0ed7a8ed3b3
238
240 Digest, Digest::MD2, Digest::SHA1, Digest::HMAC
241
242 md5sum(1)
243
244 RFC 1321
245
246 http://en.wikipedia.org/wiki/MD5
247
248 The paper "How to Break MD5 and Other Hash Functions" by Xiaoyun Wang
249 and Hongbo Yu.
250
252 This library is free software; you can redistribute it and/or modify it
253 under the same terms as Perl itself.
254
255 Copyright 1998-2003 Gisle Aas.
256 Copyright 1995-1996 Neil Winton.
257 Copyright 1991-1992 RSA Data Security, Inc.
258
259 The MD5 algorithm is defined in RFC 1321. This implementation is
260 derived from the reference C code in RFC 1321 which is covered by the
261 following copyright statement:
262
263 · Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
264 rights reserved.
265
266 License to copy and use this software is granted provided that it
267 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
268 Algorithm" in all material mentioning or referencing this software
269 or this function.
270
271 License is also granted to make and use derivative works provided
272 that such works are identified as "derived from the RSA Data Secu‐
273 rity, Inc. MD5 Message-Digest Algorithm" in all material mentioning
274 or referencing the derived work.
275
276 RSA Data Security, Inc. makes no representations concerning either
277 the merchantability of this software or the suitability of this
278 software for any particular purpose. It is provided "as is" without
279 express or implied warranty of any kind.
280
281 These notices must be retained in any copies of any part of this
282 documentation and/or software.
283
284 This copyright does not prohibit distribution of any version of Perl
285 containing this extension under the terms of the GNU or Artistic
286 licenses.
287
289 The original "MD5" interface was written by Neil Winton ("N.Win‐
290 ton@axion.bt.co.uk").
291
292 The "Digest::MD5" module is written by Gisle Aas <gisle@ActiveS‐
293 tate.com>.
294
295
296
297perl v5.8.8 2001-09-21 Digest::MD5(3pm)