1MD5(3) User Contributed Perl Documentation MD5(3)
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_handle);
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
165 @ctx = $md5->context
166 $md5->context(@ctx)
167 Saves or restores the internal state. When called with no
168 arguments, returns a list: number of blocks processed, a 16-byte
169 internal state buffer, then optionally up to 63 bytes of
170 unprocessed data if there are any. When passed those same
171 arguments, restores the state. This is only useful for specialised
172 operations.
173
175 The simplest way to use this library is to import the md5_hex()
176 function (or one of its cousins):
177
178 use Digest::MD5 qw(md5_hex);
179 print "Digest is ", md5_hex("foobarbaz"), "\n";
180
181 The above example would print out the message:
182
183 Digest is 6df23dc03f9b54cc38a0fc1483df6e21
184
185 The same checksum can also be calculated in OO style:
186
187 use Digest::MD5;
188
189 $md5 = Digest::MD5->new;
190 $md5->add('foo', 'bar');
191 $md5->add('baz');
192 $digest = $md5->hexdigest;
193
194 print "Digest is $digest\n";
195
196 With OO style, you can break the message arbitrarily. This means that
197 we are no longer limited to have space for the whole message in memory,
198 i.e. we can handle messages of any size.
199
200 This is useful when calculating checksum for files:
201
202 use Digest::MD5;
203
204 my $filename = shift || "/etc/passwd";
205 open (my $fh, '<', $filename) or die "Can't open '$filename': $!";
206 binmode($fh);
207
208 $md5 = Digest::MD5->new;
209 while (<$fh>) {
210 $md5->add($_);
211 }
212 close($fh);
213 print $md5->b64digest, " $filename\n";
214
215 Or we can use the addfile method for more efficient reading of the
216 file:
217
218 use Digest::MD5;
219
220 my $filename = shift || "/etc/passwd";
221 open (my $fh, '<', $filename) or die "Can't open '$filename': $!";
222 binmode ($fh);
223
224 print Digest::MD5->new->addfile($fh)->hexdigest, " $filename\n";
225
226 Since the MD5 algorithm is only defined for strings of bytes, it can
227 not be used on strings that contains chars with ordinal number above
228 255 (Unicode strings). The MD5 functions and methods will croak if you
229 try to feed them such input data:
230
231 use Digest::MD5 qw(md5_hex);
232
233 my $str = "abc\x{300}";
234 print md5_hex($str), "\n"; # croaks
235 # Wide character in subroutine entry
236
237 What you can do is calculate the MD5 checksum of the UTF-8
238 representation of such strings. This is achieved by filtering the
239 string through encode_utf8() function:
240
241 use Digest::MD5 qw(md5_hex);
242 use Encode qw(encode_utf8);
243
244 my $str = "abc\x{300}";
245 print md5_hex(encode_utf8($str)), "\n";
246 # 8c2d46911f3f5a326455f0ed7a8ed3b3
247
249 Digest, Digest::MD2, Digest::SHA, Digest::HMAC
250
251 md5sum(1)
252
253 RFC 1321
254
255 http://en.wikipedia.org/wiki/MD5
256
257 The paper "How to Break MD5 and Other Hash Functions" by Xiaoyun Wang
258 and Hongbo Yu.
259
261 This library is free software; you can redistribute it and/or modify it
262 under the same terms as Perl itself.
263
264 Copyright 1998-2003 Gisle Aas.
265 Copyright 1995-1996 Neil Winton.
266 Copyright 1991-1992 RSA Data Security, Inc.
267
268 The MD5 algorithm is defined in RFC 1321. This implementation is
269 derived from the reference C code in RFC 1321 which is covered by the
270 following copyright statement:
271
272 • Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
273 rights reserved.
274
275 License to copy and use this software is granted provided that it
276 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
277 Algorithm" in all material mentioning or referencing this software
278 or this function.
279
280 License is also granted to make and use derivative works provided
281 that such works are identified as "derived from the RSA Data
282 Security, Inc. MD5 Message-Digest Algorithm" in all material
283 mentioning or referencing the derived work.
284
285 RSA Data Security, Inc. makes no representations concerning either
286 the merchantability of this software or the suitability of this
287 software for any particular purpose. It is provided "as is" without
288 express or implied warranty of any kind.
289
290 These notices must be retained in any copies of any part of this
291 documentation and/or software.
292
293 This copyright does not prohibit distribution of any version of Perl
294 containing this extension under the terms of the GNU or Artistic
295 licenses.
296
298 The original "MD5" interface was written by Neil Winton
299 ("N.Winton@axion.bt.co.uk").
300
301 The "Digest::MD5" module is written by Gisle Aas
302 <gisle@ActiveState.com>.
303
304
305
306perl v5.32.1 2021-01-27 MD5(3)