File Coverage

blib/lib/Crypt/Digest/SHA256.pm
Criterion Covered Total %
statement 26 30 86.6
branch n/a
condition 0 3 0.0
subroutine 14 16 87.5
pod 11 11 100.0
total 51 60 85.0


line stmt bran cond sub pod time code
1             package Crypt::Digest::SHA256;
2              
3             ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY!
4              
5 4     4   298105 use strict;
  4         7  
  4         174  
6 4     4   37 use warnings;
  4         7  
  4         281  
7             our $VERSION = '0.089';
8              
9 4     4   23 use base qw(Crypt::Digest Exporter);
  4         12  
  4         1806  
10             our %EXPORT_TAGS = ( all => [qw( sha256 sha256_hex sha256_b64 sha256_b64u sha256_file sha256_file_hex sha256_file_b64 sha256_file_b64u )] );
11             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
12             our @EXPORT = qw();
13              
14 4     4   60 use Carp;
  4         7  
  4         308  
15             $Carp::Internal{(__PACKAGE__)}++;
16 4     4   43 use Crypt::Digest;
  4         5  
  4         1878  
17              
18             sub new {
19 19     19 1 10452 my ($class) = @_;
20 19         166 my $obj = Crypt::Digest->new('SHA256');
21 19         194 return bless $obj, $class;
22             }
23              
24             sub clone {
25 0     0 1 0 my ($self) = @_;
26 0         0 my $obj = Crypt::Digest::clone($self);
27 0   0     0 return bless $obj, ref($self) || $self;
28             }
29              
30 3     3 1 201868 sub hashsize { Crypt::Digest::hashsize('SHA256') }
31 4     4 1 1016 sub sha256 { Crypt::Digest::digest_data('SHA256', @_) }
32 6     6 1 7268 sub sha256_hex { Crypt::Digest::digest_data_hex('SHA256', @_) }
33 4     4 1 78 sub sha256_b64 { Crypt::Digest::digest_data_b64('SHA256', @_) }
34 1     1 1 14 sub sha256_b64u { Crypt::Digest::digest_data_b64u('SHA256', @_) }
35 4     4 1 53 sub sha256_file { Crypt::Digest::digest_file('SHA256', @_) }
36 4     4 1 20 sub sha256_file_hex { Crypt::Digest::digest_file_hex('SHA256', @_) }
37 4     4 1 18 sub sha256_file_b64 { Crypt::Digest::digest_file_b64('SHA256', @_) }
38 0     0 1   sub sha256_file_b64u { Crypt::Digest::digest_file_b64u('SHA256', @_) }
39              
40             1;
41              
42             =pod
43              
44             =head1 NAME
45              
46             Crypt::Digest::SHA256 - Hash function SHA-256 [size: 256 bits]
47              
48             =head1 SYNOPSIS
49              
50             ### Functional interface:
51             use Crypt::Digest::SHA256 qw( sha256 sha256_hex sha256_b64 sha256_b64u
52             sha256_file sha256_file_hex sha256_file_b64 sha256_file_b64u );
53              
54             # calculate digest from string/buffer
55             my $data = 'data string';
56             my $sha256_raw = sha256($data);
57             my $sha256_hex = sha256_hex($data);
58             my $sha256_b64 = sha256_b64($data);
59             my $sha256_b64u = sha256_b64u($data);
60             # or from file
61             my $sha256_file_raw = sha256_file('filename.dat');
62             my $sha256_file_hex = sha256_file_hex('filename.dat');
63             my $sha256_file_b64 = sha256_file_b64('filename.dat');
64             my $sha256_file_b64u = sha256_file_b64u('filename.dat');
65             # or from filehandle
66             my $filehandle = ...; # existing binary-mode filehandle
67             my $sha256_fh_raw = sha256_file($filehandle);
68             my $sha256_fh_hex = sha256_file_hex($filehandle);
69             my $sha256_fh_b64 = sha256_file_b64($filehandle);
70             my $sha256_fh_b64u = sha256_file_b64u($filehandle);
71              
72             ### OO interface:
73             use Crypt::Digest::SHA256;
74              
75             my $d = Crypt::Digest::SHA256->new;
76             $d->add('any data');
77             my $result_raw = $d->digest; # raw bytes
78             my $result_hex = $d->hexdigest; # hexadecimal form
79             my $result_b64 = $d->b64digest; # Base64 form
80             my $result_b64u = $d->b64udigest; # Base64 URL-safe form
81              
82             # or hash a file instead
83             my $file_result_raw = Crypt::Digest::SHA256->new->addfile('filename.dat')->digest;
84              
85             =head1 DESCRIPTION
86              
87             Provides an interface to the SHA256 digest algorithm.
88              
89             =head1 EXPORT
90              
91             Nothing is exported by default.
92              
93             You can export selected functions:
94              
95             use Crypt::Digest::SHA256 qw(sha256 sha256_hex sha256_b64 sha256_b64u
96             sha256_file sha256_file_hex sha256_file_b64 sha256_file_b64u);
97              
98             Or all of them at once:
99              
100             use Crypt::Digest::SHA256 ':all';
101              
102             =head1 FUNCTIONS
103              
104             =head2 sha256
105              
106             Joins all arguments into a single string and returns its SHA256 digest encoded as a binary string.
107              
108             Data arguments for the functional helpers are converted to byte strings using
109             Perl's usual scalar stringification. Defined scalars, including numbers and
110             string-overloaded objects, are accepted. C is treated as an empty
111             string and may emit Perl's usual "uninitialized value" warning. The same
112             rules apply to C, C, and
113             C.
114              
115             my $sha256_raw = sha256('data string');
116             #or
117             my $sha256_raw = sha256('any data', 'more data', 'even more data');
118              
119             =head2 sha256_hex
120              
121             Joins all arguments into a single string and returns its SHA256 digest encoded as a hexadecimal string.
122              
123             my $sha256_hex = sha256_hex('data string');
124             #or
125             my $sha256_hex = sha256_hex('any data', 'more data', 'even more data');
126              
127             =head2 sha256_b64
128              
129             Joins all arguments into a single string and returns its SHA256 digest encoded as a Base64 string, B trailing '=' padding.
130              
131             my $sha256_b64 = sha256_b64('data string');
132             #or
133             my $sha256_b64 = sha256_b64('any data', 'more data', 'even more data');
134              
135             =head2 sha256_b64u
136              
137             Joins all arguments into a single string and returns its SHA256 digest encoded as a Base64 URL-safe string (see RFC 4648 section 5).
138              
139             my $sha256_b64url = sha256_b64u('data string');
140             #or
141             my $sha256_b64url = sha256_b64u('any data', 'more data', 'even more data');
142              
143             =head2 sha256_file
144              
145             Reads a file given by a filename or filehandle and returns its SHA256 digest encoded as a binary string.
146              
147             my $sha256_raw = sha256_file('filename.dat');
148             #or
149             my $filehandle = ...; # existing binary-mode filehandle
150             my $sha256_raw = sha256_file($filehandle);
151              
152             =head2 sha256_file_hex
153              
154             Reads a file given by a filename or filehandle and returns its SHA256 digest encoded as a hexadecimal string.
155              
156             my $sha256_hex = sha256_file_hex('filename.dat');
157             #or
158             my $filehandle = ...; # existing binary-mode filehandle
159             my $sha256_hex = sha256_file_hex($filehandle);
160              
161             B The filehandle must be in binary mode before you pass it to C.
162              
163             =head2 sha256_file_b64
164              
165             Reads a file given by a filename or filehandle and returns its SHA256 digest encoded as a Base64 string, B trailing '=' padding.
166              
167             my $sha256_b64 = sha256_file_b64('filename.dat');
168             #or
169             my $filehandle = ...; # existing binary-mode filehandle
170             my $sha256_b64 = sha256_file_b64($filehandle);
171              
172             =head2 sha256_file_b64u
173              
174             Reads a file given by a filename or filehandle and returns its SHA256 digest encoded as a Base64 URL-safe string (see RFC 4648 section 5).
175              
176             my $sha256_b64url = sha256_file_b64u('filename.dat');
177             #or
178             my $filehandle = ...; # existing binary-mode filehandle
179             my $sha256_b64url = sha256_file_b64u($filehandle);
180              
181             =head1 METHODS
182              
183             The OO interface provides the same set of functions as L.
184             Unless noted otherwise, assume C<$d> is an existing digest object created via
185             C, for example:
186              
187             my $d = Crypt::Digest::SHA256->new();
188              
189             =head2 new
190              
191             my $d = Crypt::Digest::SHA256->new();
192              
193             =head2 clone
194              
195             $d->clone();
196              
197             =head2 reset
198              
199             $d->reset();
200              
201             =head2 add
202              
203             Appends data to the message. Returns the object itself (for chaining).
204              
205             Each argument is converted to bytes using Perl's usual scalar stringification.
206             Defined scalars, including numbers and string-overloaded objects, are
207             accepted. C is treated as an empty string and may emit Perl's usual
208             "uninitialized value" warning.
209              
210             $d->add('any data');
211             #or
212             $d->add('any data', 'more data', 'even more data');
213              
214             =head2 addfile
215              
216             Reads the file content and appends it to the message. Returns the object itself (for chaining).
217              
218             $d->addfile('filename.dat');
219             #or
220             my $filehandle = ...; # existing binary-mode filehandle
221             $d->addfile($filehandle);
222              
223             =head2 hashsize
224              
225             $d->hashsize;
226             #or
227             Crypt::Digest::SHA256->hashsize();
228             #or
229             Crypt::Digest::SHA256::hashsize();
230              
231             =head2 digest
232              
233             Returns the binary digest (raw bytes).
234             The first call finalizes the digest object. Any later C,
235             C, C, C, C, or
236             C call will fail until you call C.
237              
238             my $result_raw = $d->digest();
239              
240             =head2 hexdigest
241              
242             Returns the digest encoded as a lowercase hexadecimal string.
243             Like C, the first call finalizes the digest object.
244              
245             my $result_hex = $d->hexdigest();
246              
247             =head2 b64digest
248              
249             Returns the digest encoded as a Base64 string with trailing C<=> padding.
250             Like C, the first call finalizes the digest object.
251              
252             my $result_b64 = $d->b64digest();
253              
254             =head2 b64udigest
255              
256             Returns the digest encoded as a Base64 URL-safe string (no trailing C<=>).
257             Like C, the first call finalizes the digest object.
258              
259             my $result_b64url = $d->b64udigest();
260              
261             =head1 SEE ALSO
262              
263             =over
264              
265             =item * L, L
266              
267             =item * L
268              
269             =back
270              
271             =cut