-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
80 lines (63 loc) · 1.5 KB
/
examples_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// SPDX-FileCopyrightText: 2018 Joern Barthel <[email protected]>
// SPDX-License-Identifier: Apache-2.0
//go:build !ci
package ykoath_test
import (
"fmt"
"log"
"time"
yk "cunicu.li/go-iso7816/devices/yubikey"
"cunicu.li/go-iso7816/drivers/pcsc"
"github.com/ebfe/scard"
"cunicu.li/go-ykoath/v2"
)
func Example() {
ctx, err := scard.EstablishContext()
if err != nil {
log.Printf("Failed to establish context: %v", err)
return
}
sc, err := pcsc.OpenFirstCard(ctx, yk.HasOATH, false)
if err != nil {
log.Printf("Failed to connect to card: %v", err)
return
}
c, err := ykoath.NewCard(sc)
if err != nil {
log.Print(err)
return
}
defer c.Close()
// Fix the clock
c.Clock = func() time.Time {
return time.Unix(59, 0)
}
// Enable OATH for this session
if _, err = c.Select(); err != nil {
log.Printf("Failed to select applet: %v", err)
return
}
// Reset the applet
// if err := c.Reset(); err != nil {
// log.Printf("Failed to reset applet: %v", err)
// return
// }
// Add the testvector
if err = c.Put("testvector", ykoath.HmacSha1, ykoath.Totp, 8, []byte("12345678901234567890"), false, 0); err != nil {
log.Printf("Failed to put: %v", err)
return
}
names, err := c.List()
if err != nil {
log.Printf("Failed to list: %v", err)
return
}
for _, name := range names {
fmt.Printf("Name: %s\n", name)
}
otp, _ := c.CalculateMatch("testvector", nil)
fmt.Printf("OTP: %s\n", otp)
// Output:
// Name: testvector (HMAC-SHA1 TOTP)
// OTP: 94287082
}