blib/lib/Authen/HOTP.pm

filter: Statementstmt 100% 39 / 39 Branchbran 60.0% 6 / 10 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 85.9% 55 / 64 CC 8 SCAR 17.2
CC
8
Cov
89%
hotp
8.5
BEGIN
1.0
BEGIN
1.0
CRAP 5.6
↑ Summary
1 package Authen::HOTP;
2  
3 1 ▶use Digest::SHA1 qw(sha1);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Digest::HMAC qw(hmac);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 require Exporter;
10  
11 1 our @ISA = qw(Exporter);
12  
13 1 our %EXPORT_TAGS = ( 'all' => [ qw( hotp ) ] );
14  
15 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
16  
17 1 our @EXPORT = qw( );
18  
19 1 our $VERSION = '0.02';
20  
21 sub hotp
22 11 ▶{
Subroutine hotp: called CC=8 SCAR=21.4
Pod undocumented
23 11 my ($secret, $c, $digits) = @_;
24  
25 # guess hex encoded
26 11 ▶ $secret = join("", map chr(hex), $secret =~ /(..)/g)
Branch: 1 branch
BranchTrueFalseTotal
if $secret =~ /^[a-fA-F0-9]{32,}$/ 10 1 11
27 if $secret =~ /^[a-fA-F0-9]{32,}$/;
28  
29 11 ▶ $c = new Math::BigInt ($c)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c eq "Math::BigInt" 11 0 11
30 unless ref $c eq "Math::BigInt";
31  
32 11 ▶ $digits ||= 6;
Condition100%2/2
$digits ||= 6
l!l
110
MC/DC100%1/1
$digits ||= 6
$digits
Truth table100%2/2
A
A= $digits
Aresult
11
00
33  
34 11 ▶ die unless length $secret >= 16; # 128-bit minimum
Branch: 1 branch
BranchTrueFalseTotal
unless length $secret >= 16 0 11 11
35 11 ▶ die unless ref $c eq "Math::BigInt";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c eq "Math::BigInt" 0 11 11
36 11 ▶ die unless $digits >= 6 and $digits <= 10;
Branch: 1 branch
BranchTrueFalseTotal
unless $digits >= 6 and $digits <= 10 0 11 11
Condition33%1/3
$digits >= 6 and $digits <= 10
!ll&&!rl&&r
0011
MC/DC0%0/2
$digits >= 6 and $digits <= 10
$digits >= 6 $digits <= 10
Truth table33%1/3
A and B
A= $digits >= 6B= $digits <= 10
ABresult
0X0
100
111
37  
38 11 (my $hex = $c->as_hex) =~ s/^0x(.*)/"0"x(16 - length $1).$1/e;
39 11 my $bin = join '', map chr hex, $hex =~ /(..)/g; # pack 64-bit big endian
40 11 my $hash = hmac $bin, $secret, \&sha1;
41 11 my $offset = hex substr unpack("H*" => $hash), -1;
42 11 my $dt = unpack "N" => substr $hash, $offset, 4;
43 11 $dt &= 0x7fffffff; # 31-bit
44 11 $dt %= (10 ** $digits); # limit range
45  
46 11 sprintf "%0${digits}d", $dt;
47 }
48  
49 1 1;
50 __END__
↑ Summary