blib/lib/Crypt/RIPEMD160/MAC.pm

filter: Statementstmt 100% 47 / 47 Branchbran 75.0% 3 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 6 / 6 totaltotal 98.5% 67 / 68 CC 4 SCAR 4.6
CC
4
Cov
98%
DESTROY
3.0
new
2.0
BEGIN
1.0
CRAP 1.6
« blib/lib/Crypt/RIPEMD160.pm ↑ Summary RIPEMD160.xs »
1 package Crypt::RIPEMD160::MAC;
2  
3 5 ▶use Crypt::RIPEMD160 0.03;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 our $VERSION = '0.14';
10  
11 74 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
12 74 my($pkg, $key) = @_;
13  
14 # Hash long keys per RFC 2104
15 74 ▶ if (length($key) > 64) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $key > 64) 11 63 74
16 11 $key = Crypt::RIPEMD160->hash($key);
17 }
18  
19 74 my $k_ipad = chr(0x36) x 64;
20 74 my $k_opad = chr(0x5c) x 64;
21 74 $k_ipad ^= $key;
22 74 $k_opad ^= $key;
23  
24 74 my $self = {
25 'key' => $key,
26 'hash' => Crypt::RIPEMD160->new,
27 'k_ipad' => $k_ipad,
28 'k_opad' => $k_opad,
29 };
30  
31 74 bless $self, $pkg;
32  
33 74 $self->{'hash'}->add($self->{'k_ipad'});
34  
35 74 return $self;
36 }
37  
38 9 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
39 9 my($self) = @_;
40  
41 9 $self->{'hash'}->reset();
42 9 $self->{'hash'}->add($self->{'k_ipad'});
43  
44 9 return $self;
45 }
46  
47 259 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod documented
48 259 my($self, @data) = @_;
49  
50 259 $self->{'hash'}->add(@data);
51  
52 259 return $self;
53 }
54  
55 sub addfile
56 6 ▶{
Subroutine addfile: called CC=1 SCAR=0.0
Pod documented
57 6 my ($self, $handle) = @_;
58  
59 6 binmode($handle);
60 6 $self->{'hash'}->addfile($handle);
61  
62 5 return $self;
63 }
64  
65 75 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.1
66 75 my($self) = @_;
67  
68 # Best-effort zeroing of key material in Perl scalars.
69 # Not as reliable as C-level secure_memzero (Perl may keep
70 # copies via COW or realloc), but better than leaving keys
71 # in memory until GC reclaims the storage.
72 75 for my $field (qw(key k_ipad k_opad)) {
73 225 ▶ if (defined $self->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$field}) 225 0 225
74 225 $self->{$field} = "\x00" x length($self->{$field});
75 225 $self->{$field} = '';
76 }
77 }
78 }
79  
80 41 ▶sub mac {
Subroutine mac: called CC=1 SCAR=0.0
Pod documented
81 41 my($self) = @_;
82  
83 41 my($inner) = $self->{'hash'}->digest();
84  
85 41 return Crypt::RIPEMD160->hash($self->{'k_opad'}.$inner);
86 }
87  
88 33 ▶sub hexmac {
Subroutine hexmac: called CC=1 SCAR=0.0
Pod documented
89 33 my($self) = @_;
90  
91 33 my($inner) = $self->{'hash'}->digest();
92  
93 33 return Crypt::RIPEMD160->hexhash($self->{'k_opad'}.$inner);
94 }
95  
96 5 1;
97 __END__
« blib/lib/Crypt/RIPEMD160.pm ↑ Summary RIPEMD160.xs »