blib/lib/gmd5.pm

filter: Statementstmt 100% 195 / 195 Branchbran 83.3% 20 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 38 / 38 Podpod 100% 7 / 7 totaltotal 98.4% 260 / 264 CC 14 SCAR 5.4
CC
14
Cov
98%
_compute_round
4.0
_process_block
3.0
add
3.0
CRAP 1.7
↑ Summary
1 package gmd5;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '2.4.1'; # Incremented to reflect fixes
9  
10 1 our @EXPORT_OK = qw(md5 md5_hex);
11  
12 10 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
13 10 my ($class) = @_;
14 10 state @h_init = (0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476);
15 10 my $self = bless {
16 h => [@h_init],
17 buffer => '',
18 length => 0,
19 pos => 0,
20 w => [(0) x 16],
21 final_digest => undef,
22 }, $class;
23 10 return $self;
24 }
25  
26 12 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
27 12 my ($self) = @_;
28 12 state @h_init = (0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476);
29 12 $self->{h} = [@h_init];
30 12 $self->{buffer} = '';
31 12 $self->{length} = 0;
32 12 $self->{pos} = 0;
33 12 $self->{final_digest} = undef;
34 12 return $self;
35 }
36  
37 24 ▶sub add {
Subroutine add: called CC=3 SCAR=11.0
Pod documented
38 24 my ($self, $data) = @_;
39 24 _validate_input($data);
40 23 ▶ return $self unless length($data);
Branch: 1 branch
BranchTrueFalseTotal
unless length $data 2 21 23
41 21 $self->{final_digest} = undef;
42 21 _append_data($self, $data);
43 21 ▶ _process_buffer($self) while _has_enough_data($self);
Branch: 1 branch
BranchTrueFalseTotal
_has_enough_data($self) and do { _process_buffer($self) } 9 21 30
44 21 return $self;
45 }
46  
47 23 ▶sub digest {
Subroutine digest: called CC=2 SCAR=6.9
Pod documented
48 23 my ($self) = @_;
49 23 ▶ if (defined $self->{final_digest}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"final_digest"}) 1 22 23
50 1 return $self->{final_digest};
51 }
52 22 my $digest = _compute_digest($self);
53 22 $self->{final_digest} = $digest;
54 22 state @h_init = (0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476);
55 22 $self->{h} = [@h_init];
56 22 return $digest;
57 }
58  
59 22 ▶sub hexdigest {
Subroutine hexdigest: called CC=1 SCAR=0.0
Pod documented
60 22 my ($self) = @_;
61 22 my $digest = $self->digest;
62 22 return _format_hex($digest);
63 }
64  
65 2 ▶sub md5 {
Subroutine md5: called CC=1 SCAR=0.0
Pod documented
66 2 my ($data) = @_;
67 2 _validate_input($data);
68 1 my $md5 = gmd5->new;
69 1 $md5->add($data);
70 1 return $md5->digest;
71 }
72  
73 6 ▶sub md5_hex {
Subroutine md5_hex: called CC=1 SCAR=0.0
Pod documented
74 6 my ($data) = @_;
75 6 _validate_input($data);
76 5 my $md5 = gmd5->new;
77 5 $md5->add($data);
78 5 return $md5->hexdigest;
79 }
80  
81 32 ▶sub _validate_input {
Subroutine _validate_input: called CC=2 SCAR=6.9
82 32 my ($data) = @_;
83 32 ▶ die "Input must be defined" unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 3 29 32
84 }
85  
86 21 ▶sub _append_data {
Subroutine _append_data: called CC=1 SCAR=0.0
87 21 my ($self, $data) = @_;
88 21 $self->{buffer} .= $data;
89 21 $self->{length} += length($data);
90 }
91  
92 30 ▶sub _has_enough_data {
Subroutine _has_enough_data: called CC=1 SCAR=0.0
93 30 my ($self) = @_;
94 30 my $buffer_length = length($self->{buffer});
95 30 my $position = $self->{pos};
96 30 return $buffer_length - $position >= 64;
97 }
98  
99 31 ▶sub _process_buffer {
Subroutine _process_buffer: called CC=2 SCAR=6.9
100 31 my ($self) = @_;
101 31 my $buf_ref = \$self->{buffer};
102 31 my $pos = $self->{pos};
103 31 my $len = length($$buf_ref);
104 31 my $block_num = 0;
105  
106 31 ▶ while (_can_process_block($pos, $len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_can_process_block($pos, $len)) 37 31 68
107 37 my $w = [(0) x 16];
108 37 my $block = _extract_block($buf_ref, $pos);
109 37 _unpack_words($w, $block, $block_num);
110 37 _process_block($self, $self->{h}, $w);
111 37 $pos = _increment_position($pos);
112 37 $block_num++;
113 }
114  
115 31 _update_buffer($self, $buf_ref, $pos);
116 }
117  
118 68 ▶sub _can_process_block {
Subroutine _can_process_block: called CC=1 SCAR=0.0
119 68 my ($pos, $len) = @_;
120 68 return $pos + 64 <= $len;
121 }
122  
123 37 ▶sub _extract_block {
Subroutine _extract_block: called CC=1 SCAR=0.0
124 37 my ($buf_ref, $pos) = @_;
125 37 return substr($$buf_ref, $pos, 64);
126 }
127  
128 37 ▶sub _unpack_words {
Subroutine _unpack_words: called CC=1 SCAR=0.0
129 37 my ($w, $block, $block_num) = @_;
130 37 @$w = unpack("V16", $block);
131 }
132  
133 37 ▶sub _increment_position {
Subroutine _increment_position: called CC=1 SCAR=0.0
134 37 my ($pos) = @_;
135 37 return $pos + 64;
136 }
137  
138 31 ▶sub _update_buffer {
Subroutine _update_buffer: called CC=2 SCAR=7.1
139 31 my ($self, $buf_ref, $pos) = @_;
140 31 ▶ $$buf_ref = $pos ? substr($$buf_ref, $pos) : '';
Branch: 1 branch
BranchTrueFalseTotal
$pos ? : 31 0 31
141 31 $self->{pos} = 0;
142 }
143  
144 22 ▶sub _compute_digest {
Subroutine _compute_digest: called CC=1 SCAR=0.0
145 22 my ($self) = @_;
146 22 my $len = _compute_bit_length($self);
147 22 my $original_buffer = $self->{buffer};
148 22 _append_padding($self);
149 22 my $pad_len = length($self->{buffer}) - length($original_buffer) - 1;
150 22 _append_length($self, $len);
151 22 _validate_buffer_length($self);
152 22 _process_buffer($self);
153 22 my $digest = _construct_digest($self);
154 22 $self->{final_digest} = $digest;
155 22 $self->{buffer} = $original_buffer;
156 22 return $digest;
157 }
158  
159 22 ▶sub _compute_bit_length {
Subroutine _compute_bit_length: called CC=2 SCAR=7.0
160 22 my ($self) = @_;
161 22 my $len = $self->{length} * 8;
162 22 ▶ die "Invalid length" if $len < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len < 0 0 22 22
163 22 return $len;
164 }
165  
166 22 ▶sub _append_padding {
Subroutine _append_padding: called CC=1 SCAR=0.0
167 22 my ($self) = @_;
168 22 _append_one_bit($self);
169 22 _append_zero_padding($self);
170 }
171  
172 22 ▶sub _append_one_bit {
Subroutine _append_one_bit: called CC=1 SCAR=0.0
173 22 my ($self) = @_;
174 22 $self->{buffer} .= "\x80";
175 }
176  
177 22 ▶sub _append_zero_padding {
Subroutine _append_zero_padding: called CC=1 SCAR=0.0
178 22 my ($self) = @_;
179 22 my $current_len = length($self->{buffer});
180 22 my $pad_len = (56 - $current_len % 64) % 64;
181 22 $self->{buffer} .= "\x00" x $pad_len;
182 }
183  
184 22 ▶sub _append_length {
Subroutine _append_length: called CC=1 SCAR=0.0
185 22 my ($self, $len) = @_;
186 22 my $length_bytes = _pack_length($len);
187 22 $self->{buffer} .= $length_bytes;
188 }
189  
190 22 ▶sub _pack_length {
Subroutine _pack_length: called CC=1 SCAR=0.0
191 22 my ($len) = @_;
192 22 return pack("V2", $len & 0xffffffff, $len >> 32);
193 }
194  
195 22 ▶sub _validate_buffer_length {
Subroutine _validate_buffer_length: called CC=2 SCAR=7.2
196 22 my ($self) = @_;
197 22 ▶ die "Invalid buffer length" unless length($self->{buffer}) % 64 == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless length($self->{"buffer"}) % 64 == 0 0 22 22
198 }
199  
200 22 ▶sub _construct_digest {
Subroutine _construct_digest: called CC=1 SCAR=0.0
201 22 my ($self) = @_;
202 22 my $h = $self->{h};
203 22 return pack("V4", @$h);
204 }
205  
206 22 ▶sub _format_hex {
Subroutine _format_hex: called CC=1 SCAR=0.0
207 22 my ($digest) = @_;
208 22 return unpack("H*", $digest);
209 }
210  
211 2368 ▶sub _rotate_left {
Subroutine _rotate_left: called CC=1 SCAR=0.0
212 2368 my ($value, $shift) = @_;
213 2368 my $left_shift = ($value << $shift) & 0xffffffff;
214 2368 my $right_shift = ($value >> (32 - $shift)) & 0xffffffff;
215 2368 return ($left_shift | $right_shift) & 0xffffffff;
216 }
217  
218 37 ▶sub _process_block {
Subroutine _process_block: called CC=3 SCAR=11.0
219 37 my ($self, $h, $w) = @_;
220 37 state @t = map { $_ & 0xffffffff } (
221 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
222 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
223 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
224 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
225 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
226 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
227 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
228 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
229 );
230 37 state @s = (
231 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
232 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
233 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
234 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
235 );
236 37 state @g = (
237 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
238 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12,
239 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2,
240 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9
241 );
242  
243 37 ▶ my $start_time = time() if $ENV{GMD5_PROFILE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"GMD5_PROFILE"} 0 37 37
244 37 my @initial_h = @$h;
245 37 my ($a, $b, $c, $d) = @initial_h;
246 37 for my $i (0 .. 63) {
247 2368 ($a, $b, $c, $d) = _compute_round($self, $i, $a, $b, $c, $d, \@t, \@s, $w, \@g);
248 }
249 37 _update_hash_values($h, $a, $b, $c, $d, \@initial_h);
250 }
251  
252 2368 ▶sub _compute_round {
Subroutine _compute_round: called CC=4 SCAR=13.9
253 2368 my ($self, $i, $a, $b, $c, $d, $t, $s, $w, $g) = @_;
254 2368 $a &= 0xffffffff;
255 2368 $b &= 0xffffffff;
256 2368 $c &= 0xffffffff;
257 2368 $d &= 0xffffffff;
258 2368 my $f;
259 2368 ▶ if ($i < 16) {
Branch: 3 branches
BranchTrueFalseTotal
if ($i < 16) { } 592 1776 2368
elsif ($i < 32) { } 592 1184 1776
elsif ($i < 48) { } 592 592 1184
260 592 $f = _compute_f($b, $c, $d);
261 } elsif ($i < 32) {
262 592 $f = _compute_g($b, $c, $d);
263 } elsif ($i < 48) {
264 592 $f = _compute_h($b, $c, $d);
265 } else {
266 592 $f = _compute_i($b, $c, $d);
267 }
268 2368 $f &= 0xffffffff;
269 2368 my $word = $w->[$g->[$i]] & 0xffffffff;
270 2368 my $table_value = $t->[$i] & 0xffffffff;
271 2368 my $sum = ($a + $f) & 0xffffffff;
272 2368 $sum = ($sum + $word) & 0xffffffff;
273 2368 $sum = ($sum + $table_value) & 0xffffffff;
274 2368 my $temp = _rotate_left($sum, $s->[$i]) & 0xffffffff;
275 2368 $temp = ($b + $temp) & 0xffffffff;
276 2368 return ($d, $temp, $b, $c);
277 }
278  
279 592 ▶sub _compute_f {
Subroutine _compute_f: called CC=1 SCAR=0.0
280 592 my ($b, $c, $d) = @_;
281 592 $b &= 0xffffffff;
282 592 $c &= 0xffffffff;
283 592 $d &= 0xffffffff;
284 592 my $f = (($b & $c) | (~$b & $d)) & 0xffffffff;
285 592 return $f;
286 }
287  
288 592 ▶sub _compute_g {
Subroutine _compute_g: called CC=1 SCAR=0.0
289 592 my ($b, $c, $d) = @_;
290 592 $b &= 0xffffffff;
291 592 $c &= 0xffffffff;
292 592 $d &= 0xffffffff;
293 592 my $bd = $b & $d;
294 592 my $not_d = (~$d) & 0xffffffff;
295 592 my $c_not_d = $c & $not_d;
296 592 my $f = ($bd | $c_not_d) & 0xffffffff;
297 592 return $f;
298 }
299  
300 592 ▶sub _compute_h {
Subroutine _compute_h: called CC=1 SCAR=0.0
301 592 my ($b, $c, $d) = @_;
302 592 $b &= 0xffffffff;
303 592 $c &= 0xffffffff;
304 592 $d &= 0xffffffff;
305 592 my $bc = $b ^ $c;
306 592 my $f = ($bc ^ $d) & 0xffffffff;
307 592 return $f;
308 }
309  
310 592 ▶sub _compute_i {
Subroutine _compute_i: called CC=1 SCAR=0.0
311 592 my ($b, $c, $d) = @_;
312 592 $b &= 0xffffffff;
313 592 $c &= 0xffffffff;
314 592 $d &= 0xffffffff;
315 592 my $not_d = (~$d) & 0xffffffff;
316 592 my $b_not_d = $b | $not_d;
317 592 my $f = ($c ^ $b_not_d) & 0xffffffff;
318 592 return $f;
319 }
320  
321 37 ▶sub _update_hash_values {
Subroutine _update_hash_values: called CC=1 SCAR=0.0
322 37 my ($h, $a, $b, $c, $d, $initial_h) = @_;
323 37 $h->[0] = ($initial_h->[0] + $a) & 0xffffffff;
324 37 $h->[1] = ($initial_h->[1] + $b) & 0xffffffff;
325 37 $h->[2] = ($initial_h->[2] + $c) & 0xffffffff;
326 37 $h->[3] = ($initial_h->[3] + $d) & 0xffffffff;
327 }
328  
329 1 1;
↑ Summary