-
Notifications
You must be signed in to change notification settings - Fork 1
/
totp.go
59 lines (51 loc) · 1.41 KB
/
totp.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
package gotp
import (
"crypto/sha1"
"fmt"
"hash"
"strings"
"time"
)
// Totp defines an TOTP object
type Totp struct {
otp
period uint64
}
// NewDefaultTotp creates an Totp object with default arguments
func NewDefaultTotp(secret string) *Totp {
return NewTotp("", "", secret, sha1.New, "sha1", 6, 30)
}
// NewTotp creates an Totp object with detailed arguments
func NewTotp(accountName, issuer, secret string, algorithm func() hash.Hash, algoString string, digits uint8, period uint64) *Totp {
return &Totp{
otp: otp{
accountName: accountName,
issuer: issuer,
secret: secret,
algorithm: algorithm,
algoString: algoString,
digits: digits,
},
period: period,
}
}
// Type returns "totp"
func (o *Totp) Type() string {
return "totp"
}
// GenerateWithRemainingSeconds generates an TOTP value with the remaining seconds
func (o *Totp) GenerateWithRemainingSeconds(t time.Time) (uint32, uint64) {
timeUnix := uint64(t.Unix())
return o.At(timeUnix / o.period), o.period - (timeUnix % o.period)
}
// Generate an OTP value
func (o *Totp) Generate() uint32 {
result, _ := o.GenerateWithRemainingSeconds(time.Now())
return result
}
// URI reassembles the otp uri
func (o *Totp) URI() string {
label, parameters := o.uriFragments()
parameters = append(parameters, fmt.Sprintf("period=%v", o.period))
return fmt.Sprintf("otpauth://%s/%s?%s", o.Type(), label, strings.Join(parameters, "&"))
}