1MD5(3)                User Contributed Perl Documentation               MD5(3)
2
3
4

NAME

6       Digest::MD5 - Perl interface to the MD5 Algorithm
7

SYNOPSIS

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

DESCRIPTION

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

FUNCTIONS

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

METHODS

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 3-element list: number of blocks processed, a
169           16-byte internal state buffer, then up to 63 bytes of unprocessed
170           data.  When passed those same arguments, restores the state.  This
171           is only useful for specialised operations.
172

EXAMPLES

174       The simplest way to use this library is to import the md5_hex()
175       function (or one of its cousins):
176
177           use Digest::MD5 qw(md5_hex);
178           print "Digest is ", md5_hex("foobarbaz"), "\n";
179
180       The above example would print out the message:
181
182           Digest is 6df23dc03f9b54cc38a0fc1483df6e21
183
184       The same checksum can also be calculated in OO style:
185
186           use Digest::MD5;
187
188           $md5 = Digest::MD5->new;
189           $md5->add('foo', 'bar');
190           $md5->add('baz');
191           $digest = $md5->hexdigest;
192
193           print "Digest is $digest\n";
194
195       With OO style, you can break the message arbitrarily.  This means that
196       we are no longer limited to have space for the whole message in memory,
197       i.e.  we can handle messages of any size.
198
199       This is useful when calculating checksum for files:
200
201           use Digest::MD5;
202
203           my $filename = shift || "/etc/passwd";
204           open (my $fh, '<', $filename) or die "Can't open '$filename': $!";
205           binmode($fh);
206
207           $md5 = Digest::MD5->new;
208           while (<$fh>) {
209               $md5->add($_);
210           }
211           close($fh);
212           print $md5->b64digest, " $filename\n";
213
214       Or we can use the addfile method for more efficient reading of the
215       file:
216
217           use Digest::MD5;
218
219           my $filename = shift || "/etc/passwd";
220           open (my $fh, '<', $filename) or die "Can't open '$filename': $!";
221           binmode ($fh);
222
223           print Digest::MD5->new->addfile($fh)->hexdigest, " $filename\n";
224
225       Since the MD5 algorithm is only defined for strings of bytes, it can
226       not be used on strings that contains chars with ordinal number above
227       255 (Unicode strings).  The MD5 functions and methods will croak if you
228       try to feed them such input data:
229
230           use Digest::MD5 qw(md5_hex);
231
232           my $str = "abc\x{300}";
233           print md5_hex($str), "\n";  # croaks
234           # Wide character in subroutine entry
235
236       What you can do is calculate the MD5 checksum of the UTF-8
237       representation of such strings.  This is achieved by filtering the
238       string through encode_utf8() function:
239
240           use Digest::MD5 qw(md5_hex);
241           use Encode qw(encode_utf8);
242
243           my $str = "abc\x{300}";
244           print md5_hex(encode_utf8($str)), "\n";
245           # 8c2d46911f3f5a326455f0ed7a8ed3b3
246

SEE ALSO

248       Digest, Digest::MD2, Digest::SHA, Digest::HMAC
249
250       md5sum(1)
251
252       RFC 1321
253
254       http://en.wikipedia.org/wiki/MD5
255
256       The paper "How to Break MD5 and Other Hash Functions" by Xiaoyun Wang
257       and Hongbo Yu.
258
260       This library is free software; you can redistribute it and/or modify it
261       under the same terms as Perl itself.
262
263        Copyright 1998-2003 Gisle Aas.
264        Copyright 1995-1996 Neil Winton.
265        Copyright 1991-1992 RSA Data Security, Inc.
266
267       The MD5 algorithm is defined in RFC 1321. This implementation is
268       derived from the reference C code in RFC 1321 which is covered by the
269       following copyright statement:
270
271       ยท   Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
272           rights reserved.
273
274           License to copy and use this software is granted provided that it
275           is identified as the "RSA Data Security, Inc. MD5 Message-Digest
276           Algorithm" in all material mentioning or referencing this software
277           or this function.
278
279           License is also granted to make and use derivative works provided
280           that such works are identified as "derived from the RSA Data
281           Security, Inc. MD5 Message-Digest Algorithm" in all material
282           mentioning or referencing the derived work.
283
284           RSA Data Security, Inc. makes no representations concerning either
285           the merchantability of this software or the suitability of this
286           software for any particular purpose. It is provided "as is" without
287           express or implied warranty of any kind.
288
289           These notices must be retained in any copies of any part of this
290           documentation and/or software.
291
292       This copyright does not prohibit distribution of any version of Perl
293       containing this extension under the terms of the GNU or Artistic
294       licenses.
295

AUTHORS

297       The original "MD5" interface was written by Neil Winton
298       ("N.Winton@axion.bt.co.uk").
299
300       The "Digest::MD5" module is written by Gisle Aas
301       <gisle@ActiveState.com>.
302
303
304
305perl v5.28.1                      2016-03-09                            MD5(3)
Impressum