blib/lib/Crypt/Rhash.pm

filter: Statementstmt 95.3% 183 / 192 Branchbran 47.2% 17 / 36 Conditioncond 57.1% 4 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 96.5% 56 / 58 Podpod 87.5% 14 / 16 totaltotal 88.1% 276 / 313 CC 22 SCAR 19.5
CC
22
Cov
87%
AUTOLOAD
56.0
update_fd
9.2
new
3.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_HAS160 RHASH_EDONR256 RHASH_EDONR512
20 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 = '0.97';
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_ALL => 0x1FFFFFFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 ##############################################################################
65 # Rhash class methods
66  
67 # Rhash object constructor
68 sub new
69 {
70 5 ▶ my $hash_id = $_[1] or die "hash_id not specified";
Subroutine new: called CC=3 SCAR=11.4
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
my $hash_id = $_[1] or die "hash_id not specified" 0 5 5
71 5 ▶ my $context = rhash_init(scalar($hash_id)) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $context = &rhash_init(scalar $hash_id) or return undef 0 5 5
72 5 my $self = {
73 context => $context,
74 };
75 5 return bless $self;
76 }
77  
78 # destructor
79 sub DESTROY($)
80 {
81 3 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=7.2
82 # the 'if' added as workaround for perl 'global destruction' bug
83 # ($self->{context} can disappear on global destruction)
84 3 ▶ rhash_free($self->{context}) if $self->{context};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"context"} 3 0 3
85 }
86  
87 sub update($$)
88 {
89 4 ▶ my $self = shift;
Subroutine update: called CC=1 SCAR=0.0
Pod documented
90 4 my $message = shift;
91 4 rhash_update($self->{context}, $message);
92 4 return $self;
93 }
94  
95 sub update_fd($$;$$)
96 {
97 3 ▶ my ($self, $fd, $start, $size) = @_;
Subroutine update_fd: called CC=9 SCAR=22.1
Pod documented
98 3 my $res = 0;
99 3 my $num = 0;
100  
101 3 binmode($fd);
102 3 ▶ if(defined($start)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start) 1 2 3
103 1 ▶ seek($fd, scalar($start), 0) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
seek $fd, scalar $start, 0 or return undef 0 1 1
104 }
105  
106 3 my $data;
107 3 ▶ if(defined($size)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size) { } 1 2 3
108 1 ▶ for(my $left = scalar($size); $left > 0; $left -= 8192) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left > 0) 1 1 2
109 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
110 ($left < 8192 ? $left : 8192))) || last;
111 1 rhash_update($self->{context}, $data);
112 1 $num += $res;
113 }
114 } else {
115 2 ▶ while( ($res = read($fd, $data, 8192)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res = read($fd, $data, 8192)) 2 2 4
116 2 rhash_update($self->{context}, $data);
117 2 $num += $res;
118 }
119 }
120  
121 3 ▶ return (defined($res) ? $num : undef); # return undef on read error
Branch: 1 branch
BranchTrueFalseTotal
defined $res ? : 3 0 3
122 }
123  
124 sub update_file($$;$$)
125 {
126 2 ▶ my ($self, $file, $start, $size) = @_;
Subroutine update_file: called CC=2 SCAR=7.0
Pod documented
127 2 ▶ open(my $fd, "<", $file) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fd, "<", $file or return undef 0 2 2
128 2 my $res = $self->update_fd($fd, $start, $size);
129 2 close($fd);
130 2 return $res;
131 }
132  
133 sub final($)
134 {
135 3 ▶ my $self = shift;
Subroutine final: called CC=1 SCAR=0.0
Pod documented
136 3 rhash_final($self->{context});
137 3 return $self;
138 }
139  
140 sub reset($)
141 {
142 3 ▶ my $self = shift;
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
143 3 rhash_reset($self->{context});
144 3 return $self;
145 }
146  
147 sub hashed_length($)
148 {
149 1 ▶ my $self = shift;
Subroutine hashed_length: called CC=1 SCAR=0.0
Pod documented
150 1 return rhash_get_hashed_length($self->{context});
151 }
152  
153 sub hash_id($)
154 {
155 1 ▶ my $self = shift;
Subroutine hash_id: called CC=1 SCAR=0.0
Pod documented
156 1 return rhash_get_hash_id($self->{context});
157 }
158  
159 ##############################################################################
160 # Hash formatting functions
161  
162 # printing constants
163 3 ▶use constant RHPR_DEFAULT => 0x0;
Subroutine BEGIN: called CC=1 SCAR=0.0
164 3 ▶use constant RHPR_RAW => 0x1;
Subroutine BEGIN: called CC=1 SCAR=0.0
165 3 ▶use constant RHPR_HEX => 0x2;
Subroutine BEGIN: called CC=1 SCAR=0.0
166 3 ▶use constant RHPR_BASE32 => 0x3;
Subroutine BEGIN: called CC=1 SCAR=0.0
167 3 ▶use constant RHPR_BASE64 => 0x4;
Subroutine BEGIN: called CC=1 SCAR=0.0
168 3 ▶use constant RHPR_UPPERCASE => 0x8;
Subroutine BEGIN: called CC=1 SCAR=0.0
169 3 ▶use constant RHPR_REVERSE => 0x10;
Subroutine BEGIN: called CC=1 SCAR=0.0
170  
171 sub hash($;$$)
172 {
173 40 ▶ my $self = shift;
Subroutine hash: called CC=3 SCAR=11.0
Pod documented
174 40 ▶ my $hash_id = scalar(shift) || 0;
Condition100%2/2
scalar shift() || 0
l!l
355
MC/DC100%1/1
scalar shift() || 0
scalar shift()
Truth table100%2/2
A
A= scalar shift()
Aresult
11
00
175 40 ▶ my $print_flags = scalar(shift) || RHPR_DEFAULT;
Condition100%2/2
scalar shift() || 0
l!l
535
MC/DC100%1/1
scalar shift() || 0
scalar shift()
Truth table100%2/2
A
A= scalar shift()
Aresult
11
00
176 40 return rhash_print($self->{context}, $hash_id, $print_flags);
177 }
178  
179 sub hash_base32($;$)
180 {
181 1 ▶ hash($_[0], $_[1], RHPR_BASE32);
Subroutine hash_base32: called CC=1 SCAR=0.0
Pod documented
182 }
183  
184 sub hash_base64($;$)
185 {
186 0 ▶ hash($_[0], $_[1], RHPR_BASE64);
Subroutine hash_base64: not called CC=1 SCAR=6.9
Pod documented
187 }
188  
189 sub hash_hex($;$)
190 {
191 2 ▶ hash($_[0], $_[1], RHPR_HEX);
Subroutine hash_hex: called CC=1 SCAR=0.0
Pod documented
192 }
193  
194 sub hash_rhex($;$)
195 {
196 1 ▶ hash($_[0], $_[1], RHPR_HEX | RHPR_REVERSE);
Subroutine hash_rhex: called CC=1 SCAR=0.0
Pod documented
197 }
198  
199 sub hash_raw($;$)
200 {
201 1 ▶ hash($_[0], $_[1], RHPR_RAW);
Subroutine hash_raw: called CC=1 SCAR=0.0
Pod undocumented
202 }
203  
204 sub magnet_link($;$$)
205 {
206 3 ▶ my ($self, $filename, $hash_mask) = @_;
Subroutine magnet_link: called CC=1 SCAR=0.0
Pod documented
207 3 return rhash_print_magnet($self->{context}, $filename, $hash_mask);
208 }
209  
210 3 our $AUTOLOAD;
211  
212 # report error if a script called unexisting method/field
213 sub AUTOLOAD
214 {
215 0 ▶ my ($self, $field, $type, $pkg) = ($_[0], $AUTOLOAD, undef, __PACKAGE__);
Subroutine AUTOLOAD: not called CC=7 SCAR=40.3
216 0 $field =~ s/.*://;
217 0 ▶ die "function $field does not exist" if $field =~ /^(rhash_|raw2)/;
Branch: 1 branch
BranchTrueFalseTotal
if $field =~ /^(rhash_|raw2)/ 0 0 0
218 0 ▶ die "no arguments specified to $field()" if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
219 0 ▶ die "the $field() argument is undefined" if !defined $self;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 0 0
220  
221 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
222 0 ▶ my $text = (exists $self->{$field} ? "is not accessible" : "does not exist");
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$field} ? : 0 0 0
223 0 die "the method $field() $text in the class $pkg";
224 }
225  
226 # static functions
227  
228 sub msg($$)
229 {
230 1 ▶ my ($hash_id, $msg) = @_;
Subroutine msg: called CC=2 SCAR=7.1
Pod documented
231 1 my $raw = rhash_msg_raw($hash_id, $msg); # get binary hash
232 1 ▶ return (is_base32($hash_id) ? raw2base32($raw) : raw2hex($raw));
Branch: 1 branch
BranchTrueFalseTotal
&is_base32($hash_id) ? : 0 1 1
233 }
234  
235 3 1;
236 __END__
↑ Summary _rhash_timing.c »