Digest::SHA1 - Perl interface to the SHA-1 algorithm |
Digest::SHA1 - Perl interface to the SHA-1 algorithm
# Functional style use Digest::SHA1 qw(sha1 sha1_hex sha1_base64);
$digest = sha1($data); $digest = sha1_hex($data); $digest = sha1_base64($data); $digest = sha1_transform($data);
# OO style use Digest::SHA1;
$sha1 = Digest::SHA1->new;
$sha1->add($data); $sha1->addfile(*FILE);
$sha1_copy = $sha1->clone;
$digest = $sha1->digest; $digest = $sha1->hexdigest; $digest = $sha1->b64digest; $digest = $sha1->transform;
The Digest::SHA1
module allows you to use the NIST SHA-1 message
digest algorithm from within Perl programs. The algorithm takes as
input a message of arbitrary length and produces as output a 160-bit
``fingerprint'' or ``message digest'' of the input.
The Digest::SHA1
module provide a procedural interface for simple
use, as well as an object oriented interface that can handle messages
of arbitrary length and which can read files directly.
The following functions can be exported from the Digest::SHA1
module. No functions are exported by default.
sha1($data,...)
The result of sha1(``a'', ``b'', ``c'') will be exactly the same as the result of sha1(``abc'').
sha1_hex($data,...)
sha1_base64($data,...)
Note that the base64 encoded string returned is not padded to be a multiple of 4 bytes long. If you want interoperability with other base64 encoded sha1 digests you might want to append the redundant string ``='' to the result.
sha1_transform($data)
The object oriented interface to Digest::SHA1
is described in this
section. After a Digest::SHA1
object has been created, you will add
data to it and finally ask for the digest in a suitable format. A
single object can be used to calculate multiple digests.
The following methods are provided:
Digest::SHA1
object which encapsulate
the state of the SHA-1 message-digest algorithm.
If called as an instance method (i.e. $sha1->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.
my $sha1 = Digest::SHA1->new; while (<>) { $sha1->add($_); print "Line $.: ", $sha1->clone->hexdigest, "\n"; }
add($data,...)
All these lines will have the same effect on the state of the $sha1 object:
$sha1->add("a"); $sha1->add("b"); $sha1->add("c"); $sha1->add("a")->add("b")->add("c"); $sha1->add("a", "b", "c"); $sha1->add("abc");
addfile($io_handle)
The addfile()
method will croak()
if it fails reading data for some
reason. If it croaks it is unpredictable what the state of the $sha1
object will be in. The addfile()
method might have been able to read
the file partially before it failed. It is probably wise to discard
or reset the $sha1 object if this occurs.
In most cases you want to make sure that the $io_handle is in
binmode
before you pass it as argument to the addfile()
method.
add_bits($bitstring)
Digest::SHA
module instead. The
add_bits()
method is provided here for compatibility with other digest
implementations. See the Digest manpage for description of the arguments that
add_bits()
take.
Note that the digest
operation is effectively a destructive,
read-once operation. Once it has been performed, the Digest::SHA1
object is automatically reset
and can be used to calculate another
digest value. Call $sha1->clone->digest if you want to calculate the
digest without reseting the digest state.
The base64 encoded string returned is not padded to be a multiple of 4 bytes long. If you want interoperability with other base64 encoded SHA-1 digests you might want to append the string ``='' to the result.
the Digest manpage, the Digest::HMAC_SHA1 manpage, the Digest::SHA manpage, the Digest::MD5 manpage
http://www.itl.nist.gov/fipspubs/fip180-1.htm
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Copyright 1999-2004 Gisle Aas. Copyright 1997 Uwe Hollerbach.
Peter C. Gutmann, Uwe Hollerbach <uh@alumni.caltech.edu>, Gisle Aas <gisle@aas.no>
Digest::SHA1 - Perl interface to the SHA-1 algorithm |