-
Notifications
You must be signed in to change notification settings - Fork 0
/
otp.go
55 lines (43 loc) · 1.09 KB
/
otp.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
package otp
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base32"
"fmt"
"math"
"time"
)
type Config struct {
Secret string
TimeInterval int64
Digits int
Algorithm string
}
type Options struct {
Issuer string
AccountName string
}
func New(config Config) *Config {
return &config
}
func (otp *Config) Generate(options Options) (string, error) {
key, err := base32.StdEncoding.DecodeString(otp.Secret)
if err != nil {
return "", err
}
timeCount := time.Now().Unix() / otp.TimeInterval
timeBytes := make([]byte, 8)
for i := 0; i < 8; i++ {
timeBytes[7-i] = byte(timeCount >> (i * 8))
}
h := hmac.New(sha1.New, key)
h.Write(timeBytes)
hmacResult := h.Sum(nil)
offset := hmacResult[len(hmacResult)-1] & 0x0F
code := (int(hmacResult[offset])&0x7F) << 24 |
(int(hmacResult[offset+1]) & 0xFF) << 16 |
(int(hmacResult[offset+2]) & 0xFF) << 8 |
(int(hmacResult[offset+3]) & 0xFF)
otpValue := code % int(math.Pow(10, float64(otp.Digits)))
return fmt.Sprintf(fmt.Sprintf("%%0%d.d", otp.Digits), otpValue), nil
}