/
calc.go
51 lines (43 loc) · 1.01 KB
/
calc.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
package main
import (
"fmt"
"github.com/tristanwietsma/otp"
)
func getCode(secret string) (string, int64) {
iv, rem := otp.GetInterval(30)
code, err := otp.GetCode(secret, iv, otp.Hashes[0], 6)
if err != nil {
return "calculation failed", -1
}
return code, rem
}
type calcCommand struct{}
func (c calcCommand) Name() string {
return "calc"
}
func (c calcCommand) Run(args []string) bool {
if len(args) != 1 {
return false
}
cfg := getCfg()
k, ok := cfg.Key[args[0]]
if !ok {
return false
}
code, rem := getCode(k.Secret)
if rem > 0 {
fmt.Printf("%v (%v seconds)\n", code, rem)
return true
}
fmt.Printf("%v; verify %v is correctly formatted", code, getCfgPath())
return true
}
func (c calcCommand) Usage() {
usage := " calc calculate a one-time password"
fmt.Println(usage)
}
func (c calcCommand) Help() {
help := "\n" + c.Name() + " usage:\n\n 2fa " + c.Name() + " label\n\n"
help += " The label is associated with a key and defined in " + getCfgPath() + ".\n"
fmt.Println(help)
}