| 1 | package Crypt::Digest::SHA256; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 4 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 4 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 4 | our $VERSION = '0.091'; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 4 | ▶ | use base qw(Crypt::Digest Exporter); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 4 | our %EXPORT_TAGS = ( all => [qw( sha256 sha256_hex sha256_b64 sha256_b64u sha256_file sha256_file_hex sha256_file_b64 sha256_file_b64u )] ); | |||||||||||||||||||
| 11 | 4 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||
| 12 | 4 | our @EXPORT = qw(); | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 4 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 4 | $Carp::Internal{(__PACKAGE__)}++; | |||||||||||||||||||
| 16 | 4 | ▶ | use Crypt::Digest; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 21 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 19 | 21 | my ($class) = @_; | |||||||||||||||||||
| 20 | 21 | my $obj = Crypt::Digest->new('SHA256'); | |||||||||||||||||||
| 21 | 21 | return bless $obj, $class; | |||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 3 | ▶ | sub clone { | ||||||||||||||||||
|
Subroutine
clone: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||
| 25 | 3 | my ($self) = @_; | |||||||||||||||||||
| 26 | 3 | my $obj = Crypt::Digest::clone($self); | |||||||||||||||||||
| 27 | 3 | ▶ | return bless $obj, ref($self) || $self; | ||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 3 | ▶ | sub hashsize { Crypt::Digest::hashsize('SHA256') } | ||||||||||||||||||
|
Subroutine
hashsize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 31 | 4 | ▶ | sub sha256 { Crypt::Digest::digest_data('SHA256', @_) } | ||||||||||||||||||
|
Subroutine
sha256: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 32 | 6 | ▶ | sub sha256_hex { Crypt::Digest::digest_data_hex('SHA256', @_) } | ||||||||||||||||||
|
Subroutine
sha256_hex: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 33 | 4 | ▶ | sub sha256_b64 { Crypt::Digest::digest_data_b64('SHA256', @_) } | ||||||||||||||||||
|
Subroutine
sha256_b64: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 34 | 1 | ▶ | sub sha256_b64u { Crypt::Digest::digest_data_b64u('SHA256', @_) } | ||||||||||||||||||
|
Subroutine
sha256_b64u: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 35 | 4 | ▶ | sub sha256_file { Crypt::Digest::digest_file('SHA256', @_) } | ||||||||||||||||||
|
Subroutine
sha256_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 36 | 4 | ▶ | sub sha256_file_hex { Crypt::Digest::digest_file_hex('SHA256', @_) } | ||||||||||||||||||
|
Subroutine
sha256_file_hex: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 37 | 4 | ▶ | sub sha256_file_b64 { Crypt::Digest::digest_file_b64('SHA256', @_) } | ||||||||||||||||||
|
Subroutine
sha256_file_b64: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 38 | 0 | ▶ | sub sha256_file_b64u { Crypt::Digest::digest_file_b64u('SHA256', @_) } | ||||||||||||||||||
|
Subroutine
sha256_file_b64u: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 4 | 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<undef> is treated as an empty | ||||||||||||||||||||
| 111 | string and may emit Perl's usual "uninitialized value" warning. The same | ||||||||||||||||||||
| 112 | rules apply to C<sha256_hex>, C<sha256_b64>, and | ||||||||||||||||||||
| 113 | C<sha256_b64u>. | ||||||||||||||||||||
| 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<with> 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<Note:> The filehandle must be in binary mode before you pass it to C<addfile()>. | ||||||||||||||||||||
| 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<with> 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<Crypt::Digest>. | ||||||||||||||||||||
| 184 | Unless noted otherwise, assume C<$d> is an existing digest object created via | ||||||||||||||||||||
| 185 | C<new>, 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<undef> 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<add()>, | ||||||||||||||||||||
| 235 | C<addfile()>, C<digest()>, C<hexdigest()>, C<b64digest()>, or | ||||||||||||||||||||
| 236 | C<b64udigest()> call will fail until you call C<reset()>. | ||||||||||||||||||||
| 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<digest()>, 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<digest()>, 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<digest()>, 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<CryptX>, L<Crypt::Digest> | ||||||||||||||||||||
| 266 | |||||||||||||||||||||
| 267 | =item * L<https://en.wikipedia.org/wiki/SHA-2> | ||||||||||||||||||||
| 268 | |||||||||||||||||||||
| 269 | =back | ||||||||||||||||||||
| 270 | |||||||||||||||||||||
| 271 | =cut | ||||||||||||||||||||