4 use vars qw($VERSION @ISA @EXPORT_OK);
9 *import = \&Exporter::import;
10 @EXPORT_OK = qw(md5 md5_hex md5_base64);
16 Digest::MD5->bootstrap($VERSION);
19 # Try to load the pure perl version
20 require Digest::Perl::MD5;
22 Digest::Perl::MD5->import(qw(md5 md5_hex md5_base64));
23 push(@ISA, "Digest::Perl::MD5"); # make OO interface work
34 Digest::MD5 - Perl interface to the MD5 Algorithm
39 use Digest::MD5 qw(md5 md5_hex md5_base64);
42 $digest = md5_hex($data);
43 $digest = md5_base64($data);
48 $ctx = Digest::MD5->new;
53 $digest = $ctx->digest;
54 $digest = $ctx->hexdigest;
55 $digest = $ctx->b64digest;
59 The C<Digest::MD5> module allows you to use the RSA Data Security
60 Inc. MD5 Message Digest algorithm from within Perl programs. The
61 algorithm takes as input a message of arbitrary length and produces as
62 output a 128-bit "fingerprint" or "message digest" of the input.
64 The C<Digest::MD5> module provide a procedural interface for simple
65 use, as well as an object oriented interface that can handle messages
66 of arbitrary length and which can read files directly.
68 A binary digest will be 16 bytes long. A hex digest will be 32
69 characters long. A base64 digest will be 22 characters long.
73 The following functions can be exported from the C<Digest::MD5>
74 module. No functions are exported by default.
80 This function will concatenate all arguments, calculate the MD5 digest
81 of this "message", and return it in binary form.
83 =item md5_hex($data,...)
85 Same as md5(), but will return the digest in hexadecimal form.
87 =item md5_base64($data,...)
89 Same as md5(), but will return the digest as a base64 encoded string.
95 The following methods are available:
99 =item $md5 = Digest::MD5->new
101 The constructor returns a new C<Digest::MD5> object which encapsulate
102 the state of the MD5 message-digest algorithm. You can add data to
103 the object and finally ask for the digest.
105 If called as a instance method (i.e. $md5->new) it will just reset the
106 state the object to the state of a newly created object. No new
107 object is created in this case.
111 This is just an alias for $md5->new.
113 =item $md5->add($data,...)
115 The $data provided as argument are appended to the message we
116 calculate the digest for. The return value is the $md5 object itself.
118 =item $md5->addfile($io_handle)
120 The $io_handle is read until EOF and the content is appended to the
121 message we calculate the digest for. The return value is the $md5
124 In most cases you want to make sure that the $io_handle is set up to
129 Return the binary digest for the message.
131 Note that the C<digest> operation is effectively a destructive,
132 read-once operation. Once it has been performed, the C<Digest::MD5>
133 object is automatically C<reset> and can be used to calculate another
136 =item $md5->hexdigest
138 Same as $md5->digest, but will return the digest in hexadecimal form.
140 =item $md5->b64digest
142 Same as $md5->digest, but will return the digest as a base64 encoded
150 The simplest way to use this library is to import the md5_hex()
151 function (or one of its cousins):
153 use Digest::MD5 qw(md5_hex);
154 print "Digest is ", md5_hex("foobarbaz"), "\n";
156 The above example would print out the message
158 Digest is 6df23dc03f9b54cc38a0fc1483df6e21
160 provided that the implementation is working correctly. The same
161 checksum can also be calculated in OO style:
165 $md5 = Digest::MD5->new;
166 $md5->add('foo', 'bar');
168 $digest = $md5->hexdigest;
170 print "Digest is $digest\n";
172 With OO style you can break the message arbitrary. This means that we
173 are no longer limited to have space for the whole message in memory, i.e.
174 we can handle messages of any size.
176 This is useful when calculating checksum for files:
180 my $file = shift || "/etc/passwd";
181 open(FILE, $file) or die "Can't open '$file': $!";
184 $md5 = Digest::MD5->new;
189 print $md5->b64digest, " $file\n";
191 Or we can use the builtin addfile method for more efficient reading of
196 my $file = shift || "/etc/passwd";
197 open(FILE, $file) or die "Can't open '$file': $!";
200 print Digest::MD5->new->addfile(*FILE)->hexdigest, " $file\n";
215 This library is free software; you can redistribute it and/or
216 modify it under the same terms as Perl itself.
218 Copyright 1998-2000 Gisle Aas.
219 Copyright 1995-1996 Neil Winton.
220 Copyright 1991-1992 RSA Data Security, Inc.
222 The MD5 algorithm is defined in RFC 1321. The basic C code
223 implementing the algorithm is derived from that in the RFC and is
224 covered by the following copyright:
230 Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
233 License to copy and use this software is granted provided that it
234 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
235 Algorithm" in all material mentioning or referencing this software
238 License is also granted to make and use derivative works provided
239 that such works are identified as "derived from the RSA Data
240 Security, Inc. MD5 Message-Digest Algorithm" in all material
241 mentioning or referencing the derived work.
243 RSA Data Security, Inc. makes no representations concerning either
244 the merchantability of this software or the suitability of this
245 software for any particular purpose. It is provided "as is"
246 without express or implied warranty of any kind.
248 These notices must be retained in any copies of any part of this
249 documentation and/or software.
253 This copyright does not prohibit distribution of any version of Perl
254 containing this extension under the terms of the GNU or Artistic
259 The original MD5 interface was written by Neil Winton
260 (C<N.Winton@axion.bt.co.uk>).
262 This release was made by Gisle Aas <gisle@ActiveState.com>