forked from Davincible/goinsta
/
totp.go
67 lines (56 loc) · 1.44 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
60
61
62
63
64
65
66
67
package utilities
import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"encoding/base32"
"encoding/binary"
"strconv"
"strings"
"time"
)
// prefix will append extra 0s if the length of otp is less than 6.
func prefix(otp string) string {
if len(otp) == 6 {
return otp
}
for i := (6 - len(otp)); i > 0; i-- {
otp = "0" + otp
}
return otp
}
// genHOTP will generate a Hmac One Time Password
func genHOTP(secret string, interval int64) (string, error) {
// Decode base32 secret
key, err := base32.StdEncoding.DecodeString(strings.ToUpper(secret))
if err != nil {
return "", err
}
bs := make([]byte, 8)
binary.BigEndian.PutUint64(bs, uint64(interval))
// Signing the value using HMAC-SHA1 Algorithm
hash := hmac.New(sha1.New, key)
hash.Write(bs)
h := hash.Sum(nil)
// Grab offset
o := (h[19] & 15)
// Get 32 bit chunk from hash starting at the o
var header uint32
r := bytes.NewReader(h[o : o+4])
err = binary.Read(r, binary.BigEndian, &header)
if err != nil {
return "", err
}
// Ignore most significant bits as per RFC 4226, and crop to 6 digits.
h12 := (int(header) & 0x7fffffff) % 1000000
otp := strconv.Itoa(int(h12))
otp = prefix(otp)
return otp, nil
}
// GenTOTP will generate a one time pass based on the secret.
func GenTOTP(secret string) (string, error) {
//The TOTP token is just a HOTP token seeded with every 30 seconds.
interval := time.Now().Unix() / 30
otp, err := genHOTP(secret, interval)
return otp, err
}