| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) |
| 3 |
.\" |
| 4 |
.\" Standard preamble: |
| 5 |
.\" ======================================================================== |
| 6 |
.de Sp \" Vertical space (when we can't use .PP) |
| 7 |
.if t .sp .5v |
| 8 |
.if n .sp |
| 9 |
.. |
| 10 |
.de Vb \" Begin verbatim text |
| 11 |
.ft CW |
| 12 |
.nf |
| 13 |
.ne \\$1 |
| 14 |
.. |
| 15 |
.de Ve \" End verbatim text |
| 16 |
.ft R |
| 17 |
.fi |
| 18 |
.. |
| 19 |
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. |
| 20 |
.ie n \{\ |
| 21 |
. ds C` "" |
| 22 |
. ds C' "" |
| 23 |
'br\} |
| 24 |
.el\{\ |
| 25 |
. ds C` |
| 26 |
. ds C' |
| 27 |
'br\} |
| 28 |
.\" |
| 29 |
.\" Escape single quotes in literal strings from groff's Unicode transform. |
| 30 |
.ie \n(.g .ds Aq \(aq |
| 31 |
.el .ds Aq ' |
| 32 |
.\" |
| 33 |
.\" If the F register is >0, we'll generate index entries on stderr for |
| 34 |
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 35 |
.\" entries marked with X<> in POD. Of course, you'll have to process the |
| 36 |
.\" output yourself in some meaningful fashion. |
| 37 |
.\" |
| 38 |
.\" Avoid warning from groff about undefined register 'F'. |
| 39 |
.de IX |
| 40 |
.. |
| 41 |
.nr rF 0 |
| 42 |
.if \n(.g .if rF .nr rF 1 |
| 43 |
.if (\n(rF:(\n(.g==0)) \{\ |
| 44 |
. if \nF \{\ |
| 45 |
. de IX |
| 46 |
. tm Index:\\$1\t\\n%\t"\\$2" |
| 47 |
.. |
| 48 |
. if !\nF==2 \{\ |
| 49 |
. nr % 0 |
| 50 |
. nr F 2 |
| 51 |
. \} |
| 52 |
. \} |
| 53 |
.\} |
| 54 |
.rr rF |
| 55 |
.\" ======================================================================== |
| 56 |
.\" |
| 57 |
.IX Title "Digest::MD5 3perl" |
| 58 |
.TH Digest::MD5 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide" |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
Digest::MD5 \- Perl interface to the MD5 Algorithm |
| 65 |
.SH SYNOPSIS |
| 66 |
.IX Header "SYNOPSIS" |
| 67 |
.Vb 2 |
| 68 |
\& # Functional style |
| 69 |
\& use Digest::MD5 qw(md5 md5_hex md5_base64); |
| 70 |
\& |
| 71 |
\& $digest = md5($data); |
| 72 |
\& $digest = md5_hex($data); |
| 73 |
\& $digest = md5_base64($data); |
| 74 |
\& |
| 75 |
\& # OO style |
| 76 |
\& use Digest::MD5; |
| 77 |
\& |
| 78 |
\& $ctx = Digest::MD5\->new; |
| 79 |
\& |
| 80 |
\& $ctx\->add($data); |
| 81 |
\& $ctx\->addfile($file_handle); |
| 82 |
\& |
| 83 |
\& $digest = $ctx\->digest; |
| 84 |
\& $digest = $ctx\->hexdigest; |
| 85 |
\& $digest = $ctx\->b64digest; |
| 86 |
.Ve |
| 87 |
.SH DESCRIPTION |
| 88 |
.IX Header "DESCRIPTION" |
| 89 |
The \f(CW\*(C`Digest::MD5\*(C'\fR module allows you to use the RSA Data Security |
| 90 |
Inc. MD5 Message Digest algorithm from within Perl programs. The |
| 91 |
algorithm takes as input a message of arbitrary length and produces as |
| 92 |
output a 128\-bit "fingerprint" or "message digest" of the input. |
| 93 |
.PP |
| 94 |
Note that the MD5 algorithm is not as strong as it used to be. It has |
| 95 |
since 2005 been easy to generate different messages that produce the |
| 96 |
same MD5 digest. It still seems hard to generate messages that |
| 97 |
produce a given digest, but it is probably wise to move to stronger |
| 98 |
algorithms for applications that depend on the digest to uniquely identify |
| 99 |
a message. |
| 100 |
.PP |
| 101 |
The \f(CW\*(C`Digest::MD5\*(C'\fR module provide a procedural interface for simple |
| 102 |
use, as well as an object oriented interface that can handle messages |
| 103 |
of arbitrary length and which can read files directly. |
| 104 |
.SH FUNCTIONS |
| 105 |
.IX Header "FUNCTIONS" |
| 106 |
The following functions are provided by the \f(CW\*(C`Digest::MD5\*(C'\fR module. |
| 107 |
None of these functions are exported by default. |
| 108 |
.IP md5($data,...) 4 |
| 109 |
.IX Item "md5($data,...)" |
| 110 |
This function will concatenate all arguments, calculate the MD5 digest |
| 111 |
of this "message", and return it in binary form. The returned string |
| 112 |
will be 16 bytes long. |
| 113 |
.Sp |
| 114 |
The result of md5("a", "b", "c") will be exactly the same as the |
| 115 |
result of md5("abc"). |
| 116 |
.IP md5_hex($data,...) 4 |
| 117 |
.IX Item "md5_hex($data,...)" |
| 118 |
Same as \fBmd5()\fR, but will return the digest in hexadecimal form. The |
| 119 |
length of the returned string will be 32 and it will only contain |
| 120 |
characters from this set: '0'..'9' and 'a'..'f'. |
| 121 |
.IP md5_base64($data,...) 4 |
| 122 |
.IX Item "md5_base64($data,...)" |
| 123 |
Same as \fBmd5()\fR, but will return the digest as a base64 encoded string. |
| 124 |
The length of the returned string will be 22 and it will only contain |
| 125 |
characters from this set: 'A'..'Z', 'a'..'z', '0'..'9', '+' and |
| 126 |
\&'/'. |
| 127 |
.Sp |
| 128 |
Note that the base64 encoded string returned is not padded to be a |
| 129 |
multiple of 4 bytes long. If you want interoperability with other |
| 130 |
base64 encoded md5 digests you might want to append the redundant |
| 131 |
string "==" to the result. |
| 132 |
.SH METHODS |
| 133 |
.IX Header "METHODS" |
| 134 |
The object oriented interface to \f(CW\*(C`Digest::MD5\*(C'\fR is described in this |
| 135 |
section. After a \f(CW\*(C`Digest::MD5\*(C'\fR object has been created, you will add |
| 136 |
data to it and finally ask for the digest in a suitable format. A |
| 137 |
single object can be used to calculate multiple digests. |
| 138 |
.PP |
| 139 |
The following methods are provided: |
| 140 |
.ie n .IP "$md5 = Digest::MD5\->new" 4 |
| 141 |
.el .IP "\f(CW$md5\fR = Digest::MD5\->new" 4 |
| 142 |
.IX Item "$md5 = Digest::MD5->new" |
| 143 |
The constructor returns a new \f(CW\*(C`Digest::MD5\*(C'\fR object which encapsulate |
| 144 |
the state of the MD5 message-digest algorithm. |
| 145 |
.Sp |
| 146 |
If called as an instance method (i.e. \f(CW$md5\fR\->new) it will just reset the |
| 147 |
state the object to the state of a newly created object. No new |
| 148 |
object is created in this case. |
| 149 |
.ie n .IP $md5\->reset 4 |
| 150 |
.el .IP \f(CW$md5\fR\->reset 4 |
| 151 |
.IX Item "$md5->reset" |
| 152 |
This is just an alias for \f(CW$md5\fR\->new. |
| 153 |
.ie n .IP $md5\->clone 4 |
| 154 |
.el .IP \f(CW$md5\fR\->clone 4 |
| 155 |
.IX Item "$md5->clone" |
| 156 |
This a copy of the \f(CW$md5\fR object. It is useful when you do not want to |
| 157 |
destroy the digests state, but need an intermediate value of the |
| 158 |
digest, e.g. when calculating digests iteratively on a continuous data |
| 159 |
stream. Example: |
| 160 |
.Sp |
| 161 |
.Vb 5 |
| 162 |
\& my $md5 = Digest::MD5\->new; |
| 163 |
\& while (<>) { |
| 164 |
\& $md5\->add($_); |
| 165 |
\& print "Line $.: ", $md5\->clone\->hexdigest, "\en"; |
| 166 |
\& } |
| 167 |
.Ve |
| 168 |
.ie n .IP $md5\->add($data,...) 4 |
| 169 |
.el .IP \f(CW$md5\fR\->add($data,...) 4 |
| 170 |
.IX Item "$md5->add($data,...)" |
| 171 |
The \f(CW$data\fR provided as argument are appended to the message we |
| 172 |
calculate the digest for. The return value is the \f(CW$md5\fR object itself. |
| 173 |
.Sp |
| 174 |
All these lines will have the same effect on the state of the \f(CW$md5\fR |
| 175 |
object: |
| 176 |
.Sp |
| 177 |
.Vb 4 |
| 178 |
\& $md5\->add("a"); $md5\->add("b"); $md5\->add("c"); |
| 179 |
\& $md5\->add("a")\->add("b")\->add("c"); |
| 180 |
\& $md5\->add("a", "b", "c"); |
| 181 |
\& $md5\->add("abc"); |
| 182 |
.Ve |
| 183 |
.ie n .IP $md5\->addfile($io_handle) 4 |
| 184 |
.el .IP \f(CW$md5\fR\->addfile($io_handle) 4 |
| 185 |
.IX Item "$md5->addfile($io_handle)" |
| 186 |
The \f(CW$io_handle\fR will be read until EOF and its content appended to the |
| 187 |
message we calculate the digest for. The return value is the \f(CW$md5\fR |
| 188 |
object itself. |
| 189 |
.Sp |
| 190 |
The \fBaddfile()\fR method will \fBcroak()\fR if it fails reading data for some |
| 191 |
reason. If it croaks it is unpredictable what the state of the \f(CW$md5\fR |
| 192 |
object will be in. The \fBaddfile()\fR method might have been able to read |
| 193 |
the file partially before it failed. It is probably wise to discard |
| 194 |
or reset the \f(CW$md5\fR object if this occurs. |
| 195 |
.Sp |
| 196 |
In most cases you want to make sure that the \f(CW$io_handle\fR is in |
| 197 |
\&\f(CW\*(C`binmode\*(C'\fR before you pass it as argument to the \fBaddfile()\fR method. |
| 198 |
.ie n .IP "$md5\->add_bits($data, $nbits)" 4 |
| 199 |
.el .IP "\f(CW$md5\fR\->add_bits($data, \f(CW$nbits\fR)" 4 |
| 200 |
.IX Item "$md5->add_bits($data, $nbits)" |
| 201 |
.PD 0 |
| 202 |
.ie n .IP $md5\->add_bits($bitstring) 4 |
| 203 |
.el .IP \f(CW$md5\fR\->add_bits($bitstring) 4 |
| 204 |
.IX Item "$md5->add_bits($bitstring)" |
| 205 |
.PD |
| 206 |
Since the MD5 algorithm is byte oriented you might only add bits as |
| 207 |
multiples of 8, so you probably want to just use \fBadd()\fR instead. The |
| 208 |
\&\fBadd_bits()\fR method is provided for compatibility with other digest |
| 209 |
implementations. See Digest for description of the arguments |
| 210 |
that \fBadd_bits()\fR take. |
| 211 |
.ie n .IP $md5\->digest 4 |
| 212 |
.el .IP \f(CW$md5\fR\->digest 4 |
| 213 |
.IX Item "$md5->digest" |
| 214 |
Return the binary digest for the message. The returned string will be |
| 215 |
16 bytes long. |
| 216 |
.Sp |
| 217 |
Note that the \f(CW\*(C`digest\*(C'\fR operation is effectively a destructive, |
| 218 |
read-once operation. Once it has been performed, the \f(CW\*(C`Digest::MD5\*(C'\fR |
| 219 |
object is automatically \f(CW\*(C`reset\*(C'\fR and can be used to calculate another |
| 220 |
digest value. Call \f(CW$md5\fR\->clone\->digest if you want to calculate the |
| 221 |
digest without resetting the digest state. |
| 222 |
.ie n .IP $md5\->hexdigest 4 |
| 223 |
.el .IP \f(CW$md5\fR\->hexdigest 4 |
| 224 |
.IX Item "$md5->hexdigest" |
| 225 |
Same as \f(CW$md5\fR\->digest, but will return the digest in hexadecimal |
| 226 |
form. The length of the returned string will be 32 and it will only |
| 227 |
contain characters from this set: '0'..'9' and 'a'..'f'. |
| 228 |
.ie n .IP $md5\->b64digest 4 |
| 229 |
.el .IP \f(CW$md5\fR\->b64digest 4 |
| 230 |
.IX Item "$md5->b64digest" |
| 231 |
Same as \f(CW$md5\fR\->digest, but will return the digest as a base64 encoded |
| 232 |
string. The length of the returned string will be 22 and it will only |
| 233 |
contain characters from this set: 'A'..'Z', 'a'..'z', '0'..'9', '+' |
| 234 |
and '/'. |
| 235 |
.Sp |
| 236 |
The base64 encoded string returned is not padded to be a multiple of 4 |
| 237 |
bytes long. If you want interoperability with other base64 encoded |
| 238 |
md5 digests you might want to append the string "==" to the result. |
| 239 |
.ie n .IP "@ctx = $md5\->context" 4 |
| 240 |
.el .IP "\f(CW@ctx\fR = \f(CW$md5\fR\->context" 4 |
| 241 |
.IX Item "@ctx = $md5->context" |
| 242 |
.PD 0 |
| 243 |
.ie n .IP $md5\->context(@ctx) 4 |
| 244 |
.el .IP \f(CW$md5\fR\->context(@ctx) 4 |
| 245 |
.IX Item "$md5->context(@ctx)" |
| 246 |
.PD |
| 247 |
Saves or restores the internal state. |
| 248 |
When called with no arguments, returns a list: |
| 249 |
number of blocks processed, |
| 250 |
a 16\-byte internal state buffer, |
| 251 |
then optionally up to 63 bytes of unprocessed data if there are any. |
| 252 |
When passed those same arguments, restores the state. |
| 253 |
This is only useful for specialised operations. |
| 254 |
.SH EXAMPLES |
| 255 |
.IX Header "EXAMPLES" |
| 256 |
The simplest way to use this library is to import the \fBmd5_hex()\fR |
| 257 |
function (or one of its cousins): |
| 258 |
.PP |
| 259 |
.Vb 2 |
| 260 |
\& use Digest::MD5 qw(md5_hex); |
| 261 |
\& print "Digest is ", md5_hex("foobarbaz"), "\en"; |
| 262 |
.Ve |
| 263 |
.PP |
| 264 |
The above example would print out the message: |
| 265 |
.PP |
| 266 |
.Vb 1 |
| 267 |
\& Digest is 6df23dc03f9b54cc38a0fc1483df6e21 |
| 268 |
.Ve |
| 269 |
.PP |
| 270 |
The same checksum can also be calculated in OO style: |
| 271 |
.PP |
| 272 |
.Vb 1 |
| 273 |
\& use Digest::MD5; |
| 274 |
\& |
| 275 |
\& $md5 = Digest::MD5\->new; |
| 276 |
\& $md5\->add(\*(Aqfoo\*(Aq, \*(Aqbar\*(Aq); |
| 277 |
\& $md5\->add(\*(Aqbaz\*(Aq); |
| 278 |
\& $digest = $md5\->hexdigest; |
| 279 |
\& |
| 280 |
\& print "Digest is $digest\en"; |
| 281 |
.Ve |
| 282 |
.PP |
| 283 |
With OO style, you can break the message arbitrarily. This means that we |
| 284 |
are no longer limited to have space for the whole message in memory, i.e. |
| 285 |
we can handle messages of any size. |
| 286 |
.PP |
| 287 |
This is useful when calculating checksum for files: |
| 288 |
.PP |
| 289 |
.Vb 1 |
| 290 |
\& use Digest::MD5; |
| 291 |
\& |
| 292 |
\& my $filename = shift || "/etc/passwd"; |
| 293 |
\& open (my $fh, \*(Aq<\*(Aq, $filename) or die "Can\*(Aqt open \*(Aq$filename\*(Aq: $!"; |
| 294 |
\& binmode($fh); |
| 295 |
\& |
| 296 |
\& $md5 = Digest::MD5\->new; |
| 297 |
\& while (<$fh>) { |
| 298 |
\& $md5\->add($_); |
| 299 |
\& } |
| 300 |
\& close($fh); |
| 301 |
\& print $md5\->b64digest, " $filename\en"; |
| 302 |
.Ve |
| 303 |
.PP |
| 304 |
Or we can use the addfile method for more efficient reading of |
| 305 |
the file: |
| 306 |
.PP |
| 307 |
.Vb 1 |
| 308 |
\& use Digest::MD5; |
| 309 |
\& |
| 310 |
\& my $filename = shift || "/etc/passwd"; |
| 311 |
\& open (my $fh, \*(Aq<\*(Aq, $filename) or die "Can\*(Aqt open \*(Aq$filename\*(Aq: $!"; |
| 312 |
\& binmode ($fh); |
| 313 |
\& |
| 314 |
\& print Digest::MD5\->new\->addfile($fh)\->hexdigest, " $filename\en"; |
| 315 |
.Ve |
| 316 |
.PP |
| 317 |
Since the MD5 algorithm is only defined for strings of bytes, it can not be |
| 318 |
used on strings that contains chars with ordinal number above 255 (Unicode |
| 319 |
strings). The MD5 functions and methods will croak if you try to feed them |
| 320 |
such input data: |
| 321 |
.PP |
| 322 |
.Vb 1 |
| 323 |
\& use Digest::MD5 qw(md5_hex); |
| 324 |
\& |
| 325 |
\& my $str = "abc\ex{300}"; |
| 326 |
\& print md5_hex($str), "\en"; # croaks |
| 327 |
\& # Wide character in subroutine entry |
| 328 |
.Ve |
| 329 |
.PP |
| 330 |
What you can do is calculate the MD5 checksum of the UTF\-8 |
| 331 |
representation of such strings. This is achieved by filtering the |
| 332 |
string through \fBencode_utf8()\fR function: |
| 333 |
.PP |
| 334 |
.Vb 2 |
| 335 |
\& use Digest::MD5 qw(md5_hex); |
| 336 |
\& use Encode qw(encode_utf8); |
| 337 |
\& |
| 338 |
\& my $str = "abc\ex{300}"; |
| 339 |
\& print md5_hex(encode_utf8($str)), "\en"; |
| 340 |
\& # 8c2d46911f3f5a326455f0ed7a8ed3b3 |
| 341 |
.Ve |
| 342 |
.SH "SEE ALSO" |
| 343 |
.IX Header "SEE ALSO" |
| 344 |
Digest, |
| 345 |
Digest::MD2, |
| 346 |
Digest::SHA, |
| 347 |
Digest::HMAC |
| 348 |
.PP |
| 349 |
\&\fBmd5sum\fR\|(1) |
| 350 |
.PP |
| 351 |
RFC 1321 |
| 352 |
.PP |
| 353 |
http://en.wikipedia.org/wiki/MD5 |
| 354 |
.PP |
| 355 |
The paper "How to Break MD5 and Other Hash Functions" by Xiaoyun Wang |
| 356 |
and Hongbo Yu. |
| 357 |
.SH COPYRIGHT |
| 358 |
.IX Header "COPYRIGHT" |
| 359 |
This library is free software; you can redistribute it and/or |
| 360 |
modify it under the same terms as Perl itself. |
| 361 |
.PP |
| 362 |
.Vb 3 |
| 363 |
\& Copyright 1998\-2003 Gisle Aas. |
| 364 |
\& Copyright 1995\-1996 Neil Winton. |
| 365 |
\& Copyright 1991\-1992 RSA Data Security, Inc. |
| 366 |
.Ve |
| 367 |
.PP |
| 368 |
The MD5 algorithm is defined in RFC 1321. This implementation is |
| 369 |
derived from the reference C code in RFC 1321 which is covered by |
| 370 |
the following copyright statement: |
| 371 |
.IP \(bu 4 |
| 372 |
Copyright (C) 1991\-2, RSA Data Security, Inc. Created 1991. All |
| 373 |
rights reserved. |
| 374 |
.Sp |
| 375 |
License to copy and use this software is granted provided that it |
| 376 |
is identified as the "RSA Data Security, Inc. MD5 Message-Digest |
| 377 |
Algorithm" in all material mentioning or referencing this software |
| 378 |
or this function. |
| 379 |
.Sp |
| 380 |
License is also granted to make and use derivative works provided |
| 381 |
that such works are identified as "derived from the RSA Data |
| 382 |
Security, Inc. MD5 Message-Digest Algorithm" in all material |
| 383 |
mentioning or referencing the derived work. |
| 384 |
.Sp |
| 385 |
RSA Data Security, Inc. makes no representations concerning either |
| 386 |
the merchantability of this software or the suitability of this |
| 387 |
software for any particular purpose. It is provided "as is" |
| 388 |
without express or implied warranty of any kind. |
| 389 |
.Sp |
| 390 |
These notices must be retained in any copies of any part of this |
| 391 |
documentation and/or software. |
| 392 |
.PP |
| 393 |
This copyright does not prohibit distribution of any version of Perl |
| 394 |
containing this extension under the terms of the GNU or Artistic |
| 395 |
licenses. |
| 396 |
.SH AUTHORS |
| 397 |
.IX Header "AUTHORS" |
| 398 |
The original \f(CW\*(C`MD5\*(C'\fR interface was written by Neil Winton |
| 399 |
(\f(CW\*(C`[email protected]\*(C'\fR). |
| 400 |
.PP |
| 401 |
The \f(CW\*(C`Digest::MD5\*(C'\fR module is written by Gisle Aas <[email protected]>. |