| 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
| |||||||||||||||||||||||||||||
| 27 | if $secret =~ /^[a-fA-F0-9]{32,}$/; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 11 | ▶ | $c = new Math::BigInt ($c) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | unless ref $c eq "Math::BigInt"; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 11 | ▶ | $digits ||= 6; | ||||||||||||||||||||||||||
|
Condition100%2/2
$digits ||= 6
MC/DC100%1/1
$digits ||= 6
$digits
Truth table100%2/2
A
A= $digits
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 11 | ▶ | die unless length $secret >= 16; # 128-bit minimum | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 11 | ▶ | die unless ref $c eq "Math::BigInt"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 11 | ▶ | die unless $digits >= 6 and $digits <= 10; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$digits >= 6 and $digits <= 10
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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||