1Digest(3pm)            Perl Programmers Reference Guide            Digest(3pm)
2
3
4

NAME

6       Digest - Modules that calculate message digests
7

SYNOPSIS

9         $md5  = Digest->new("MD5");
10         $sha1 = Digest->new("SHA-1");
11         $sha256 = Digest->new("SHA-256");
12         $sha384 = Digest->new("SHA-384");
13         $sha512 = Digest->new("SHA-512");
14
15         $hmac = Digest->HMAC_MD5($key);
16

DESCRIPTION

18       The "Digest::" modules calculate digests, also called "fingerprints" or
19       "hashes", of some data, called a message.  The digest is (usually) some
20       small/fixed size string.  The actual size of the digest depend of the
21       algorithm used.  The message is simply a sequence of arbitrary bytes or
22       bits.
23
24       An important property of the digest algorithms is that the digest is
25       likely to change if the message change in some way.  Another property
26       is that digest functions are one-way functions, that is it should be
27       hard to find a message that correspond to some given digest.  Algo‐
28       rithms differ in how "likely" and how "hard", as well as how efficient
29       they are to compute.
30
31       Note that the properties of the algorithms change over time, as the
32       algorithms are analyzed and machines grow faster.  If your application
33       for instance depends on it being "impossible" to generate the same
34       digest for a different message it is wise to make it easy to plug in
35       stronger algorithms as the one used grow weaker.  Using the interface
36       documented here should make it easy to change algorithms later.
37
38       All "Digest::" modules provide the same programming interface.  A func‐
39       tional interface for simple use, as well as an object oriented inter‐
40       face that can handle messages of arbitrary length and which can read
41       files directly.
42
43       The digest can be delivered in three formats:
44
45       binary  This is the most compact form, but it is not well suited for
46               printing or embedding in places that can't handle arbitrary
47               data.
48
49       hex     A twice as long string of lowercase hexadecimal digits.
50
51       base64  A string of portable printable characters.  This is the base64
52               encoded representation of the digest with any trailing padding
53               removed.  The string will be about 30% longer than the binary
54               version.  MIME::Base64 tells you more about this encoding.
55
56       The functional interface is simply importable functions with the same
57       name as the algorithm.  The functions take the message as argument and
58       return the digest.  Example:
59
60         use Digest::MD5 qw(md5);
61         $digest = md5($message);
62
63       There are also versions of the functions with "_hex" or "_base64"
64       appended to the name, which returns the digest in the indicated form.
65

OO INTERFACE

67       The following methods are available for all "Digest::" modules:
68
69       $ctx = Digest->XXX($arg,...)
70       $ctx = Digest->new(XXX => $arg,...)
71       $ctx = Digest::XXX->new($arg,...)
72           The constructor returns some object that encapsulate the state of
73           the message-digest algorithm.  You can add data to the object and
74           finally ask for the digest.  The "XXX" should of course be replaced
75           by the proper name of the digest algorithm you want to use.
76
77           The two first forms are simply syntactic sugar which automatically
78           load the right module on first use.  The second form allow you to
79           use algorithm names which contains letters which are not legal perl
80           identifiers, e.g. "SHA-1".  If no implementation for the given
81           algorithm can be found, then an exception is raised.
82
83           If new() is called as an instance method (i.e. $ctx->new) it will
84           just reset the state the object to the state of a newly created
85           object.  No new object is created in this case, and the return
86           value is the reference to the object (i.e. $ctx).
87
88       $other_ctx = $ctx->clone
89           The clone method creates a copy of the digest state object and
90           returns a reference to the copy.
91
92       $ctx->reset
93           This is just an alias for $ctx->new.
94
95       $ctx->add( $data, ... )
96           The $data provided as argument are appended to the message we cal‐
97           culate the digest for.  The return value is the $ctx object itself.
98
99       $ctx->addfile( $io_handle )
100           The $io_handle is read until EOF and the content is appended to the
101           message we calculate the digest for.  The return value is the $ctx
102           object itself.
103
104       $ctx->add_bits( $data, $nbits )
105       $ctx->add_bits( $bitstring )
106           The bits provided are appended to the message we calculate the
107           digest for.  The return value is the $ctx object itself.
108
109           The two argument form of add_bits() will add the first $nbits bits
110           from data.  For the last potentially partial byte only the high
111           order "$nbits % 8" bits are used.  If $nbits is greater than
112           "length($data) * 8", then this method would do the same as
113           "$ctx->add($data)", that is $nbits is silently ignored.
114
115           The one argument form of add_bits() takes a $bitstring of "1" and
116           "0" chars as argument.  It's a shorthand for
117           "$ctx->add_bits(pack("B*", $bitstring), length($bitstring))".
118
119           This example shows two calls that should have the same effect:
120
121              $ctx->add_bits("111100001010");
122              $ctx->add_bits("\xF0\xA0", 12);
123
124           Most digest algorithms are byte based.  For those it is not possi‐
125           ble to add bits that are not a multiple of 8, and the add_bits()
126           method will croak if you try.
127
128       $ctx->digest
129           Return the binary digest for the message.
130
131           Note that the "digest" operation is effectively a destructive,
132           read-once operation. Once it has been performed, the $ctx object is
133           automatically "reset" and can be used to calculate another digest
134           value.  Call $ctx->clone->digest if you want to calculate the
135           digest without reseting the digest state.
136
137       $ctx->hexdigest
138           Same as $ctx->digest, but will return the digest in hexadecimal
139           form.
140
141       $ctx->b64digest
142           Same as $ctx->digest, but will return the digest as a base64
143           encoded string.
144

