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