File Coverage

blib/lib/Crypt/Digest/TurboSHAKE.pm
Criterion Covered Total %
statement 12 31 38.7
branch 0 10 0.0
condition 0 6 0.0
subroutine 4 6 66.6
pod 1 1 100.0
total 17 54 31.4


line stmt bran cond sub pod time code
1             package Crypt::Digest::TurboSHAKE;
2              
3 2     2   91472 use strict;
  2         4  
  2         56  
4 2     2   12 use warnings;
  2         3  
  2         183  
5             our $VERSION = '0.089';
6              
7 2     2   11 use Carp;
  2         2  
  2         112  
8             $Carp::Internal{(__PACKAGE__)}++;
9 2     2   357 use CryptX;
  2         4  
  2         511  
10              
11             sub addfile {
12 0     0 1   my ($self, $file) = @_;
13              
14 0           my ($handle, $close_handle);
15 0 0 0       if (ref($file) && eval { defined fileno($file) }) {
  0 0 0        
16 0           $handle = $file;
17             }
18             elsif (defined($file) && !ref($file)) {
19 0 0         open($handle, "<", $file) || croak "FATAL: cannot open '$file': $!";
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;
  0            
31 0           while (($n = read($handle, $buf, 32*1024))) {
32 0           $self->add($buf)
33             }
34 0 0         croak "FATAL: read failed: $!" unless defined $n;
35             }
36 0 0         close($handle) if $close_handle;
37              
38 0           return $self;
39             }
40              
41 0     0     sub CLONE_SKIP { 1 } # prevent cloning
42              
43             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
67              
68             Provides an interface to TurboSHAKE128 and TurboSHAKE256 as defined in
69             L.
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
73             can be called multiple times to stream arbitrary amounts of output.
74              
75             After the first C, treat the object as being in output mode:
76             do not call C again on that state. Use C 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, 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 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 calls are limited to 1,000,000,000 bytes, but the recommended way
127             to read large output is to call C repeatedly in 10 MB chunks.
128              
129             After the first C call the object is in output mode. Calling
130             C in this state croaks; use C 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, L, L
142              
143             =item * L
144              
145             =back
146              
147             =cut