Signature/Method/HMAC.pm

filter: Statementstmt 97.8% 46 / 47 Branchbran 50.0% 6 / 12 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 85.7% 66 / 77 CC 8 SCAR 11.9
CC
8
Cov
85%
_get_digest
7.2
verify
2.0
_pad_base64
2.0
CRAP 3.3
« Signature.pm ↑ Summary Signature/Method/RSA.pm »
1 package Authen::HTTP::Signature::Method::HMAC;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Digest::SHA qw(hmac_sha1_base64 hmac_sha256_base64 hmac_sha512_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Authen::HTTP::Signature::Method::HMAC - Compute digest using a symmetric key
14  
15 =cut
16  
17 2 our $VERSION = '0.03';
18  
19 =head1 PURPOSE
20  
21 This class uses a symmetric key to compute a HTTP signature digest. It implements the
22 HMAC-SHA{1, 256, 512} algorithms.
23  
24 =head1 ATTRIBUTES
25  
26 These are Perlish mutators; pass a value to set it, pass no value to get the current value.
27  
28 =over
29  
30 =item key
31  
32 Key material. Read-only. Required.
33  
34 =back
35  
36 =cut
37  
38 2 has 'key' => (
39 is => 'ro',
40 required => 1,
41 );
42  
43 =over
44  
45 =item data
46  
47 The data to be signed. Read-only. Required.
48  
49 =back
50  
51 =cut
52  
53 2 has 'data' => (
54 is => 'ro',
55 required => 1,
56 );
57  
58 =over
59  
60 =item hash
61  
62 The algorithm to generate the digest. Read-only. Required.
63  
64 =back
65  
66 =cut
67  
68 2 has 'hash' => (
69 is => 'ro',
70 required => 1,
71 );
72  
73 =head1 METHODS
74  
75 =cut
76  
77 7 ▶sub _pad_base64 {
Subroutine _pad_base64: called CC=2 SCAR=7.0
78 7 my $self = shift;
79 7 my $b64_str = shift;
80  
81 7 my $n = length($b64_str) % 4;
82  
83 7 ▶ if ( $n ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 7 0 7
84 7 $b64_str .= '=' x (4-$n);
85 }
86  
87 7 return $b64_str;
88 }
89  
90 7 ▶sub _get_digest {
Subroutine _get_digest: called CC=6 SCAR=19.7
91 7 my $self = shift;
92 7 my $algo = shift;
93 7 my $data = shift;
94 7 my $key = shift;
95  
96 7 my $digest;
97 7 ▶ if ( $algo =~ /sha1/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($algo =~ /sha1/) { } 4 3 7
elsif ($algo =~ /sha256/) { } 3 0 3
elsif ($algo =~ /sha512/) { } 0 0 0
98 4 $digest = hmac_sha1_base64($data, $key);
99 }
100 elsif ( $algo =~ /sha256/ ) {
101 3 $digest = hmac_sha256_base64($data, $key);
102 }
103 elsif ( $algo =~ /sha512/ ) {
104 0 $digest = hmac_sha512_base64($data, $key);
105 }
106  
107 7 ▶ confess "I couldn't get a $algo digest\n" unless defined $digest && length $digest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $digest and length $digest 0 7 7
Condition33%1/3
defined $digest and length $digest
!ll&&!rl&&r
007
MC/DC0%0/2
defined $digest and length $digest
defined $digest length $digest
Truth table33%1/3
A and B
A= defined $digestB= length $digest
ABresult
0X0
100
111
108  
109 7 return $digest;
110 }
111  
112 =over
113  
114 =item sign()
115  
116 Signs C<data> with C<key> using C<hash>.
117  
118 Returns a Base 64 encoded digest.
119  
120 =back
121  
122 =cut
123  
124 3 ▶sub sign {
Subroutine sign: called CC=1 SCAR=0.0
Pod documented
125 3 my $self = shift;
126  
127 3 return $self->_generate_signature();
128 }
129  
130 7 ▶sub _generate_signature {
Subroutine _generate_signature: called CC=1 SCAR=0.0
131 7 my $self = shift;
132  
133 7 return $self->_pad_base64(
134 $self->_get_digest(
135 $self->hash,
136 $self->data,
137 $self->key
138 )
139 );
140 }
141  
142 =over
143  
144 =item verify()
145  
146 Compares the given signature to a computed one. Returns true if they are the same. False otherwise.
147  
148 =back
149  
150 =cut
151  
152 4 ▶sub verify {
Subroutine verify: called CC=2 SCAR=7.0
Pod documented
153 4 my $self = shift;
154 4 my $candidate = shift;
155  
156 4 ▶ confess "How can I validate anything without a signature?" unless $candidate;
Branch: 1 branch
BranchTrueFalseTotal
unless $candidate 0 4 4
157  
158 4 return $self->_generate_signature() eq $candidate;
159 }
160  
161 =head1 SEE ALSO
162  
163 L<Authen::HTTP::Signature>
164  
165 =cut
166  
167 2 1;
« Signature.pm ↑ Summary Signature/Method/RSA.pm »