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