Blame SHA1.pm

Packit f92210
package Digest::SHA1;
Packit f92210
Packit f92210
use strict;
Packit f92210
use vars qw($VERSION @ISA @EXPORT_OK);
Packit f92210
Packit f92210
$VERSION = '2.13';
Packit f92210
Packit f92210
require Exporter;
Packit f92210
*import = \&Exporter::import;
Packit f92210
@EXPORT_OK = qw(sha1 sha1_hex sha1_base64 sha1_transform);
Packit f92210
Packit f92210
require DynaLoader;
Packit f92210
@ISA=qw(DynaLoader);
Packit f92210
Packit f92210
eval {
Packit f92210
    require Digest::base;
Packit f92210
    push(@ISA, 'Digest::base');
Packit f92210
};
Packit f92210
if ($@) {
Packit f92210
    my $err = $@;
Packit f92210
    *add_bits = sub { die $err };
Packit f92210
}
Packit f92210
Packit f92210
Digest::SHA1->bootstrap($VERSION);
Packit f92210
Packit f92210
1;
Packit f92210
__END__
Packit f92210
Packit f92210
=head1 NAME
Packit f92210
Packit f92210
Digest::SHA1 - Perl interface to the SHA-1 algorithm
Packit f92210
Packit f92210
=head1 SYNOPSIS
Packit f92210
Packit f92210
 # Functional style
Packit f92210
 use Digest::SHA1  qw(sha1 sha1_hex sha1_base64);
Packit f92210
Packit f92210
 $digest = sha1($data);
Packit f92210
 $digest = sha1_hex($data);
Packit f92210
 $digest = sha1_base64($data);
Packit f92210
 $digest = sha1_transform($data);
Packit f92210
Packit f92210
Packit f92210
 # OO style
Packit f92210
 use Digest::SHA1;
Packit f92210
Packit f92210
 $sha1 = Digest::SHA1->new;
Packit f92210
Packit f92210
 $sha1->add($data);
Packit f92210
 $sha1->addfile(*FILE);
Packit f92210
Packit f92210
 $sha1_copy = $sha1->clone;
Packit f92210
Packit f92210
 $digest = $sha1->digest;
Packit f92210
 $digest = $sha1->hexdigest;
Packit f92210
 $digest = $sha1->b64digest;
Packit f92210
 $digest = $sha1->transform;
Packit f92210
Packit f92210
=head1 DESCRIPTION
Packit f92210
Packit f92210
The C<Digest::SHA1> module allows you to use the NIST SHA-1 message
Packit f92210
digest algorithm from within Perl programs.  The algorithm takes as
Packit f92210
input a message of arbitrary length and produces as output a 160-bit
Packit f92210
"fingerprint" or "message digest" of the input.
Packit f92210
Packit f92210
In 2005, security flaws were identified in SHA-1, namely that a possible
Packit f92210
mathematical weakness might exist, indicating that a stronger hash function
Packit f92210
would be desirable.  The L<Digest::SHA> module implements the stronger
Packit f92210
algorithms in the SHA family.
Packit f92210
Packit f92210
The C<Digest::SHA1> module provide a procedural interface for simple
Packit f92210
use, as well as an object oriented interface that can handle messages
Packit f92210
of arbitrary length and which can read files directly.
Packit f92210
Packit f92210
=head1 FUNCTIONS
Packit f92210
Packit f92210
The following functions can be exported from the C<Digest::SHA1>
Packit f92210
module.  No functions are exported by default.
Packit f92210
Packit f92210
=over 4
Packit f92210
Packit f92210
=item sha1($data,...)
Packit f92210
Packit f92210
This function will concatenate all arguments, calculate the SHA-1
Packit f92210
digest of this "message", and return it in binary form.  The returned
Packit f92210
string will be 20 bytes long.
Packit f92210
Packit f92210
The result of sha1("a", "b", "c") will be exactly the same as the
Packit f92210
result of sha1("abc").
Packit f92210
Packit f92210
=item sha1_hex($data,...)
Packit f92210
Packit f92210
Same as sha1(), but will return the digest in hexadecimal form.  The
Packit f92210
length of the returned string will be 40 and it will only contain
Packit f92210
characters from this set: '0'..'9' and 'a'..'f'.
Packit f92210
Packit f92210
=item sha1_base64($data,...)
Packit f92210
Packit f92210
Same as sha1(), but will return the digest as a base64 encoded string.
Packit f92210
The length of the returned string will be 27 and it will only contain
Packit f92210
characters from this set: 'A'..'Z', 'a'..'z', '0'..'9', '+' and
Packit f92210
'/'.
Packit f92210
Packit f92210
Note that the base64 encoded string returned is not padded to be a
Packit f92210
multiple of 4 bytes long.  If you want interoperability with other
Packit f92210
base64 encoded sha1 digests you might want to append the redundant
Packit f92210
string "=" to the result.
Packit f92210
Packit f92210
=item sha1_transform($data)
Packit f92210
Packit f92210
Implements the basic SHA1 transform on a 64 byte block. The $data
Packit f92210
argument and the returned $digest are in binary form. This algorithm
Packit f92210
is used in NIST FIPS 186-2
Packit f92210
Packit f92210
=back
Packit f92210
Packit f92210
=head1 METHODS
Packit f92210
Packit f92210
The object oriented interface to C<Digest::SHA1> is described in this
Packit f92210
section.  After a C<Digest::SHA1> object has been created, you will add
Packit f92210
data to it and finally ask for the digest in a suitable format.  A
Packit f92210
single object can be used to calculate multiple digests.
Packit f92210
Packit f92210
The following methods are provided:
Packit f92210
Packit f92210
=over 4
Packit f92210
Packit f92210
=item $sha1 = Digest::SHA1->new
Packit f92210
Packit f92210
The constructor returns a new C<Digest::SHA1> object which encapsulate
Packit f92210
the state of the SHA-1 message-digest algorithm.
Packit f92210
Packit f92210
If called as an instance method (i.e. $sha1->new) it will just reset the
Packit f92210
state the object to the state of a newly created object.  No new
Packit f92210
object is created in this case.
Packit f92210
Packit f92210
=item $sha1->reset
Packit f92210
Packit f92210
This is just an alias for $sha1->new.
Packit f92210
Packit f92210
=item $sha1->clone
Packit f92210
Packit f92210
This a copy of the $sha1 object. It is useful when you do not want to
Packit f92210
destroy the digests state, but need an intermediate value of the
Packit f92210
digest, e.g. when calculating digests iteratively on a continuous data
Packit f92210
stream.  Example:
Packit f92210
Packit f92210
    my $sha1 = Digest::SHA1->new;
