| 1 | package Crypt::Digest::SHAKE; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 5 | our $VERSION = '0.091'; | |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 5 | $Carp::Internal{(__PACKAGE__)}++; | |||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use CryptX; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | sub addfile { | ||||||||||||||||||||||||||||||
|
Subroutine
addfile: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 12 | 3 | my ($self, $file) = @_; | |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 3 | my ($handle, $close_handle); | |||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | if (ref($file) && eval { defined fileno($file) }) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
ref $file and eval {
do {
defined fileno $file
}
}
MC/DC100%2/2
ref $file and eval {
do {
defined fileno $file
}
}
ref $file
eval {
do {
defined fileno $file
}
}
Truth table100%3/3
A and B
A= ref $fileB= eval {
do {
defined fileno $file
}
}
| |||||||||||||||||||||||||||||||||
| 16 | 1 | $handle = $file; | |||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||
| 18 | ▶ | elsif (defined($file) && !ref($file)) { | |||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $file and not ref $file
MC/DC50%1/2
defined $file and not ref $file
defined $file
not ref $file
Truth table66%2/3
A and not(B)
A= defined $fileB= not ref $file
| |||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | open($handle, "<", $file) || croak "FATAL: cannot open '$file': $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 20 | 1 | binmode($handle); | |||||||||||||||||||||||||||||||
| 21 | 1 | $close_handle = 1; | |||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||
| 23 | else { | ||||||||||||||||||||||||||||||||
| 24 | 1 | croak "FATAL: invalid handle"; | |||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 2 | my $n; | |||||||||||||||||||||||||||||||
| 28 | 2 | my $buf = ""; | |||||||||||||||||||||||||||||||
| 29 | { | ||||||||||||||||||||||||||||||||
| 30 | 2 | local $SIG{__DIE__} = \&CryptX::_croak; | |||||||||||||||||||||||||||||||
| 31 | 2 | ▶ | while (($n = read($handle, $buf, 32*1024))) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 32 | 2 | $self->add($buf) | |||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||
| 34 | 2 | ▶ | croak "FATAL: read failed: $!" unless defined $n; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||
| 36 | 2 | ▶ | close($handle) if $close_handle; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 2 | return $self; | |||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub CLONE_SKIP { 1 } # prevent cloning | ||||||||||||||||||||||||||||||
|
Subroutine
CLONE_SKIP: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | 5 | 1; | |||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | =pod | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | Crypt::Digest::SHAKE - Hash functions SHAKE128, SHAKE256 from SHA3 family | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | use Crypt::Digest::SHAKE; | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | my $d = Crypt::Digest::SHAKE->new(128); | ||||||||||||||||||||||||||||||||
| 56 | $d->add('any data'); | ||||||||||||||||||||||||||||||||
| 57 | my $part1 = $d->done(100); # 100 raw bytes | ||||||||||||||||||||||||||||||||
| 58 | my $part2 = $d->done(100); # another 100 raw bytes | ||||||||||||||||||||||||||||||||
| 59 | #... | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | # or absorb input from a file instead | ||||||||||||||||||||||||||||||||
| 62 | my $file_d = Crypt::Digest::SHAKE->new(128); | ||||||||||||||||||||||||||||||||
| 63 | $file_d->addfile('filename.dat'); | ||||||||||||||||||||||||||||||||
| 64 | my $file_part1 = $file_d->done(100); | ||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | I<Since: CryptX-0.048> | ||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | Provides an interface to the SHA3's sponge function SHAKE. | ||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | This is an XOF (extendable output function). Feed input with C<add()> / | ||||||||||||||||||||||||||||||||
| 73 | C<addfile()>, then read output with one or more C<done($len)> calls. | ||||||||||||||||||||||||||||||||
| 74 | After the first C<done()>, treat the object as being in output mode: | ||||||||||||||||||||||||||||||||
| 75 | do not call C<add()> again on that state. Use C<reset()> or a new object | ||||||||||||||||||||||||||||||||
| 76 | to start hashing a new message. | ||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | Unless noted otherwise, assume C<$d> is an existing SHAKE object created via | ||||||||||||||||||||||||||||||||
| 81 | C<new>, for example: | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | my $d = Crypt::Digest::SHAKE->new(128); | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | =head2 new | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | my $d = Crypt::Digest::SHAKE->new($num); | ||||||||||||||||||||||||||||||||
| 88 | # $num ... [integer] 128 or 256 (selects SHAKE128 or SHAKE256) | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | =head2 clone | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | my $d2 = $d->clone(); | ||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | =head2 reset | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | $d->reset(); | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | =head2 add | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | Appends data to the message. Returns the object itself (for chaining). | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | Each argument is converted to bytes using Perl's usual scalar stringification. | ||||||||||||||||||||||||||||||||
| 103 | Defined scalars, including numbers and string-overloaded objects, are | ||||||||||||||||||||||||||||||||
| 104 | accepted. C<undef> is treated as an empty string and may emit Perl's usual | ||||||||||||||||||||||||||||||||
| 105 | "uninitialized value" warning. | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | $d->add('any data'); | ||||||||||||||||||||||||||||||||
| 108 | #or | ||||||||||||||||||||||||||||||||
| 109 | $d->add('any data', 'more data', 'even more data'); | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | =head2 addfile | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | Reads the file content and appends it to the message. Returns the object itself (for chaining). | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | $d->addfile('filename.dat'); | ||||||||||||||||||||||||||||||||
| 116 | #or | ||||||||||||||||||||||||||||||||
| 117 | my $filehandle = ...; # existing binary-mode filehandle | ||||||||||||||||||||||||||||||||
| 118 | $d->addfile($filehandle); | ||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | =head2 done | ||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | Returns C<$len> bytes of output as a binary string. Can be called repeatedly | ||||||||||||||||||||||||||||||||
| 123 | to stream an unlimited amount of output from the same absorbed input. The | ||||||||||||||||||||||||||||||||
| 124 | C<$len> argument is required and must be a positive integer. Single | ||||||||||||||||||||||||||||||||
| 125 | C<done()> calls are limited to 1,000,000,000 bytes, but the recommended way | ||||||||||||||||||||||||||||||||
| 126 | to read large output is to call C<done()> repeatedly in 10 MB chunks. | ||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | After the first C<done()> call the object is in output mode. Calling | ||||||||||||||||||||||||||||||||
| 129 | C<add()> in this state croaks; use C<reset()> or create a new object to hash | ||||||||||||||||||||||||||||||||
| 130 | a different message. | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | my $result_raw = $d->done($len); | ||||||||||||||||||||||||||||||||
| 133 | # can be called multiple times; $len is the number of output bytes to read | ||||||||||||||||||||||||||||||||
| 134 | # after the first done(), add() croaks until you call reset() | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | =over | ||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | =item * L<CryptX>, L<Crypt::Digest> | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | =item * L<https://csrc.nist.gov/pubs/fips/202/final> | ||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | =back | ||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | =cut | ||||||||||||||||||||||||||||||||