4 use vars qw($VERSION %MMAP $AUTOLOAD);
9 "SHA-1" => "Digest::SHA1",
10 "HMAC-MD5" => "Digest::HMAC_MD5",
11 "HMAC-SHA-1" => "Digest::HMAC_SHA1",
16 shift; # class ignored
17 my $algorithm = shift;
18 my $class = $MMAP{$algorithm} || "Digest::$algorithm";
20 unless (exists ${"$class\::"}{"VERSION"}) {
21 eval "require $class";
30 my $algorithm = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2);
31 $class->new($algorithm, @_);
40 Digest:: - Modules that calculate message digests
48 $sha1 = Digest->new("SHA-1");
50 $hmac = Digest->HMAC_MD5($key);
54 The C<Digest::> modules calculate digests, also called "fingerprints"
55 or "hashes", of some data, called a message. The digest is (usually)
56 some small/fixed size string. The actual size of the digest depend of
57 the algorithm used. The message is simply a sequence of arbitrary
60 An important property of the digest algorithms is that the digest is
61 I<likely> to change if the message change in some way. Another
62 property is that digest functions are one-way functions, i.e. it
63 should be I<hard> to find a message that correspond to some given
64 digest. Algorithms differ in how "likely" and how "hard", as well as
65 how efficient they are to compute.
67 All C<Digest::> modules provide the same programming interface. A
68 functional interface for simple use, as well as an object oriented
69 interface that can handle messages of arbitrary length and which can
72 The digest can be delivered in three formats:
78 This is the most compact form, but it is not well suited for printing
79 or embedding in places that can't handle arbitrary data.
83 A twice as long string of (lowercase) hexadecimal digits.
87 A string of portable printable characters. This is the base64 encoded
88 representation of the digest with any trailing padding removed. The
89 string will be about 30% longer than the binary version.
90 L<MIME::Base64> tells you more about this encoding.
95 The functional interface is simply importable functions with the same
96 name as the algorithm. The functions take the message as argument and
97 return the digest. Example:
99 use Digest::MD5 qw(md5);
100 $digest = md5($message);
102 There are also versions of the functions with "_hex" or "_base64"
103 appended to the name, which returns the digest in the indicated form.
107 The following methods are available for all C<Digest::> modules:
111 =item $ctx = Digest->XXX($arg,...)
113 =item $ctx = Digest->new(XXX => $arg,...)
115 =item $ctx = Digest::XXX->new($arg,...)
117 The constructor returns some object that encapsulate the state of the
118 message-digest algorithm. You can add data to the object and finally
119 ask for the digest. The "XXX" should of course be replaced by the proper
120 name of the digest algorithm you want to use.
122 The two first forms are simply syntactic sugar which automatically
123 load the right module on first use. The second form allow you to use
124 algorithm names which contains letters which are not legal perl
125 identifiers, e.g. "SHA-1".
127 If new() is called as an instance method (i.e. $ctx->new) it will just
128 reset the state the object to the state of a newly created object. No
129 new object is created in this case, and the return value is the
130 reference to the object (i.e. $ctx).
134 This is just an alias for $ctx->new.
136 =item $ctx->add($data,...)
138 The $data provided as argument are appended to the message we
139 calculate the digest for. The return value is the $ctx object itself.
141 =item $ctx->addfile($io_handle)
143 The $io_handle is read until EOF and the content is appended to the
144 message we calculate the digest for. The return value is the $ctx
149 Return the binary digest for the message.
151 Note that the C<digest> operation is effectively a destructive,
152 read-once operation. Once it has been performed, the $ctx object is
153 automatically C<reset> and can be used to calculate another digest
156 =item $ctx->hexdigest
158 Same as $ctx->digest, but will return the digest in hexadecimal form.
160 =item $ctx->b64digest
162 Same as $ctx->digest, but will return the digest as a base64 encoded
169 L<Digest::MD5>, L<Digest::SHA1>, L<Digest::HMAC>, L<Digest::MD2>
175 Gisle Aas <gisle@aas.no>
177 The C<Digest::> interface is based on the interface originally
178 developed by Neil Winton for his C<MD5> module.