-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
60 lines (46 loc) · 956 Bytes
/
example_test.go
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
package otp_test
import (
"fmt"
"time"
"github.com/cristalhq/otp"
)
func ExampleHOTP() {
hotp, err := otp.NewHOTP(otp.HOTPConfig{
Algo: otp.AlgorithmSHA1,
Digits: 10,
Issuer: "cristalhq",
})
checkErr(err)
secretInBase32 := "JBSWY3DPEHPK3PXP"
code, err := hotp.GenerateCode(42, secretInBase32)
checkErr(err)
fmt.Println(code)
err = hotp.Validate(code, 42, secretInBase32)
checkErr(err)
// Output:
// 0979090604
}
func ExampleTOTP() {
totp, err := otp.NewTOTP(otp.TOTPConfig{
Algo: otp.AlgorithmSHA1,
Digits: 10,
Issuer: "cristalhq",
Period: 30,
Skew: 2,
})
checkErr(err)
secretInBase32 := "JBSWY3DPEHPK3PXP"
at := time.Date(2023, 11, 26, 12, 15, 18, 0, time.UTC)
code, err := totp.GenerateCode(secretInBase32, at)
checkErr(err)
fmt.Println(code)
err = totp.Validate(code, at, secretInBase32)
checkErr(err)
// Output:
// 0462778229
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}