The Digest:: modules calculate digests, also called ``fingerprints''
or ``hashes'', of some data, called a message. The digest is (usually)
some small/fixed size string. The actual size of the digest depend of
the algorithm used. The message is simply a sequence of arbitrary bytes or bits.
An important property of the digest algorithms is that the digest is
likely to change if the message change in some way. Another
property is that digest functions are one-way functions, that is it
should be hard to find a message that correspond to some given
digest. Algorithms differ in how ``likely'' and how ``hard'', as well as
how efficient they are to compute.
Note that the properties of the algorithms change over time, as the
algorithms are analyzed and machines grow faster. If your application
for instance depends on it being ``impossible'' to generate the same
digest for a different message it is wise to make it easy to plug in
stronger algorithms as the one used grow weaker. Using the interface
documented here should make it easy to change algorithms later.
All Digest:: modules provide the same programming interface. A
functional interface for simple use, as well as an object oriented
interface that can handle messages of arbitrary length and which can
read files directly.
A string of portable printable characters. This is the base64 encoded
representation of the digest with any trailing padding removed. The
string will be about 30% longer than the binary version.
the MIME::Base64 manpage tells you more about this encoding.
The functional interface is simply importable functions with the same
name as the algorithm. The functions take the message as argument and
return the digest. Example:
The constructor returns some object that encapsulate the state of the
message-digest algorithm. You can add data to the object and finally
ask for the digest. The ``XXX'' should of course be replaced by the proper
name of the digest algorithm you want to use.
The two first forms are simply syntactic sugar which automatically
load the right module on first use. The second form allow you to use
algorithm names which contains letters which are not legal perl
identifiers, e.g. ``SHA-1''. If no implementation for the given algorithm
can be found, then an exception is raised.
If new() is called as an instance method (i.e. $ctx->new) it will just
reset the state the object to the state of a newly created object. No
new object is created in this case, and the return value is the
reference to the object (i.e. $ctx).
The bits provided are appended to the message we calculate the digest
for. The return value is the $ctx object itself.
The two argument form of add_bits() will add the first $nbits bits
from data. For the last potentially partial byte only the high order
$nbits % 8 bits are used. If $nbits is greater than <
length($data) * 8 >, then this method would do the same as <
$ctx-add($data) >>, that is $nbits is silently ignored.
Most digest algorithms are byte based. For those it is not possible
to add bits that are not a multiple of 8, and the add_bits() method
will croak if you try.
Note that the digest operation is effectively a destructive,
read-once operation. Once it has been performed, the $ctx object is
automatically reset and can be used to calculate another digest
value. Call $ctx->clone->digest if you want to calculate the digest
without reseting the digest state.
This table should give some indication on the relative speed of
different algorithms. It is sorted by throughput based on a benchmark
done with of some implementations of this API:
These numbers was achieved Apr 2004 with ActivePerl-5.8.3 running
under Linux on a P4 2.8 GHz CPU. The last 5 entries differ by being
pure perl implementations of the algorithms, which explains why they
are so slow.