| 1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
| 2 | .\" |
| 3 | .\" Standard preamble: |
| 4 | .\" ======================================================================== |
| 5 | .de Sh \" Subsection heading |
| 6 | .br |
| 7 | .if t .Sp |
| 8 | .ne 5 |
| 9 | .PP |
| 10 | \fB\\$1\fR |
| 11 | .PP |
| 12 | .. |
| 13 | .de Sp \" Vertical space (when we can't use .PP) |
| 14 | .if t .sp .5v |
| 15 | .if n .sp |
| 16 | .. |
| 17 | .de Vb \" Begin verbatim text |
| 18 | .ft CW |
| 19 | .nf |
| 20 | .ne \\$1 |
| 21 | .. |
| 22 | .de Ve \" End verbatim text |
| 23 | .ft R |
| 24 | .fi |
| 25 | .. |
| 26 | .\" Set up some character translations and predefined strings. \*(-- will |
| 27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left |
| 28 | .\" double quote, and \*(R" will give a right double quote. | will give a |
| 29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to |
| 30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' |
| 31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. |
| 32 | .tr \(*W-|\(bv\*(Tr |
| 33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' |
| 34 | .ie n \{\ |
| 35 | . ds -- \(*W- |
| 36 | . ds PI pi |
| 37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch |
| 38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch |
| 39 | . ds L" "" |
| 40 | . ds R" "" |
| 41 | . ds C` "" |
| 42 | . ds C' "" |
| 43 | 'br\} |
| 44 | .el\{\ |
| 45 | . ds -- \|\(em\| |
| 46 | . ds PI \(*p |
| 47 | . ds L" `` |
| 48 | . ds R" '' |
| 49 | 'br\} |
| 50 | .\" |
| 51 | .\" If the F register is turned on, we'll generate index entries on stderr for |
| 52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index |
| 53 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
| 54 | .\" output yourself in some meaningful fashion. |
| 55 | .if \nF \{\ |
| 56 | . de IX |
| 57 | . tm Index:\\$1\t\\n%\t"\\$2" |
| 58 | .. |
| 59 | . nr % 0 |
| 60 | . rr F |
| 61 | .\} |
| 62 | .\" |
| 63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 64 | .\" way too many mistakes in technical documents. |
| 65 | .hy 0 |
| 66 | .if n .na |
| 67 | .\" |
| 68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). |
| 69 | .\" Fear. Run. Save yourself. No user-serviceable parts. |
| 70 | . \" fudge factors for nroff and troff |
| 71 | .if n \{\ |
| 72 | . ds #H 0 |
| 73 | . ds #V .8m |
| 74 | . ds #F .3m |
| 75 | . ds #[ \f1 |
| 76 | . ds #] \fP |
| 77 | .\} |
| 78 | .if t \{\ |
| 79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) |
| 80 | . ds #V .6m |
| 81 | . ds #F 0 |
| 82 | . ds #[ \& |
| 83 | . ds #] \& |
| 84 | .\} |
| 85 | . \" simple accents for nroff and troff |
| 86 | .if n \{\ |
| 87 | . ds ' \& |
| 88 | . ds ` \& |
| 89 | . ds ^ \& |
| 90 | . ds , \& |
| 91 | . ds ~ ~ |
| 92 | . ds / |
| 93 | .\} |
| 94 | .if t \{\ |
| 95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" |
| 96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' |
| 97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' |
| 98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' |
| 99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' |
| 100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' |
| 101 | .\} |
| 102 | . \" troff and (daisy-wheel) nroff accents |
| 103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' |
| 104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' |
| 105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] |
| 106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' |
| 107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' |
| 108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] |
| 109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] |
| 110 | .ds ae a\h'-(\w'a'u*4/10)'e |
| 111 | .ds Ae A\h'-(\w'A'u*4/10)'E |
| 112 | . \" corrections for vroff |
| 113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' |
| 114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' |
| 115 | . \" for low resolution devices (crt and lpr) |
| 116 | .if \n(.H>23 .if \n(.V>19 \ |
| 117 | \{\ |
| 118 | . ds : e |
| 119 | . ds 8 ss |
| 120 | . ds o a |
| 121 | . ds d- d\h'-1'\(ga |
| 122 | . ds D- D\h'-1'\(hy |
| 123 | . ds th \o'bp' |
| 124 | . ds Th \o'LP' |
| 125 | . ds ae ae |
| 126 | . ds Ae AE |
| 127 | .\} |
| 128 | .rm #[ #] #H #V #F C |
| 129 | .\" ======================================================================== |
| 130 | .\" |
| 131 | .IX Title "Digest 3" |
| 132 | .TH Digest 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide" |
| 133 | .SH "NAME" |
| 134 | Digest \- Modules that calculate message digests |
| 135 | .SH "SYNOPSIS" |
| 136 | .IX Header "SYNOPSIS" |
| 137 | .Vb 5 |
| 138 | \& $md5 = Digest->new("MD5"); |
| 139 | \& $sha1 = Digest->new("SHA-1"); |
| 140 | \& $sha256 = Digest->new("SHA-256"); |
| 141 | \& $sha384 = Digest->new("SHA-384"); |
| 142 | \& $sha512 = Digest->new("SHA-512"); |
| 143 | .Ve |
| 144 | .PP |
| 145 | .Vb 1 |
| 146 | \& $hmac = Digest->HMAC_MD5($key); |
| 147 | .Ve |
| 148 | .SH "DESCRIPTION" |
| 149 | .IX Header "DESCRIPTION" |
| 150 | The \f(CW\*(C`Digest::\*(C'\fR modules calculate digests, also called \*(L"fingerprints\*(R" |
| 151 | or \*(L"hashes\*(R", of some data, called a message. The digest is (usually) |
| 152 | some small/fixed size string. The actual size of the digest depend of |
| 153 | the algorithm used. The message is simply a sequence of arbitrary |
| 154 | bytes or bits. |
| 155 | .PP |
| 156 | An important property of the digest algorithms is that the digest is |
| 157 | \&\fIlikely\fR to change if the message change in some way. Another |
| 158 | property is that digest functions are one-way functions, that is it |
| 159 | should be \fIhard\fR to find a message that correspond to some given |
| 160 | digest. Algorithms differ in how \*(L"likely\*(R" and how \*(L"hard\*(R", as well as |
| 161 | how efficient they are to compute. |
| 162 | .PP |
| 163 | Note that the properties of the algorithms change over time, as the |
| 164 | algorithms are analyzed and machines grow faster. If your application |
| 165 | for instance depends on it being \*(L"impossible\*(R" to generate the same |
| 166 | digest for a different message it is wise to make it easy to plug in |
| 167 | stronger algorithms as the one used grow weaker. Using the interface |
| 168 | documented here should make it easy to change algorithms later. |
| 169 | .PP |
| 170 | All \f(CW\*(C`Digest::\*(C'\fR modules provide the same programming interface. A |
| 171 | functional interface for simple use, as well as an object oriented |
| 172 | interface that can handle messages of arbitrary length and which can |
| 173 | read files directly. |
| 174 | .PP |
| 175 | The digest can be delivered in three formats: |
| 176 | .IP "\fIbinary\fR" 8 |
| 177 | .IX Item "binary" |
| 178 | This is the most compact form, but it is not well suited for printing |
| 179 | or embedding in places that can't handle arbitrary data. |
| 180 | .IP "\fIhex\fR" 8 |
| 181 | .IX Item "hex" |
| 182 | A twice as long string of lowercase hexadecimal digits. |
| 183 | .IP "\fIbase64\fR" 8 |
| 184 | .IX Item "base64" |
| 185 | A string of portable printable characters. This is the base64 encoded |
| 186 | representation of the digest with any trailing padding removed. The |
| 187 | string will be about 30% longer than the binary version. |
| 188 | MIME::Base64 tells you more about this encoding. |
| 189 | .PP |
| 190 | The functional interface is simply importable functions with the same |
| 191 | name as the algorithm. The functions take the message as argument and |
| 192 | return the digest. Example: |
| 193 | .PP |
| 194 | .Vb 2 |
| 195 | \& use Digest::MD5 qw(md5); |
| 196 | \& $digest = md5($message); |
| 197 | .Ve |
| 198 | .PP |
| 199 | There are also versions of the functions with \*(L"_hex\*(R" or \*(L"_base64\*(R" |
| 200 | appended to the name, which returns the digest in the indicated form. |
| 201 | .SH "OO INTERFACE" |
| 202 | .IX Header "OO INTERFACE" |
| 203 | The following methods are available for all \f(CW\*(C`Digest::\*(C'\fR modules: |
| 204 | .IP "$ctx = Digest\->\s-1XXX\s0($arg,...)" 4 |
| 205 | .IX Item "$ctx = Digest->XXX($arg,...)" |
| 206 | .PD 0 |
| 207 | .ie n .IP "$ctx = Digest\->new(\s-1XXX\s0 => $arg,...)" 4 |
| 208 | .el .IP "$ctx = Digest\->new(\s-1XXX\s0 => \f(CW$arg\fR,...)" 4 |
| 209 | .IX Item "$ctx = Digest->new(XXX => $arg,...)" |
| 210 | .IP "$ctx = Digest::XXX\->new($arg,...)" 4 |
| 211 | .IX Item "$ctx = Digest::XXX->new($arg,...)" |
| 212 | .PD |
| 213 | The constructor returns some object that encapsulate the state of the |
| 214 | message-digest algorithm. You can add data to the object and finally |
| 215 | ask for the digest. The \*(L"\s-1XXX\s0\*(R" should of course be replaced by the proper |
| 216 | name of the digest algorithm you want to use. |
| 217 | .Sp |
| 218 | The two first forms are simply syntactic sugar which automatically |
| 219 | load the right module on first use. The second form allow you to use |
| 220 | algorithm names which contains letters which are not legal perl |
| 221 | identifiers, e.g. \*(L"\s-1SHA\-1\s0\*(R". If no implementation for the given algorithm |
| 222 | can be found, then an exception is raised. |
| 223 | .Sp |
| 224 | If \fInew()\fR is called as an instance method (i.e. \f(CW$ctx\fR\->new) it will just |
| 225 | reset the state the object to the state of a newly created object. No |
| 226 | new object is created in this case, and the return value is the |
| 227 | reference to the object (i.e. \f(CW$ctx\fR). |
| 228 | .ie n .IP "$other_ctx = $ctx\->clone" 4 |
| 229 | .el .IP "$other_ctx = \f(CW$ctx\fR\->clone" 4 |
| 230 | .IX Item "$other_ctx = $ctx->clone" |
| 231 | The clone method creates a copy of the digest state object and returns |
| 232 | a reference to the copy. |
| 233 | .IP "$ctx\->reset" 4 |
| 234 | .IX Item "$ctx->reset" |
| 235 | This is just an alias for \f(CW$ctx\fR\->new. |
| 236 | .ie n .IP "$ctx\->add( $data, ... )" 4 |
| 237 | .el .IP "$ctx\->add( \f(CW$data\fR, ... )" 4 |
| 238 | .IX Item "$ctx->add( $data, ... )" |
| 239 | The \f(CW$data\fR provided as argument are appended to the message we |
| 240 | calculate the digest for. The return value is the \f(CW$ctx\fR object itself. |
| 241 | .ie n .IP "$ctx\->addfile( $io_handle )" 4 |
| 242 | .el .IP "$ctx\->addfile( \f(CW$io_handle\fR )" 4 |
| 243 | .IX Item "$ctx->addfile( $io_handle )" |
| 244 | The \f(CW$io_handle\fR is read until \s-1EOF\s0 and the content is appended to the |
| 245 | message we calculate the digest for. The return value is the \f(CW$ctx\fR |
| 246 | object itself. |
| 247 | .ie n .IP "$ctx\->add_bits( $data\fR, \f(CW$nbits )" 4 |
| 248 | .el .IP "$ctx\->add_bits( \f(CW$data\fR, \f(CW$nbits\fR )" 4 |
| 249 | .IX Item "$ctx->add_bits( $data, $nbits )" |
| 250 | .PD 0 |
| 251 | .ie n .IP "$ctx\->add_bits( $bitstring )" 4 |
| 252 | .el .IP "$ctx\->add_bits( \f(CW$bitstring\fR )" 4 |
| 253 | .IX Item "$ctx->add_bits( $bitstring )" |
| 254 | .PD |
| 255 | The bits provided are appended to the message we calculate the digest |
| 256 | for. The return value is the \f(CW$ctx\fR object itself. |
| 257 | .Sp |
| 258 | The two argument form of \fIadd_bits()\fR will add the first \f(CW$nbits\fR bits |
| 259 | from data. For the last potentially partial byte only the high order |
| 260 | \&\f(CW\*(C`$nbits % 8\*(C'\fR bits are used. If \f(CW$nbits\fR is greater than \f(CW\*(C`length($data) * 8\*(C'\fR, then this method would do the same as \f(CW\*(C`$ctx\->add($data)\*(C'\fR, that is \f(CW$nbits\fR is silently ignored. |
| 261 | .Sp |
| 262 | The one argument form of \fIadd_bits()\fR takes a \f(CW$bitstring\fR of \*(L"1\*(R" and \*(L"0\*(R" |
| 263 | chars as argument. It's a shorthand for \f(CW\*(C`$ctx\->add_bits(pack("B*", |
| 264 | $bitstring), length($bitstring))\*(C'\fR. |
| 265 | .Sp |
| 266 | This example shows two calls that should have the same effect: |
| 267 | .Sp |
| 268 | .Vb 2 |
| 269 | \& $ctx->add_bits("111100001010"); |
| 270 | \& $ctx->add_bits("\exF0\exA0", 12); |
| 271 | .Ve |
| 272 | .Sp |
| 273 | Most digest algorithms are byte based. For those it is not possible |
| 274 | to add bits that are not a multiple of 8, and the \fIadd_bits()\fR method |
| 275 | will croak if you try. |
| 276 | .IP "$ctx\->digest" 4 |
| 277 | .IX Item "$ctx->digest" |
| 278 | Return the binary digest for the message. |
| 279 | .Sp |
| 280 | Note that the \f(CW\*(C`digest\*(C'\fR operation is effectively a destructive, |
| 281 | read-once operation. Once it has been performed, the \f(CW$ctx\fR object is |
| 282 | automatically \f(CW\*(C`reset\*(C'\fR and can be used to calculate another digest |
| 283 | value. Call \f(CW$ctx\fR\->clone\->digest if you want to calculate the digest |
| 284 | without reseting the digest state. |
| 285 | .IP "$ctx\->hexdigest" 4 |
| 286 | .IX Item "$ctx->hexdigest" |
| 287 | Same as \f(CW$ctx\fR\->digest, but will return the digest in hexadecimal form. |
| 288 | .IP "$ctx\->b64digest" 4 |
| 289 | .IX Item "$ctx->b64digest" |
| 290 | Same as \f(CW$ctx\fR\->digest, but will return the digest as a base64 encoded |
| 291 | string. |
| 292 | .SH "Digest speed" |
| 293 | .IX Header "Digest speed" |
| 294 | This table should give some indication on the relative speed of |
| 295 | different algorithms. It is sorted by throughput based on a benchmark |
| 296 | done with of some implementations of this \s-1API:\s0 |
| 297 | .PP |
| 298 | .Vb 1 |
| 299 | \& Algorithm Size Implementation MB/s |
| 300 | .Ve |
| 301 | .PP |
| 302 | .Vb 13 |
| 303 | \& MD4 128 Digest::MD4 v1.3 165.0 |
| 304 | \& MD5 128 Digest::MD5 v2.33 98.8 |
| 305 | \& SHA-256 256 Digest::SHA2 v1.1.0 66.7 |
| 306 | \& SHA-1 160 Digest::SHA v4.3.1 58.9 |
| 307 | \& SHA-1 160 Digest::SHA1 v2.10 48.8 |
| 308 | \& SHA-256 256 Digest::SHA v4.3.1 41.3 |
| 309 | \& Haval-256 256 Digest::Haval256 v1.0.4 39.8 |
| 310 | \& SHA-384 384 Digest::SHA2 v1.1.0 19.6 |
| 311 | \& SHA-512 512 Digest::SHA2 v1.1.0 19.3 |
| 312 | \& SHA-384 384 Digest::SHA v4.3.1 19.2 |
| 313 | \& SHA-512 512 Digest::SHA v4.3.1 19.2 |
| 314 | \& Whirlpool 512 Digest::Whirlpool v1.0.2 13.0 |
| 315 | \& MD2 128 Digest::MD2 v2.03 9.5 |
| 316 | .Ve |
| 317 | .PP |
| 318 | .Vb 5 |
| 319 | \& Adler-32 32 Digest::Adler32 v0.03 1.3 |
| 320 | \& CRC-16 16 Digest::CRC v0.05 1.1 |
| 321 | \& CRC-32 32 Digest::CRC v0.05 1.1 |
| 322 | \& MD5 128 Digest::Perl::MD5 v1.5 1.0 |
| 323 | \& CRC-CCITT 16 Digest::CRC v0.05 0.8 |
| 324 | .Ve |
| 325 | .PP |
| 326 | These numbers was achieved Apr 2004 with ActivePerl\-5.8.3 running |
| 327 | under Linux on a P4 2.8 GHz \s-1CPU\s0. The last 5 entries differ by being |
| 328 | pure perl implementations of the algorithms, which explains why they |
| 329 | are so slow. |
| 330 | .SH "SEE ALSO" |
| 331 | .IX Header "SEE ALSO" |
| 332 | Digest::Adler32, Digest::CRC, Digest::Haval256, |
| 333 | Digest::HMAC, Digest::MD2, Digest::MD4, Digest::MD5, |
| 334 | Digest::SHA, Digest::SHA1, Digest::SHA2, Digest::Whirlpool |
| 335 | .PP |
| 336 | New digest implementations should consider subclassing from Digest::base. |
| 337 | .PP |
| 338 | MIME::Base64 |
| 339 | .SH "AUTHOR" |
| 340 | .IX Header "AUTHOR" |
| 341 | Gisle Aas <gisle@aas.no> |
| 342 | .PP |
| 343 | The \f(CW\*(C`Digest::\*(C'\fR interface is based on the interface originally |
| 344 | developed by Neil Winton for his \f(CW\*(C`MD5\*(C'\fR module. |
| 345 | .PP |
| 346 | This library is free software; you can redistribute it and/or |
| 347 | modify it under the same terms as Perl itself. |
| 348 | .PP |
| 349 | .Vb 2 |
| 350 | \& Copyright 1998-2001,2003-2004 Gisle Aas. |
| 351 | \& Copyright 1995-1996 Neil Winton. |
| 352 | .Ve |