Digest speed

146       This table should give some indication on the relative speed of differ‐
147       ent algorithms.  It is sorted by throughput based on a benchmark done
148       with of some implementations of this API:
149
150        Algorithm      Size    Implementation                  MB/s
151
152        MD4            128     Digest::MD4 v1.3               165.0
153        MD5            128     Digest::MD5 v2.33               98.8
154        SHA-256        256     Digest::SHA2 v1.1.0             66.7
155        SHA-1          160     Digest::SHA v4.3.1              58.9
156        SHA-1          160     Digest::SHA1 v2.10              48.8
157        SHA-256        256     Digest::SHA v4.3.1              41.3
158        Haval-256      256     Digest::Haval256 v1.0.4         39.8
159        SHA-384        384     Digest::SHA2 v1.1.0             19.6
160        SHA-512        512     Digest::SHA2 v1.1.0             19.3
161        SHA-384        384     Digest::SHA v4.3.1              19.2
162        SHA-512        512     Digest::SHA v4.3.1              19.2
163        Whirlpool      512     Digest::Whirlpool v1.0.2        13.0
164        MD2            128     Digest::MD2 v2.03                9.5
165
166        Adler-32        32     Digest::Adler32 v0.03            1.3
167        CRC-16          16     Digest::CRC v0.05                1.1
168        CRC-32          32     Digest::CRC v0.05                1.1
169        MD5            128     Digest::Perl::MD5 v1.5           1.0
170        CRC-CCITT       16     Digest::CRC v0.05                0.8
171
172       These numbers was achieved Apr 2004 with ActivePerl-5.8.3 running under
173       Linux on a P4 2.8 GHz CPU.  The last 5 entries differ by being pure
174       perl implementations of the algorithms, which explains why they are so
175       slow.
176

SEE ALSO

178       Digest::Adler32, Digest::CRC, Digest::Haval256, Digest::HMAC,
179       Digest::MD2, Digest::MD4, Digest::MD5, Digest::SHA, Digest::SHA1,
180       Digest::SHA2, Digest::Whirlpool
181
182       New digest implementations should consider subclassing from
183       Digest::base.
184
185       MIME::Base64
186

AUTHOR

188       Gisle Aas <gisle@aas.no>
189
190       The "Digest::" interface is based on the interface originally developed
191       by Neil Winton for his "MD5" module.
192
193       This library is free software; you can redistribute it and/or modify it
194       under the same terms as Perl itself.
195
196           Copyright 1998-2001,2003-2004 Gisle Aas.
197           Copyright 1995-1996 Neil Winton.
198
199
200
201perl v5.8.8                       2001-09-21                       Digest(3pm)
Impressum