blib/lib/Crypt/Rhash.pm

filter: Statementstmt 95.1% 194 / 204 Branchbran 47.3% 18 / 38 Conditioncond 50.0% 5 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 96.6% 58 / 60 Podpod 87.5% 14 / 16 totaltotal 87.1% 291 / 334 CC 25 SCAR 19.5
CC
25
Cov
86%
AUTOLOAD
56.0
update_fd
9.2
new
7.1
CRAP 7.1
↑ Summary _rhash_timing.c »
1 package Crypt::Rhash;
2  
3 3 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 require Exporter;
8 3 our @ISA = (qw(Exporter));
9  
10 # possible tags for export
11 3 our %EXPORT_TAGS = (
12 Functions => [qw(raw2hex raw2base32 raw2base64)],
13 Constants => [qw(RHASH_CRC32 RHASH_CRC32C RHASH_MD4 RHASH_MD5
14 RHASH_SHA1 RHASH_TIGER RHASH_TTH RHASH_BTIH RHASH_ED2K
15 RHASH_AICH RHASH_WHIRLPOOL RHASH_RIPEMD160 RHASH_GOST94
16 RHASH_GOST94_CRYPTOPRO RHASH_GOST12_256 RHASH_GOST12_512
17 RHASH_SHA224 RHASH_SHA256 RHASH_SHA384 RHASH_SHA512
18 RHASH_SHA3_224 RHASH_SHA3_256 RHASH_SHA3_384 RHASH_SHA3_512
19 RHASH_BLAKE2S RHASH_BLAKE2B RHASH_EDONR256 RHASH_EDONR512
20 RHASH_HAS160 RHASH_SNEFRU128 RHASH_SNEFRU256 RHASH_ALL)]
21 );
22  
23 3 Exporter::export_tags( );
24 3 Exporter::export_ok_tags( qw(Functions Constants) );
25  
26 3 our $VERSION = '1.00';
27  
28 3 require XSLoader;
29 3 XSLoader::load('Crypt::Rhash', $VERSION);
30  
31 ##############################################################################
32 # ids of hash functions
33 3 ▶use constant RHASH_CRC32 => 0x01;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶use constant RHASH_MD4 => 0x02;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use constant RHASH_MD5 => 0x04;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use constant RHASH_SHA1 => 0x08;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶use constant RHASH_TIGER => 0x10;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 ▶use constant RHASH_TTH => 0x20;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use constant RHASH_BTIH => 0x40;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use constant RHASH_ED2K => 0x80;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶use constant RHASH_AICH => 0x100;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 ▶use constant RHASH_WHIRLPOOL => 0x200;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 ▶use constant RHASH_RIPEMD160 => 0x400;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 ▶use constant RHASH_GOST94 => 0x800;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 3 ▶use constant RHASH_GOST94_CRYPTOPRO => 0x1000;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 3 ▶use constant RHASH_HAS160 => 0x2000;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 3 ▶use constant RHASH_GOST12_256 => 0x4000;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 3 ▶use constant RHASH_GOST12_512 => 0x8000;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 3 ▶use constant RHASH_SHA224 => 0x10000;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 3 ▶use constant RHASH_SHA256 => 0x20000;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 3 ▶use constant RHASH_SHA384 => 0x40000;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 3 ▶use constant RHASH_SHA512 => 0x80000;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 3 ▶use constant RHASH_EDONR256 => 0x100000;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 3 ▶use constant RHASH_EDONR512 => 0x200000;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 3 ▶use constant RHASH_SHA3_224 => 0x0400000;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 3 ▶use constant RHASH_SHA3_256 => 0x0800000;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 3 ▶use constant RHASH_SHA3_384 => 0x1000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 3 ▶use constant RHASH_SHA3_512 => 0x2000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 3 ▶use constant RHASH_CRC32C => 0x4000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 3 ▶use constant RHASH_SNEFRU128 => 0x8000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 3 ▶use constant RHASH_SNEFRU256 => 0x10000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 3 ▶use constant RHASH_BLAKE2S => 0x20000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 3 ▶use constant RHASH_BLAKE2B => 0x40000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 3 ▶use constant RHASH_ALL => 0x7FFFFFFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 ##############################################################################
67 # Rhash class methods
68  
69 # Rhash object constructor
70 sub new($$@)
71 {
72 5 ▶ my $hash_id = 0;
Subroutine new: called CC=6 SCAR=19.6
Pod undocumented
73 5 shift;
74 5 ▶ scalar(@_) > 0 or die "hash_id not specified";
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ > 0 or die "hash_id not specified" 0 5 5
75 5 for my $id (@_) {
76 5 $hash_id |= scalar($id);
77 5 ▶ if(!scalar($id) || (scalar($id) & RHASH_ALL) != $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (not scalar $id or (scalar $id & 2147483647) != $id) 0 5 5
Condition33%1/3
not scalar $id or (scalar $id & 2147483647) != $id
l!l&&r!l&&!r
005
MC/DC0%0/2
not scalar $id or (scalar $id & 2147483647) != $id
not scalar $id (scalar $id & 2147483647) != $id
Truth table33%1/3
not(A) or B
A= not scalar $idB= (scalar $id & 2147483647) != $id
ABresult
1X1
011
000
78 0 die "bad hash_id = " . scalar($id);
79 }
80 }
81 5 ▶ my $context = rhash_init($hash_id) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $context = &rhash_init($hash_id) or return undef 0 5 5
82 5 my $self = {
83 context => $context,
84 };
85 5 return bless $self;
86 }
87  
88 # destructor
89 sub DESTROY($)
90 {
91 3 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=7.2
92 # the 'if' added as workaround for perl 'global destruction' bug
93 # ($self->{context} can disappear on global destruction)
94 3 ▶ rhash_free($self->{context}) if $self->{context};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"context"} 3 0 3
95 }
96  
97 sub update($$)
98 {
99 4 ▶ my $self = shift;
Subroutine update: called CC=1 SCAR=0.0
Pod documented
100 4 my $message = shift;
101 4 rhash_update($self->{context}, $message);
102 4 return $self;
103 }
104  
105 sub update_fd($$;$$)
106 {
107 3 ▶ my ($self, $fd, $start, $size) = @_;
Subroutine update_fd: called CC=9 SCAR=22.1
Pod documented
108 3 my $res = 0;
109 3 my $num = 0;
110  
111 3 binmode($fd);
112 3 ▶ if(defined($start)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start) 1 2 3
113 1 ▶ seek($fd, scalar($start), 0) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
seek $fd, scalar $start, 0 or return undef 0 1 1
114 }
115  
116 3 my $data;
117 3 ▶ if(defined($size)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size) { } 1 2 3
118 1 ▶ for(my $left = scalar($size); $left > 0; $left -= 8192) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left > 0) 1 1 2
119 1 ▶ ($res = read($fd, $data,
Branch: 2 branches
BranchTrueFalseTotal
$res = read($fd, $data, $left < 8192 ? $left : 8192) or last 0 1 1
$left < 8192 ? : 1 0 1
120 ($left < 8192 ? $left : 8192))) || last;
121 1 rhash_update($self->{context}, $data);
122 1 $num += $res;
123 }
124 } else {
125 2 ▶ while( ($res = read($fd, $data, 8192)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res = read($fd, $data, 8192)) 2 2 4
126 2 rhash_update($self->{context}, $data);
127 2 $num += $res;
128 }
129 }
130  
131 3 ▶ return (defined($res) ? $num : undef); # return undef on read error
Branch: 1 branch
BranchTrueFalseTotal
defined $res ? : 3 0 3
132 }
133  
134 sub update_file($$;$$)
135 {
136 2 ▶ my ($self, $file, $start, $size) = @_;
Subroutine update_file: called CC=2 SCAR=7.0
Pod documented
137 2 ▶ open(my $fd, "<", $file) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fd, "<", $file or return undef 0 2 2
138 2 my $res = $self->update_fd($fd, $start, $size);
139 2 close($fd);
140 2 return $res;
141 }
142  
143 sub final($)
144 {
145 3 ▶ my $self = shift;
Subroutine final: called CC=1 SCAR=0.0
Pod documented
146 3 rhash_final($self->{context});
147 3 return $self;
148 }
149  
150 sub reset($)
151 {
152 3 ▶ my $self = shift;
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
153 3 rhash_reset($self->{context});
154 3 return $self;
155 }
156  
157 sub hashed_length($)
158 {
159 1 ▶ my $self = shift;
Subroutine hashed_length: called CC=1 SCAR=0.0
Pod documented
160 1 return rhash_get_hashed_length($self->{context});
161 }
162  
163 sub hash_id($)
164 {
165 1 ▶ my $self = shift;
Subroutine hash_id: called CC=1 SCAR=0.0
Pod documented
166 1 return rhash_get_hash_id($self->{context});
167 }
168  
169 ##############################################################################
170 # Message digest formatting functions
171  
172 # printing constants
173 3 ▶use constant RHPR_DEFAULT => 0x0;
Subroutine BEGIN: called CC=1 SCAR=0.0
174 3 ▶use constant RHPR_RAW => 0x1;
Subroutine BEGIN: called CC=1 SCAR=0.0
175 3 ▶use constant RHPR_HEX => 0x2;
Subroutine BEGIN: called CC=1 SCAR=0.0
176 3 ▶use constant RHPR_BASE32 => 0x3;
Subroutine BEGIN: called CC=1 SCAR=0.0
177 3 ▶use constant RHPR_BASE64 => 0x4;
Subroutine BEGIN: called CC=1 SCAR=0.0
178 3 ▶use constant RHPR_UPPERCASE => 0x8;
Subroutine BEGIN: called CC=1 SCAR=0.0
179 3 ▶use constant RHPR_REVERSE => 0x10;
Subroutine BEGIN: called CC=1 SCAR=0.0
180  
181 sub hash($;$$)
182 {
183 42 ▶ my $self = shift;
Subroutine hash: called CC=3 SCAR=11.0
Pod documented
184 42 ▶ my $hash_id = scalar(shift) || 0;
Condition100%2/2
scalar shift() || 0
l!l
375
MC/DC100%1/1
scalar shift() || 0
scalar shift()
Truth table100%2/2
A
A= scalar shift()
Aresult
11
00
185 42 ▶ my $print_flags = scalar(shift) || RHPR_DEFAULT;
Condition100%2/2
scalar shift() || 0
l!l
537
MC/DC100%1/1
scalar shift() || 0
scalar shift()
Truth table100%2/2
A
A= scalar shift()
Aresult
11
00
186 42 return rhash_print($self->{context}, $hash_id, $print_flags);
187 }
188  
189 sub hash_base32($;$)
190 {
191 1 ▶ hash($_[0], $_[1], RHPR_BASE32);
Subroutine hash_base32: called CC=1 SCAR=0.0
Pod documented
192 }
193  
194 sub hash_base64($;$)
195 {
196 0 ▶ hash($_[0], $_[1], RHPR_BASE64);
Subroutine hash_base64: not called CC=1 SCAR=6.9
Pod documented
197 }
198  
199 sub hash_hex($;$)
200 {
201 2 ▶ hash($_[0], $_[1], RHPR_HEX);
Subroutine hash_hex: called CC=1 SCAR=0.0
Pod documented
202 }
203  
204 sub hash_rhex($;$)
205 {
206 1 ▶ hash($_[0], $_[1], RHPR_HEX | RHPR_REVERSE);
Subroutine hash_rhex: called CC=1 SCAR=0.0
Pod documented
207 }
208  
209 sub hash_raw($;$)
210 {
211 1 ▶ hash($_[0], $_[1], RHPR_RAW);
Subroutine hash_raw: called CC=1 SCAR=0.0
Pod undocumented
212 }
213  
214 sub magnet_link($;$$)
215 {
216 3 ▶ my ($self, $filename, $hash_mask) = @_;
Subroutine magnet_link: called CC=1 SCAR=0.0
Pod documented
217 3 return rhash_print_magnet($self->{context}, $filename, $hash_mask);
218 }
219  
220 3 our $AUTOLOAD;
221  
222 # report error if a script called unexisting method/field
223 sub AUTOLOAD
224 {
225 0 ▶ my ($self, $field, $type, $pkg) = ($_[0], $AUTOLOAD, undef, __PACKAGE__);
Subroutine AUTOLOAD: not called CC=7 SCAR=40.3
226 0 $field =~ s/.*://;
227 0 ▶ die "function $field does not exist" if $field =~ /^(rhash_|raw2)/;
Branch: 1 branch
BranchTrueFalseTotal
if $field =~ /^(rhash_|raw2)/ 0 0 0
228 0 ▶ die "no arguments specified to $field()" if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
229 0 ▶ die "the $field() argument is undefined" if !defined $self;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 0 0
230  
231 0 ▶ ($type = ref($self)) && $type eq $pkg || die "the $field() argument is not a $pkg reference";
Branch: 1 branch
BranchTrueFalseTotal
$type = ref $self and $type eq $pkg or die "the $field() argument is not a $pkg reference" 0 0 0
Condition0%0/3
$type = ref $self and $type eq $pkg
!ll&&!rl&&r
000
MC/DC0%0/2
$type = ref $self and $type eq $pkg
$type = ref $self $type eq $pkg
Truth table0%0/3
A and B
A= $type = ref $selfB= $type eq $pkg
ABresult
0X0
100
111
232 0 ▶ my $text = (exists $self->{$field} ? "is not accessible" : "does not exist");
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$field} ? : 0 0 0
233 0 die "the method $field() $text in the class $pkg";
234 }
235  
236 # static functions
237  
238 sub msg($$)
239 {
240 1 ▶ my ($hash_id, $msg) = @_;
Subroutine msg: called CC=2 SCAR=7.1
Pod documented
241 1 my $raw = rhash_msg_raw($hash_id, $msg); # get a binary message digest
242 1 ▶ return (is_base32($hash_id) ? raw2base32($raw) : raw2hex($raw));
Branch: 1 branch
BranchTrueFalseTotal
&is_base32($hash_id) ? : 0 1 1
243 }
244  
245 3 1;
246 __END__
↑ Summary _rhash_timing.c »