blib/lib/Digest/SHA.pm

filter: Statementstmt 81.6% 129 / 158 Branchbran 58.3% 56 / 96 Conditioncond 46.6% 7 / 15 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 83.3% 15 / 18 Podpod 100% 7 / 7 totaltotal 71.3% 217 / 304 CC 56 SCAR 25.6
CC
56
Cov
71%
dump
30.0
addfile
22.8
load
20.0
CRAP 12.9
↑ Summary SHA.xs »
1 package Digest::SHA;
2  
3 24 require 5.003000;
4  
5 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 ▶use vars qw($VERSION @ISA @EXPORT_OK $errmsg);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use Fcntl qw(O_RDONLY O_RDWR);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 ▶use Cwd qw(getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 24 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 24 $VERSION = '6.04';
13  
14 24 require Exporter;
15 24 @ISA = qw(Exporter);
16 24 @EXPORT_OK = qw(
17 $errmsg
18 hmac_sha1 hmac_sha1_base64 hmac_sha1_hex
19 hmac_sha224 hmac_sha224_base64 hmac_sha224_hex
20 hmac_sha256 hmac_sha256_base64 hmac_sha256_hex
21 hmac_sha384 hmac_sha384_base64 hmac_sha384_hex
22 hmac_sha512 hmac_sha512_base64 hmac_sha512_hex
23 hmac_sha512224 hmac_sha512224_base64 hmac_sha512224_hex
24 hmac_sha512256 hmac_sha512256_base64 hmac_sha512256_hex
25 sha1 sha1_base64 sha1_hex
26 sha224 sha224_base64 sha224_hex
27 sha256 sha256_base64 sha256_hex
28 sha384 sha384_base64 sha384_hex
29 sha512 sha512_base64 sha512_hex
30 sha512224 sha512224_base64 sha512224_hex
31 sha512256 sha512256_base64 sha512256_hex);
32  
33 # Inherit from Digest::base if possible
34  
35 24 eval {
36 24 require Digest::base;
37 24 push(@ISA, 'Digest::base');
38 };
39  
40 # The following routines aren't time-critical, so they can be left in Perl
41  
42 27 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
43 27 my($class, $alg) = @_;
44 27 ▶ $alg =~ s/\D+//g if defined $alg;
Branch: 1 branch
BranchTrueFalseTotal
if defined $alg 22 5 27
45 27 ▶ if (ref($class)) { # instance method
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 9 18 27
46 9 ▶ if (!defined($alg) || ($alg == $class->algorithm)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $alg or $alg == $class->algorithm) 7 2 9
Condition100%3/3
not defined $alg or $alg == $class->algorithm
l!l&&r!l&&!r
162
MC/DC100%2/2
not defined $alg or $alg == $class->algorithm
not defined $alg $alg == $class->algorithm
Truth table100%3/3
not(A) or B
A= not defined $algB= $alg == $class->algorithm
ABresult
1X1
011
000
47 7 sharewind($class);
48 7 return($class);
49 }
50 2 ▶ return shainit($class, $alg) ? $class : undef;
Branch: 1 branch
BranchTrueFalseTotal
&shainit($class, $alg) ? : 2 0 2
51 }
52 18 ▶ $alg = 1 unless defined $alg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $alg 4 14 18
53 18 return $class->newSHA($alg);
54 }
55  
56 24 ▶BEGIN { *reset = \&new }
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 712 ▶sub add_bits {
Subroutine add_bits: called CC=3 SCAR=11.0
Pod documented
59 712 my($self, $data, $nbits) = @_;
60 712 ▶ unless (defined $nbits) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nbits) 38 674 712
61 38 $nbits = length($data);
62 38 $data = pack("B*", $data);
63 }
64 712 ▶ $nbits = length($data) * 8 if $nbits > length($data) * 8;
Branch: 1 branch
BranchTrueFalseTotal
if $nbits > length($data) * 8 0 712 712
65 712 shawrite($data, $nbits, $self);
66 712 return($self);
67 }
68  
69 0 ▶sub _bail {
Subroutine _bail: not called CC=1 SCAR=6.9
70 0 my $msg = shift;
71  
72 0 $errmsg = $!;
73 0 $msg .= ": $!";
74 0 require Carp;
75 0 Carp::croak($msg);
76 }
77  
78 {
79 24 my $_can_T_filehandle;
80  
81 1 ▶ sub _istext {
Subroutine _istext: called CC=5 SCAR=18.1
82 1 local *FH = shift;
83 1 my $file = shift;
84  
85 1 ▶ if (! defined $_can_T_filehandle) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_can_T_filehandle) 1 0 1
86 1 local $^W = 0;
87 1 my $istext = eval { -T FH };
88 1 ▶ $_can_T_filehandle = $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 1 1
89 1 ▶ return $_can_T_filehandle ? $istext : -T $file;
Branch: 1 branch
BranchTrueFalseTotal
$_can_T_filehandle ? : 1 0 1
90 }
91 0 ▶ return $_can_T_filehandle ? -T FH : -T $file;
Branch: 1 branch
BranchTrueFalseTotal
$_can_T_filehandle ? : 0 0 0
92 }
93 }
94  
95 24 ▶sub _addfile {
Subroutine _addfile: called CC=3 SCAR=11.0
96 2 my ($self, $handle) = @_;
97  
98 2 my $n;
99 2 my $buf = "";
100  
101 2 ▶ while (($n = read($handle, $buf, 4096))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n = read($handle, $buf, 4096)) 2 2 4
102 2 $self->add($buf);
103 }
104 2 ▶ _bail("Read failed") unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 2 2
105  
106 2 $self;
107 }
108  
109 6 ▶sub addfile {
Subroutine addfile: called CC=16 SCAR=31.3
Pod documented
110 6 my ($self, $file, $mode) = @_;
111  
112 6 ▶ return(_addfile($self, $file)) unless ref(\$file) eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
unless ref \$file eq "SCALAR" 2 4 6
113  
114 4 ▶ $mode = defined($mode) ? $mode : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $mode ? : 4 0 4
115 my ($binary, $UNIVERSAL, $BITS) =
116 4 map { $_ eq $mode } ("b", "U", "0");
117  
118 ## Always interpret "-" to mean STDIN; otherwise use
119 ## sysopen to handle full range of POSIX file names.
120 ## If $file is a directory, force an EISDIR error
121 ## by attempting to open with mode O_RDWR
122  
123 4 local *FH;
124 4 ▶ if ($file eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq '-') { } 0 4 4
125 0 ▶ if (-d STDIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d STDIN) 0 0 0
126 0 ▶ sysopen(FH, getcwd(), O_RDWR)
Branch: 1 branch
BranchTrueFalseTotal
sysopen FH, getcwd(), 2 or _bail("Open failed") 0 0 0
127 or _bail('Open failed');
128 }
129 0 ▶ open(FH, '< -')
Branch: 1 branch
BranchTrueFalseTotal
open FH, "< -" or _bail("Open failed") 0 0 0
130 or _bail('Open failed');
131 }
132 else {
133 4 ▶ sysopen(FH, $file, -d $file ? O_RDWR : O_RDONLY)
Branch: 2 branches
BranchTrueFalseTotal
sysopen FH, $file, -d $file ? 2 : 0 or _bail("Open failed") 4 0 4
-d $file ? : 0 4 4
134 or _bail('Open failed');
135 }
136  
137 4 ▶ if ($BITS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BITS) 2 2 4
138 2 my ($n, $buf) = (0, "");
139 2 ▶ while (($n = read(FH, $buf, 4096))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n = read(FH, $buf, 4096)) 2 2 4
140 2 $buf =~ tr/01//cd;
141 2 $self->add_bits($buf);
142 }
143 2 ▶ _bail("Read failed") unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 2 2
144 2 close(FH);
145 2 return($self);
146 }
147  
148 2 ▶ binmode(FH) if $binary || $UNIVERSAL;
Branch: 1 branch
BranchTrueFalseTotal
if $binary or $UNIVERSAL 2 0 2
Condition66%2/3
$binary or $UNIVERSAL
l!l&&r!l&&!r
110
MC/DC0%0/2
$binary or $UNIVERSAL
$binary $UNIVERSAL
Truth table66%2/3
A or B
A= $binaryB= $UNIVERSAL
ABresult
1X1
011
000
149 2 ▶ if ($UNIVERSAL && _istext(*FH, $file)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UNIVERSAL and _istext(*FH, $file)) { } 1 1 2
Condition66%2/3
$UNIVERSAL and _istext(*FH, $file)
!ll&&!rl&&r
101
MC/DC50%1/2
$UNIVERSAL and _istext(*FH, $file)
$UNIVERSAL _istext(*FH, $file)
Truth table66%2/3
A and B
A= $UNIVERSALB= _istext(*FH, $file)
ABresult
0X0
100
111
150 1 $self->_addfileuniv(*FH);
151 }
152 1 else { $self->_addfilebin(*FH) }
153 2 close(FH);
154  
155 2 $self;
156 }
157  
158 4 ▶sub getstate {
Subroutine getstate: called CC=8 SCAR=20.9
Pod documented
159 4 my $self = shift;
160  
161 4 ▶ my $alg = $self->algorithm or return;
Branch: 1 branch
BranchTrueFalseTotal
my $alg = $self->algorithm or return 4 0 4
162 4 ▶ my $state = $self->_getstate or return;
Branch: 1 branch
BranchTrueFalseTotal
my $state = $self->_getstate or return 4 0 4
163 4 ▶ my $nD = $alg <= 256 ? 8 : 16;
Branch: 1 branch
BranchTrueFalseTotal
$alg <= 256 ? : 2 2 4
164 4 ▶ my $nH = $alg <= 256 ? 32 : 64;
Branch: 1 branch
BranchTrueFalseTotal
$alg <= 256 ? : 2 2 4
165 4 ▶ my $nB = $alg <= 256 ? 64 : 128;
Branch: 1 branch
BranchTrueFalseTotal
$alg <= 256 ? : 2 2 4
166 4 my($H, $block, $blockcnt, $lenhh, $lenhl, $lenlh, $lenll) =
167 $state =~ /^(.{$nH})(.{$nB})(.{4})(.{4})(.{4})(.{4})(.{4})$/s;
168 4 for ($alg, $H, $block, $blockcnt, $lenhh, $lenhl, $lenlh, $lenll) {
169 32 ▶ return unless defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 32 32
170 }
171  
172 4 my @s = ();
173 4 push(@s, "alg:" . $alg);
174 4 push(@s, "H:" . join(":", unpack("H*", $H) =~ /.{$nD}/g));
175 4 push(@s, "block:" . join(":", unpack("H*", $block) =~ /.{2}/g));
176 4 push(@s, "blockcnt:" . unpack("N", $blockcnt));
177 4 push(@s, "lenhh:" . unpack("N", $lenhh));
178 4 push(@s, "lenhl:" . unpack("N", $lenhl));
179 4 push(@s, "lenlh:" . unpack("N", $lenlh));
180 4 push(@s, "lenll:" . unpack("N", $lenll));
181 4 join("\n", @s) . "\n";
182 }
183  
184 11 ▶sub putstate {
Subroutine putstate: called CC=13 SCAR=26.0
Pod documented
185 11 my($class, $state) = @_;
186  
187 11 my %s = ();
188 11 for (split(/\n/, $state)) {
189 96 s/^\s+//;
190 96 s/\s+$//;
191 96 ▶ next if (/^(#|$)/);
Branch: 1 branch
BranchTrueFalseTotal
if /^(#|$)/ 8 88 96
192 88 my @f = split(/[:\s]+/);
193 88 my $tag = shift(@f);
194 88 $s{$tag} = join('', @f);
195 }
196  
197 # H and block may contain arbitrary values, but check everything else
198 11 ▶ grep { $_ == $s{'alg'} } (1,224,256,384,512,512224,512256) or return;
Branch: 1 branch
BranchTrueFalseTotal
grep {$_ == $s{"alg"};} 1, 224, 256, 384, 512, 512224, 512256 or return 11 0 11
199 11 ▶ length($s{'H'}) == ($s{'alg'} <= 256 ? 64 : 128) or return;
Branch: 2 branches
BranchTrueFalseTotal
length $s{"H"} == ($s{"alg"} <= 256 ? 64 : 128) or return 11 0 11
$s{'alg'} <= 256 ? : 7 4 11
200 11 ▶ length($s{'block'}) == ($s{'alg'} <= 256 ? 128 : 256) or return;
Branch: 2 branches
BranchTrueFalseTotal
length $s{"block"} == ($s{"alg"} <= 256 ? 128 : 256) or return 11 0 11
$s{'alg'} <= 256 ? : 7 4 11
201 {
202 24 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
203 11 for (qw(blockcnt lenhh lenhl lenlh lenll)) {
204 55 ▶ 0 <= $s{$_} or return;
Branch: 1 branch
BranchTrueFalseTotal
0 <= $s{$_} or return 55 0 55
205 55 ▶ $s{$_} <= 4294967295 or return;
Branch: 1 branch
BranchTrueFalseTotal
$s{$_} <= 4294967295 or return 55 0 55
206 }
207 11 ▶ $s{'blockcnt'} < ($s{'alg'} <= 256 ? 512 : 1024) or return;
Branch: 2 branches
BranchTrueFalseTotal
$s{"blockcnt"} < ($s{"alg"} <= 256 ? 512 : 1024) or return 11 0 11
$s{'alg'} <= 256 ? : 7 4 11
208 }
209  
210 my $packed_state = (
211 pack("H*", $s{'H'}) .
212 pack("H*", $s{'block'}) .
213 pack("N", $s{'blockcnt'}) .
214 pack("N", $s{'lenhh'}) .
215 pack("N", $s{'lenhl'}) .
216 pack("N", $s{'lenlh'}) .
217 11 pack("N", $s{'lenll'})
218 );
219  
220 11 return $class->new($s{'alg'})->_putstate($packed_state);
221 }
222  
223 0 ▶sub dump {
Subroutine dump: not called CC=5 SCAR=34.0
Pod documented
224 0 my $self = shift;
225 0 my $file = shift;
226  
227 0 ▶ my $state = $self->getstate or return;
Branch: 1 branch
BranchTrueFalseTotal
my $state = $self->getstate or return 0 0 0
228 0 ▶ $file = "-" if (!defined($file) || $file eq "");
Branch: 1 branch
BranchTrueFalseTotal
if not defined $file or $file eq "" 0 0 0
Condition0%0/3
not defined $file or $file eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $file or $file eq ""
not defined $file $file eq ""
Truth table0%0/3
not(A) or B
A= not defined $fileB= $file eq ""
ABresult
1X1
011
000
229  
230 0 local *FH;
231 0 ▶ open(FH, "> $file") or return;
Branch: 1 branch
BranchTrueFalseTotal
open FH, "> $file" or return 0 0 0
232 0 print FH $state;
233 0 close(FH);
234  
235 0 return($self);
236 }
237  
238 0 ▶sub load {
Subroutine load: not called CC=4 SCAR=30.0
Pod documented
239 0 my $class = shift;
240 0 my $file = shift;
241  
242 0 ▶ $file = "-" if (!defined($file) || $file eq "");
Branch: 1 branch
BranchTrueFalseTotal
if not defined $file or $file eq "" 0 0 0
Condition0%0/3
not defined $file or $file eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $file or $file eq ""
not defined $file $file eq ""
Truth table0%0/3
not(A) or B
A= not defined $fileB= $file eq ""
ABresult
1X1
011
000
243  
244 0 local *FH;
245 0 ▶ open(FH, "< $file") or return;
Branch: 1 branch
BranchTrueFalseTotal
open FH, "< $file" or return 0 0 0
246 0 my $str = join('', <FH>);
247 0 close(FH);
248  
249 0 $class->putstate($str);
250 }
251  
252 eval {
253 24 require XSLoader;
254 24 XSLoader::load('Digest::SHA', $VERSION);
255 24 1;
256 24 ▶} or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require XSLoader; XSLoader::load("Digest::SHA", $Digest::SHA::VERSION); 1 } } or do { require DynaLoader; push @Digest::SHA::ISA, "DynaLoader"; "Digest::SHA"->bootstrap($Digest::SHA::VERSION) } 24 0 24
257 0 require DynaLoader;
258 0 push @ISA, 'DynaLoader';
259 0 Digest::SHA->bootstrap($VERSION);
260 };
261  
262 24 1;
263 __END__
↑ Summary SHA.xs »