blib/lib/Crypt/Digest/TurboSHAKE.pm

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