-
Notifications
You must be signed in to change notification settings - Fork 0
/
totp.go
45 lines (36 loc) · 922 Bytes
/
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
package totp
type Decoder interface {
DecodeString(s string) ([]byte, error)
}
type DecoderFunc func(s string) ([]byte, error)
func (d DecoderFunc) DecodeString(s string) ([]byte, error) {
return d(s)
}
func SimpleDecode(s string) ([]byte, error) {
return []byte(s), nil
}
type ErrorDecodeSecret error
type ErrorDecodeTimestamp error
type Generator struct {
// HMACHashAlgorithm allows sha1, sha256, sha512
HMACHashAlgorithm string
Secret []byte
PeriodSeconds int64
Digits int
}
func NewGenerator(alg string, period, digits int, secret []byte) *Generator {
return &Generator{
HMACHashAlgorithm: alg,
Secret: secret,
PeriodSeconds: int64(period),
Digits: digits,
}
}
func (o *Generator) SetSecretString(decoder Decoder, secret string) error {
s, err := decoder.DecodeString(secret)
if err != nil {
return err
}
o.Secret = s
return nil
}