summaryrefslogtreecommitdiff
path: root/t/totp-test-vectors.t
blob: beeb1189da926b4103d1d7a7747fcb0cbce3e229 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use Test::More;

use utf8;
use strict;
use warnings;

require_ok 'Pass::OTP';

# Based on the test vectors from RFC6238

my %seeds = (
    sha1   => '3132333435363738393031323334353637383930',
    sha256 => '3132333435363738393031323334353637383930'.
              '313233343536373839303132',
    sha512 => '3132333435363738393031323334353637383930'.
              '3132333435363738393031323334353637383930'.
              '3132333435363738393031323334353637383930'.
              '31323334',
);

sub is_totp {
    my %opts = (@_);

    is(
        Pass::OTP::otp(
        secret    => $seeds{lc $opts{algorithm}},
        algorithm => $opts{algorithm},
        now       => $opts{now},
        digits    => 8,
        type      => 'totp'),
        $opts{totp},
        "Test vector with time (sec) $opts{now} on mode $opts{algorithm}"
    );
}

is_totp(now => 59, algorithm => 'sha1',   totp => '94287082');
is_totp(now => 59, algorithm => 'sha256', totp => '46119246');
is_totp(now => 59, algorithm => 'sha512', totp => '90693936');


is_totp(now => 1111111109, algorithm => 'sha1',   totp => '07081804');
is_totp(now => 1111111109, algorithm => 'sha256', totp => '68084774');
is_totp(now => 1111111109, algorithm => 'sha512', totp => '25091201');


is_totp(now => 1111111111, algorithm => 'sha1',   totp => '14050471');
is_totp(now => 1111111111, algorithm => 'sha256', totp => '67062674');
is_totp(now => 1111111111, algorithm => 'sha512', totp => '99943326');


is_totp(now => 1234567890, algorithm => 'sha1',   totp => '89005924');
is_totp(now => 1234567890, algorithm => 'sha256', totp => '91819424');
is_totp(now => 1234567890, algorithm => 'sha512', totp => '93441116');


is_totp(now => 2000000000, algorithm => 'sha1',   totp => '69279037');
is_totp(now => 2000000000, algorithm => 'sha256', totp => '90698825');
is_totp(now => 2000000000, algorithm => 'sha512', totp => '38618901');


is_totp(now => 20000000000, algorithm => 'sha1',   totp => '65353130');
is_totp(now => 20000000000, algorithm => 'sha256', totp => '77737706');
is_totp(now => 20000000000, algorithm => 'sha512', totp => '47863826');


is_totp(now => 59, algorithm => 'SHA512', totp => '90693936');

done_testing(20);