1Digest(3)             User Contributed Perl Documentation            Digest(3)
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.
28       Algorithms differ in how "likely" and how "hard", as well as how
29       efficient 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
39       functional interface for simple use, as well as an object oriented
40       interface that can handle messages of arbitrary length and which can
41       read 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       $ctx->add( $chunk1, $chunk2, ... )
97           The string value of the $data provided as argument is appended to
98           the message we calculate the digest for.  The return value is the
99           $ctx object itself.
100
101           If more arguments are provided then they are all appended to the
102           message, thus all these lines will have the same effect on the
103           state of the $ctx object:
104
105             $ctx->add("a"); $ctx->add("b"); $ctx->add("c");
106             $ctx->add("a")->add("b")->add("c");
107             $ctx->add("a", "b", "c");
108             $ctx->add("abc");
109
110           Most algorithms are only defined for strings of bytes and this
111           method might therefore croak if the provided arguments contain
112           chars with ordinal number above 255.
113
114       $ctx->addfile( $io_handle )
115           The $io_handle is read until EOF and the content is appended to the
116           message we calculate the digest for.  The return value is the $ctx
117           object itself.
118
119           The addfile() method will croak() if it fails reading data for some
120           reason.  If it croaks it is unpredictable what the state of the
121           $ctx object will be in. The addfile() method might have been able
122           to read the file partially before it failed.  It is probably wise
123           to discard or reset the $ctx object if this occurs.
124
125           In most cases you want to make sure that the $io_handle is in
126           "binmode" before you pass it as argument to the addfile() method.
127
128       $ctx->add_bits( $data, $nbits )
129       $ctx->add_bits( $bitstring )
130           The add_bits() method is an alternative to add() that allow partial
131           bytes to be appended to the message.  Most users should just ignore
132           this method as partial bytes is very unlikely to be of any
133           practical use.
134
135           The two argument form of add_bits() will add the first $nbits bits
136           from $data.  For the last potentially partial byte only the high
137           order "$nbits % 8" bits are used.  If $nbits is greater than
138           "length($data) * 8", then this method would do the same as
139           "$ctx->add($data)".
140
141           The one argument form of add_bits() takes a $bitstring of "1" and
142           "0" chars as argument.  It's a shorthand for
143           "$ctx->add_bits(pack("B*", $bitstring), length($bitstring))".
144
145           The return value is the $ctx object itself.
146
147           This example shows two calls that should have the same effect:
148
149              $ctx->add_bits("111100001010");
150              $ctx->add_bits("\xF0\xA0", 12);
151
152           Most digest algorithms are byte based and for these it is not
153           possible to add bits that are not a multiple of 8, and the
154           add_bits() method will croak if you try.
155
156       $ctx->digest
157           Return the binary digest for the message.
158
159           Note that the "digest" operation is effectively a destructive,
160           read-once operation. Once it has been performed, the $ctx object is
161           automatically "reset" and can be used to calculate another digest
162           value.  Call $ctx->clone->digest if you want to calculate the
163           digest without resetting the digest state.
164
165       $ctx->hexdigest
166           Same as $ctx->digest, but will return the digest in hexadecimal
167           form.
168
169       $ctx->b64digest
170           Same as $ctx->digest, but will return the digest as a base64
171           encoded string.
172

Digest speed

174       This table should give some indication on the relative speed of
175       different algorithms.  It is sorted by throughput based on a benchmark
176       done with of some implementations of this API:
177
178        Algorithm      Size    Implementation                  MB/s
179
180        MD4            128     Digest::MD4 v1.3               165.0
181        MD5            128     Digest::MD5 v2.33               98.8
182        SHA-256        256     Digest::SHA2 v1.1.0             66.7
183        SHA-1          160     Digest::SHA v4.3.1              58.9
184        SHA-1          160     Digest::SHA1 v2.10              48.8
185        SHA-256        256     Digest::SHA v4.3.1              41.3
186        Haval-256      256     Digest::Haval256 v1.0.4         39.8
187        SHA-384        384     Digest::SHA2 v1.1.0             19.6
188        SHA-512        512     Digest::SHA2 v1.1.0             19.3
189        SHA-384        384     Digest::SHA v4.3.1              19.2
190        SHA-512        512     Digest::SHA v4.3.1              19.2
191        Whirlpool      512     Digest::Whirlpool v1.0.2        13.0
192        MD2            128     Digest::MD2 v2.03                9.5
193
194        Adler-32        32     Digest::Adler32 v0.03            1.3
195        CRC-16          16     Digest::CRC v0.05                1.1
196        CRC-32          32     Digest::CRC v0.05                1.1
197        MD5            128     Digest::Perl::MD5 v1.5           1.0
198        CRC-CCITT       16     Digest::CRC v0.05                0.8
199
200       These numbers was achieved Apr 2004 with ActivePerl-5.8.3 running under
201       Linux on a P4 2.8 GHz CPU.  The last 5 entries differ by being pure
202       perl implementations of the algorithms, which explains why they are so
203       slow.
204

SEE ALSO

206       Digest::Adler32, Digest::CRC, Digest::Haval256, Digest::HMAC,
207       Digest::MD2, Digest::MD4, Digest::MD5, Digest::SHA, Digest::SHA1,
208       Digest::SHA2, Digest::Whirlpool
209
210       New digest implementations should consider subclassing from
211       Digest::base.
212
213       MIME::Base64
214
215       http://en.wikipedia.org/wiki/Cryptographic_hash_function
216

AUTHOR

218       Gisle Aas <gisle@aas.no>
219
220       The "Digest::" interface is based on the interface originally developed
221       by Neil Winton for his "MD5" module.
222
223       This library is free software; you can redistribute it and/or modify it
224       under the same terms as Perl itself.
225
226           Copyright 1998-2006 Gisle Aas.
227           Copyright 1995,1996 Neil Winton.
228
229
230
231perl v5.30.1                      2020-01-29                         Digest(3)
Impressum