File Coverage

blib/lib/Crypt/Digest/KangarooTwelve.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::KangarooTwelve;
2              
3 2     2   138556 use strict;
  2         4  
  2         67  
4 2     2   9 use warnings;
  2         3  
  2         164  
5             our $VERSION = '0.089';
6              
7 2     2   13 use Carp;
  2         3  
  2         230  
8             $Carp::Internal{(__PACKAGE__)}++;
9 2     2   658 use CryptX;
  2         4  
  2         881  
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::KangarooTwelve - XOF (extendable output) hash function KangarooTwelve
50              
51             =head1 SYNOPSIS
52              
53             use Crypt::Digest::KangarooTwelve;
54              
55             my $d = Crypt::Digest::KangarooTwelve->new(128); # 128-bit security
56             $d->add('any data');
57             $d->customization('optional context string');
58             my $result = $d->done(32); # 32 bytes of output
59              
60             # or absorb input from a file instead
61             my $file_d = Crypt::Digest::KangarooTwelve->new(128);
62             $file_d->addfile('filename.dat');
63             $file_d->customization('optional context string');
64             my $file_result = $file_d->done(32);
65              
66             =head1 DESCRIPTION
67              
68             I
69              
70             Provides an interface to KangarooTwelve (K12) as defined in
71             L.
72              
73             KangarooTwelve is a fast cryptographic hash and XOF based on a reduced-round
74             (12-round) Keccak-p permutation. It supports an optional B
75             that binds the output to a specific context. C can be called multiple
76             times to stream arbitrary amounts of output.
77              
78             B: C must be called before C;
79             C must be called before C.
80             After the first C, treat the object as being in output mode:
81             do not call C or C again on that state. Use C
82             or a new object to start hashing a new message.
83              
84             =head1 METHODS
85              
86             Unless noted otherwise, assume C<$d> is an existing KangarooTwelve object
87             created via C, for example:
88              
89             my $d = Crypt::Digest::KangarooTwelve->new(128);
90              
91             =head2 new
92              
93             my $d = Crypt::Digest::KangarooTwelve->new($num);
94             # $num ... [integer] 128 or 256 (security level in bits)
95              
96             =head2 clone
97              
98             my $d2 = $d->clone;
99              
100             =head2 reset
101              
102             $d->reset;
103              
104             =head2 add
105              
106             Appends data to the message. Returns the object itself (for chaining).
107              
108             Each argument is converted to bytes using Perl's usual scalar stringification.
109             Defined scalars, including numbers and string-overloaded objects, are
110             accepted. C is treated as an empty string and may emit Perl's usual
111             "uninitialized value" warning.
112              
113             $d->add('any data');
114             #or
115             $d->add('chunk1', 'chunk2', ...);
116              
117             =head2 addfile
118              
119             Reads the file content and appends it to the message. Returns the object itself (for chaining).
120              
121             $d->addfile('filename.dat');
122             #or
123             my $filehandle = ...; # existing binary-mode filehandle
124             $d->addfile($filehandle);
125              
126             =head2 customization
127              
128             $d->customization('context string'); # optional; call after add(), before done()
129              
130             Each argument is converted to bytes using Perl's usual scalar stringification.
131             Defined scalars, including numbers and string-overloaded objects, are
132             accepted. C is treated as an empty string and may emit Perl's usual
133             "uninitialized value" warning.
134              
135             =head2 done
136              
137             Returns C<$len> bytes of output as a binary string. Can be called repeatedly
138             to stream an unlimited amount of output from the same absorbed input. The
139             C<$len> argument is required and must be a positive integer. Single
140             C calls are limited to 1,000,000,000 bytes, but the recommended way
141             to read large output is to call C repeatedly in 10 MB chunks.
142              
143             After the first C call the object is in output mode. Calling
144             C or C in this state croaks; use
145             C or create a new object to hash a different message.
146              
147             my $result_raw = $d->done($len);
148             # can be called multiple times; $len is the number of output bytes to read
149             # after the first done(), add() / customization() croak until you call reset()
150              
151             =head1 SEE ALSO
152              
153             =over
154              
155             =item * L, L, L
156              
157             =item * L
158              
159             =back
160              
161             =cut