File: totp_test.go

package info (click to toggle)
golang-github-gokyle-twofactor 1.0.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 120 kB
  • sloc: makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,675 bytes parent folder | download | duplicates (2)
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
package twofactor

import (
	"crypto"
	"fmt"
	"testing"
)

var rfcTotpKey = []byte("12345678901234567890")
var rfcTotpStep uint64 = 30

var rfcTotpTests = []struct {
	Time uint64
	Code string
	T    uint64
	Algo crypto.Hash
}{
	{59, "94287082", 1, crypto.SHA1},
	//{59, "46119246", 1, crypto.SHA256},
	//{59, "90693936", 1, crypto.SHA512},
	{1111111109, "07081804", 37037036, crypto.SHA1},
	// {1111111109, "68084774", 37037036, crypto.SHA256},
	// {1111111109, "25091201", 37037036, crypto.SHA512},
	{1111111111, "14050471", 37037037, crypto.SHA1},
	// {1111111111, "67062674", 37037037, crypto.SHA256},
	// {1111111111, "99943326", 37037037, crypto.SHA512},
	{1234567890, "89005924", 41152263, crypto.SHA1},
	// {1234567890, "91819424", 41152263, crypto.SHA256},
	// {1234567890, "93441116", 41152263, crypto.SHA512},
	{2000000000, "69279037", 66666666, crypto.SHA1},
	// {2000000000, "90698825", 66666666, crypto.SHA256},
	// {2000000000, "38618901", 66666666, crypto.SHA512},
	{20000000000, "65353130", 666666666, crypto.SHA1},
	// {20000000000, "77737706", 666666666, crypto.SHA256},
	// {20000000000, "47863826", 666666666, crypto.SHA512},
}

func TestTotpRFC(t *testing.T) {
	for _, tc := range rfcTotpTests {
		otp := NewTOTP(rfcTotpKey, 0, 30, 8, tc.Algo)
		if otp.otpCounter(tc.Time) != tc.T {
			fmt.Println("twofactor: invalid T")
			fmt.Printf("\texpected: %d\n", tc.T)
			fmt.Printf("\t  actual: %d\n", otp.otpCounter(tc.Time))
			t.FailNow()
		}

		if code := otp.otp(otp.otpCounter(tc.Time)); code != tc.Code {
			fmt.Println("twofactor: invalid TOTP")
			fmt.Printf("\texpected: %s\n", tc.Code)
			fmt.Printf("\t  actual: %s\n", code)
			t.FailNow()
		}
	}
}