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   87131 use strict;
  4         6  
  4         110  
4 4     4   16 use warnings;
  4         4  
  4         183  
5             our $VERSION = '0.089_001';
6              
7 4     4   29 use Carp;
  4         4  
  4         217  
8             $Carp::Internal{(__PACKAGE__)}++;
9 4     4   357 use CryptX;
  4         12  
  4         1018  
10              
11             sub addfile {
12 3     3 1 59 my ($self, $file) = @_;
13              
14 3         6 my ($handle, $close_handle);
15 3 100 100     19 if (ref($file) && eval { defined fileno($file) }) {
  2 100 66     17  
16 1         2 $handle = $file;
17             }
18             elsif (defined($file) && !ref($file)) {
19 1 50       67 open($handle, "<", $file) || croak "FATAL: cannot open '$file': $!";
20 1         4 binmode($handle);
21 1         3 $close_handle = 1;
22             }
23             else {
24 1         156 croak "FATAL: invalid handle";
25             }
26              
27 2         3 my $n;
28 2         4 my $buf = "";
29             {
30 2         3 local $SIG{__DIE__} = \&CryptX::_croak;
  2         11  
31 2         61 while (($n = read($handle, $buf, 32*1024))) {
32 2         113 $self->add($buf)
33             }
34 2 50       11 croak "FATAL: read failed: $!" unless defined $n;
35             }
36 2 100       16 close($handle) if $close_handle;
37              
38 2         30 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             I
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 /
73             C, then read output with one or more C calls.
74             After the first C, treat the object as being in output mode:
75             do not call C again on that state. Use C 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, 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 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 calls are limited to 1,000,000,000 bytes, but the recommended way
126             to read large output is to call C repeatedly in 10 MB chunks.
127              
128             After the first C call the object is in output mode. Calling
129             C in this state croaks; use C 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, L
141              
142             =item * L
143              
144             =back
145              
146             =cut