File Coverage

blib/lib/Crypt/Digest/SHAKE.pm
Criterion Covered Total %
statement 30 31 96.7
branch 8 10 80.0
condition 5 6 83.3
subroutine 5 6 83.3
pod 1 1 100.0
total 49 54 90.7


line stmt bran cond sub pod time code
1             package Crypt::Digest::SHAKE;
2              
3 4     4   131805 use strict;
  4         7  
  4         138  
4 4     4   19 use warnings;
  4         6  
  4         271  
5             our $VERSION = '0.089';
6              
7 4     4   23 use Carp;
  4         5  
  4         267  
8             $Carp::Internal{(__PACKAGE__)}++;
9 4     4   398 use CryptX;
  4         7  
  4         1226  
10              
11             sub addfile {
12 3     3 1 40 my ($self, $file) = @_;
13              
14 3         4 my ($handle, $close_handle);
15 3 100 100     13 if (ref($file) && eval { defined fileno($file) }) {
  2 100 66     13  
16 1         2 $handle = $file;
17             }
18             elsif (defined($file) && !ref($file)) {
19 1 50       47 open($handle, "<", $file) || croak "FATAL: cannot open '$file': $!";
20 1         3 binmode($handle);
21 1         2 $close_handle = 1;
22             }
23             else {
24 1         92 croak "FATAL: invalid handle";
25             }
26              
27 2         3 my $n;
28 2         2 my $buf = "";
29             {
30 2         2 local $SIG{__DIE__} = \&CryptX::_croak;
  2         8  
31 2         33 while (($n = read($handle, $buf, 32*1024))) {
32 2         80 $self->add($buf)
33             }
34 2 50       6 croak "FATAL: read failed: $!" unless defined $n;
35             }
36 2 100       11 close($handle) if $close_handle;
37              
38 2         20 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::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             Provides an interface to the SHA3's sponge function SHAKE.
69              
70             This is an XOF (extendable output function). Feed input with C /
71             C, then read output with one or more C calls.
72             After the first C, treat the object as being in output mode:
73             do not call C again on that state. Use C or a new object
74             to start hashing a new message.
75              
76             =head1 METHODS
77              
78             Unless noted otherwise, assume C<$d> is an existing SHAKE object created via
79             C, for example:
80              
81             my $d = Crypt::Digest::SHAKE->new(128);
82              
83             =head2 new
84              
85             my $d = Crypt::Digest::SHAKE->new($num);
86             # $num ... [integer] 128 or 256 (selects SHAKE128 or SHAKE256)
87              
88             =head2 clone
89              
90             my $d2 = $d->clone();
91              
92             =head2 reset
93              
94             $d->reset();
95              
96             =head2 add
97              
98             Appends data to the message. Returns the object itself (for chaining).
99              
100             Each argument is converted to bytes using Perl's usual scalar stringification.
101             Defined scalars, including numbers and string-overloaded objects, are
102             accepted. C is treated as an empty string and may emit Perl's usual
103             "uninitialized value" warning.
104              
105             $d->add('any data');
106             #or
107             $d->add('any data', 'more data', 'even more data');
108              
109             =head2 addfile
110              
111             Reads the file content and appends it to the message. Returns the object itself (for chaining).
112              
113             $d->addfile('filename.dat');
114             #or
115             my $filehandle = ...; # existing binary-mode filehandle
116             $d->addfile($filehandle);
117              
118             =head2 done
119              
120             Returns C<$len> bytes of output as a binary string. Can be called repeatedly
121             to stream an unlimited amount of output from the same absorbed input. The
122             C<$len> argument is required and must be a positive integer. Single
123             C calls are limited to 1,000,000,000 bytes, but the recommended way
124             to read large output is to call C repeatedly in 10 MB chunks.
125              
126             After the first C call the object is in output mode. Calling
127             C in this state croaks; use C or create a new object to hash
128             a different message.
129              
130             my $result_raw = $d->done($len);
131             # can be called multiple times; $len is the number of output bytes to read
132             # after the first done(), add() croaks until you call reset()
133              
134             =head1 SEE ALSO
135              
136             =over
137              
138             =item * L, L
139              
140             =item * L
141              
142             =back
143              
144             =cut