1SHA1(3) User Contributed Perl Documentation SHA1(3)
2
3
4
6 Digest::SHA1 - Perl interface to the SHA-1 algorithm
7
9 # Functional style
10 use Digest::SHA1 qw(sha1 sha1_hex sha1_base64);
11
12 $digest = sha1($data);
13 $digest = sha1_hex($data);
14 $digest = sha1_base64($data);
15 $digest = sha1_transform($data);
16
17 # OO style
18 use Digest::SHA1;
19
20 $sha1 = Digest::SHA1->new;
21
22 $sha1->add($data);
23 $sha1->addfile(*FILE);
24
25 $sha1_copy = $sha1->clone;
26
27 $digest = $sha1->digest;
28 $digest = $sha1->hexdigest;
29 $digest = $sha1->b64digest;
30 $digest = $sha1->transform;
31
33 The "Digest::SHA1" module allows you to use the NIST SHA-1 message
34 digest algorithm from within Perl programs. The algorithm takes as
35 input a message of arbitrary length and produces as output a 160-bit
36 "fingerprint" or "message digest" of the input.
37
38 The "Digest::SHA1" module provide a procedural interface for simple
39 use, as well as an object oriented interface that can handle messages
40 of arbitrary length and which can read files directly.
41
43 The following functions can be exported from the "Digest::SHA1" module.
44 No functions are exported by default.
45
46 sha1($data,...)
47 This function will concatenate all arguments, calculate the SHA-1
48 digest of this "message", and return it in binary form. The
49 returned string will be 20 bytes long.
50
51 The result of sha1("a", "b", "c") will be exactly the same as the
52 result of sha1("abc").
53
54 sha1_hex($data,...)
55 Same as sha1(), but will return the digest in hexadecimal form.
56 The length of the returned string will be 40 and it will only con‐
57 tain characters from this set: '0'..'9' and 'a'..'f'.
58
59 sha1_base64($data,...)
60 Same as sha1(), but will return the digest as a base64 encoded
61 string. The length of the returned string will be 27 and it will
62 only contain characters from this set: 'A'..'Z', 'a'..'z',
63 '0'..'9', '+' and '/'.
64
65 Note that the base64 encoded string returned is not padded to be a
66 multiple of 4 bytes long. If you want interoperability with other
67 base64 encoded sha1 digests you might want to append the redundant
68 string "=" to the result.
69
70 sha1_transform($data)
71 Implements the basic SHA1 transform on a 64 byte block. The $data
72 argument and the returned $digest are in binary form. This algo‐
73 rithm is used in NIST FIPS 186-2
74
76 The object oriented interface to "Digest::SHA1" is described in this
77 section. After a "Digest::SHA1" object has been created, you will add
78 data to it and finally ask for the digest in a suitable format. A sin‐
79 gle object can be used to calculate multiple digests.
80
81 The following methods are provided:
82
83 $sha1 = Digest::SHA1->new
84 The constructor returns a new "Digest::SHA1" object which encapsu‐
85 late the state of the SHA-1 message-digest algorithm.
86
87 If called as an instance method (i.e. $sha1->new) it will just
88 reset the state the object to the state of a newly created object.
89 No new object is created in this case.
90
91 $sha1->reset
92 This is just an alias for $sha1->new.
93
94 $sha1->clone
95 This a copy of the $sha1 object. It is useful when you do not want
96 to destroy the digests state, but need an intermediate value of the
97 digest, e.g. when calculating digests iteratively on a continuous
98 data stream. Example:
99
100 my $sha1 = Digest::SHA1->new;
101 while (<>) {
102 $sha1->add($_);
103 print "Line $.: ", $sha1->clone->hexdigest, "\n";
104 }
105
106 $sha1->add($data,...)
107 The $data provided as argument are appended to the message we cal‐
108 culate the digest for. The return value is the $sha1 object
109 itself.
110
111 All these lines will have the same effect on the state of the $sha1
112 object:
113
114 $sha1->add("a"); $sha1->add("b"); $sha1->add("c");
115 $sha1->add("a")->add("b")->add("c");
116 $sha1->add("a", "b", "c");
117 $sha1->add("abc");
118
119 $sha1->addfile($io_handle)
120 The $io_handle will be read until EOF and its content appended to
121 the message we calculate the digest for. The return value is the
122 $sha1 object itself.
123
124 The addfile() method will croak() if it fails reading data for some
125 reason. If it croaks it is unpredictable what the state of the
126 $sha1 object will be in. The addfile() method might have been able
127 to read the file partially before it failed. It is probably wise
128 to discard or reset the $sha1 object if this occurs.
129
130 In most cases you want to make sure that the $io_handle is in "bin‐
131 mode" before you pass it as argument to the addfile() method.
132
133 $sha1->add_bits($data, $nbits)
134 $sha1->add_bits($bitstring)
135 This implementation of SHA-1 only supports byte oriented input so
136 you might only add bits as multiples of 8. If you need bit level
137 support please consider using the "Digest::SHA" module instead.
138 The add_bits() method is provided here for compatibility with other
139 digest implementations. See Digest for description of the argu‐
140 ments that add_bits() take.
141
142 $sha1->digest
143 Return the binary digest for the message. The returned string will
144 be 20 bytes long.
145
146 Note that the "digest" operation is effectively a destructive,
147 read-once operation. Once it has been performed, the "Digest::SHA1"
148 object is automatically "reset" and can be used to calculate
149 another digest value. Call $sha1->clone->digest if you want to
150 calculate the digest without reseting the digest state.
151
152 $sha1->hexdigest
153 Same as $sha1->digest, but will return the digest in hexadecimal
154 form. The length of the returned string will be 40 and it will only
155 contain characters from this set: '0'..'9' and 'a'..'f'.
156
157 $sha1->b64digest
158 Same as $sha1->digest, but will return the digest as a base64
159 encoded string. The length of the returned string will be 27 and
160 it will only contain characters from this set: 'A'..'Z', 'a'..'z',
161 '0'..'9', '+' and '/'.
162
163 The base64 encoded string returned is not padded to be a multiple
164 of 4 bytes long. If you want interoperability with other base64
165 encoded SHA-1 digests you might want to append the string "=" to
166 the result.
167
169 Digest, Digest::HMAC_SHA1, Digest::SHA, Digest::MD5
170
171 http://www.itl.nist.gov/fipspubs/fip180-1.htm
172
174 This library is free software; you can redistribute it and/or modify it
175 under the same terms as Perl itself.
176
177 Copyright 1999-2004 Gisle Aas.
178 Copyright 1997 Uwe Hollerbach.
179
181 Peter C. Gutmann, Uwe Hollerbach <uh@alumni.caltech.edu>, Gisle Aas
182 <gisle@aas.no>
183
184
185
186perl v5.8.8 2006-01-18 SHA1(3)