-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtotp_test.go
More file actions
147 lines (129 loc) · 3.81 KB
/
totp_test.go
File metadata and controls
147 lines (129 loc) · 3.81 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package totp_test
import (
"encoding/base32"
"testing"
"time"
"github.com/itpey/totp"
)
type tc struct {
TS int64
TOTP string
Mode totp.Algorithm
Secret string
}
var (
secSha1 = base32.StdEncoding.EncodeToString([]byte("12345678901234567890"))
secSha256 = base32.StdEncoding.EncodeToString([]byte("12345678901234567890123456789012"))
secSha512 = base32.StdEncoding.EncodeToString([]byte("1234567890123456789012345678901234567890123456789012345678901234"))
rfcMatrixTCs = []tc{
{59, "94287082", totp.AlgorithmSHA1, secSha1},
{59, "46119246", totp.AlgorithmSHA256, secSha256},
{59, "90693936", totp.AlgorithmSHA512, secSha512},
{1111111109, "07081804", totp.AlgorithmSHA1, secSha1},
{1111111109, "68084774", totp.AlgorithmSHA256, secSha256},
{1111111109, "25091201", totp.AlgorithmSHA512, secSha512},
{1111111111, "14050471", totp.AlgorithmSHA1, secSha1},
{1111111111, "67062674", totp.AlgorithmSHA256, secSha256},
{1111111111, "99943326", totp.AlgorithmSHA512, secSha512},
{1234567890, "89005924", totp.AlgorithmSHA1, secSha1},
{1234567890, "91819424", totp.AlgorithmSHA256, secSha256},
{1234567890, "93441116", totp.AlgorithmSHA512, secSha512},
{2000000000, "69279037", totp.AlgorithmSHA1, secSha1},
{2000000000, "90698825", totp.AlgorithmSHA256, secSha256},
{2000000000, "38618901", totp.AlgorithmSHA512, secSha512},
{20000000000, "65353130", totp.AlgorithmSHA1, secSha1},
{20000000000, "77737706", totp.AlgorithmSHA256, secSha256},
{20000000000, "47863826", totp.AlgorithmSHA512, secSha512},
}
)
func TestValidateRFCMatrix(t *testing.T) {
for _, tx := range rfcMatrixTCs {
cfg := totp.Config{
Secret: tx.Secret,
Digits: totp.DigitsEight,
Period: 30,
Skew: 0,
Algorithm: tx.Mode,
}
generator := totp.New(cfg)
valid, err := generator.ValidateForTime(tx.TOTP, time.Unix(tx.TS, 0).UTC())
if err != nil {
t.Errorf("Error validating TOTP for time %d: %v", tx.TS, err)
}
if !valid {
t.Errorf("TOTP %s was not valid for time %d", tx.TOTP, tx.TS)
}
}
}
func TestGenerateRFCTCs(t *testing.T) {
for _, tx := range rfcMatrixTCs {
cfg := totp.Config{
Secret: tx.Secret,
Digits: totp.DigitsEight,
Period: 30,
Skew: 0,
Algorithm: tx.Mode,
}
generator := totp.New(cfg)
passcode, err := generator.GenerateForTime(time.Unix(tx.TS, 0).UTC())
if err != nil {
t.Errorf("Error generating TOTP for time %d: %v", tx.TS, err)
continue
}
if passcode != tx.TOTP {
t.Errorf("Expected TOTP %s, but got %s for time %d", tx.TOTP, passcode, tx.TS)
}
}
}
func TestValidateSkew(t *testing.T) {
secSha1 := base32.StdEncoding.EncodeToString([]byte("12345678901234567890"))
tests := []tc{
{29, "94287082", totp.AlgorithmSHA1, secSha1},
{59, "94287082", totp.AlgorithmSHA1, secSha1},
{61, "94287082", totp.AlgorithmSHA1, secSha1},
}
for _, tx := range tests {
cfg := totp.Config{
Secret: tx.Secret,
Digits: totp.DigitsEight,
Period: 30,
Skew: 1,
Algorithm: tx.Mode,
}
generator := totp.New(cfg)
valid, err := generator.ValidateForTime(tx.TOTP, time.Unix(tx.TS, 0).UTC())
if err != nil {
t.Errorf("Error validating TOTP for time %d: %v", tx.TS, err)
}
if !valid {
t.Errorf("TOTP %s was not valid for time %d", tx.TOTP, tx.TS)
}
}
}
func BenchmarkGenerate(b *testing.B) {
totp := totp.New(totp.Config{
Secret: "JBSWY3DPEHPK3PXP",
Digits: 6,
Period: 30,
Skew: 1,
Algorithm: totp.AlgorithmSHA1,
})
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = totp.Generate()
}
}
func BenchmarkValidate(b *testing.B) {
totp := totp.New(totp.Config{
Secret: "JBSWY3DPEHPK3PXP",
Digits: 6,
Period: 30,
Skew: 1,
Algorithm: totp.AlgorithmSHA1,
})
code, _ := totp.Generate()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = totp.Validate(code)
}
}