Packit f92210
    while (<>) {
Packit f92210
	$sha1->add($_);
Packit f92210
	print "Line $.: ", $sha1->clone->hexdigest, "\n";
Packit f92210
    }
Packit f92210
Packit f92210
=item $sha1->add($data,...)
Packit f92210
Packit f92210
The $data provided as argument are appended to the message we
Packit f92210
calculate the digest for.  The return value is the $sha1 object itself.
Packit f92210
Packit f92210
All these lines will have the same effect on the state of the $sha1
Packit f92210
object:
Packit f92210
Packit f92210
    $sha1->add("a"); $sha1->add("b"); $sha1->add("c");
Packit f92210
    $sha1->add("a")->add("b")->add("c");
Packit f92210
    $sha1->add("a", "b", "c");
Packit f92210
    $sha1->add("abc");
Packit f92210
Packit f92210
=item $sha1->addfile($io_handle)
Packit f92210
Packit f92210
The $io_handle will be read until EOF and its content appended to the
Packit f92210
message we calculate the digest for.  The return value is the $sha1
Packit f92210
object itself.
Packit f92210
Packit f92210
The addfile() method will croak() if it fails reading data for some
Packit f92210
reason.  If it croaks it is unpredictable what the state of the $sha1
Packit f92210
object will be in. The addfile() method might have been able to read
Packit f92210
the file partially before it failed.  It is probably wise to discard
Packit f92210
or reset the $sha1 object if this occurs.
Packit f92210
Packit f92210
In most cases you want to make sure that the $io_handle is in
Packit f92210
C<binmode> before you pass it as argument to the addfile() method.
Packit f92210
Packit f92210
=item $sha1->add_bits($data, $nbits)
Packit f92210
Packit f92210
=item $sha1->add_bits($bitstring)
Packit f92210
Packit f92210
This implementation of SHA-1 only supports byte oriented input so you
Packit f92210
might only add bits as multiples of 8.  If you need bit level support
Packit f92210
please consider using the C<Digest::SHA> module instead.  The
Packit f92210
add_bits() method is provided here for compatibility with other digest
Packit f92210
implementations.  See L<Digest> for description of the arguments that
Packit f92210
add_bits() take.
Packit f92210
Packit f92210
=item $sha1->digest
Packit f92210
Packit f92210
Return the binary digest for the message.  The returned string will be
Packit f92210
20 bytes long.
Packit f92210
Packit f92210
Note that the C<digest> operation is effectively a destructive,
Packit f92210
read-once operation. Once it has been performed, the C<Digest::SHA1>
Packit f92210
object is automatically C<reset> and can be used to calculate another
Packit f92210
digest value.  Call $sha1->clone->digest if you want to calculate the
Packit f92210
digest without reseting the digest state.
Packit f92210
Packit f92210
=item $sha1->hexdigest
Packit f92210
Packit f92210
Same as $sha1->digest, but will return the digest in hexadecimal
Packit f92210
form. The length of the returned string will be 40 and it will only
Packit f92210
contain characters from this set: '0'..'9' and 'a'..'f'.
Packit f92210
Packit f92210
=item $sha1->b64digest
Packit f92210
Packit f92210
Same as $sha1->digest, but will return the digest as a base64 encoded
Packit f92210
string.  The length of the returned string will be 27 and it will only
Packit f92210
contain characters from this set: 'A'..'Z', 'a'..'z', '0'..'9', '+'
Packit f92210
and '/'.
Packit f92210
Packit f92210
Packit f92210
The base64 encoded string returned is not padded to be a multiple of 4
Packit f92210
bytes long.  If you want interoperability with other base64 encoded
Packit f92210
SHA-1 digests you might want to append the string "=" to the result.
Packit f92210
Packit f92210
=back
Packit f92210
Packit f92210
=head1 SEE ALSO
Packit f92210
Packit f92210
L<Digest>, L<Digest::HMAC_SHA1>, L<Digest::SHA>, L<Digest::MD5>
Packit f92210
Packit f92210
http://www.itl.nist.gov/fipspubs/fip180-1.htm
Packit f92210
Packit f92210
http://en.wikipedia.org/wiki/SHA_hash_functions
Packit f92210
Packit f92210
=head1 COPYRIGHT
Packit f92210
Packit f92210
This library is free software; you can redistribute it and/or
Packit f92210
modify it under the same terms as Perl itself.
Packit f92210
Packit f92210
 Copyright 1999-2004 Gisle Aas.
Packit f92210
 Copyright 1997 Uwe Hollerbach.
Packit f92210
Packit f92210
=head1 AUTHORS
Packit f92210
Packit f92210
Peter C. Gutmann,
Packit f92210
Uwe Hollerbach <uh@alumni.caltech.edu>,
Packit f92210
Gisle Aas <gisle@aas.no>
Packit f92210
Packit f92210
=cut