-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtotp.go
73 lines (62 loc) · 1.74 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
68
69
70
71
72
73
package totp
import (
"crypto/hmac"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/binary"
"math"
"time"
)
// TOTP struct where K is a byte array of user key, Digit how long the token and Window the time window in seconds
type TOTP struct {
K []byte
Digit int
Window uint64
WindowSize int
Algorithm string
}
// Validate check if the code is correct, return true or false and a map with its correspondent timestamp and PIN value.
func (t *TOTP) Validate(code uint64) (map[uint64]uint64, bool) {
//Verify all possible PINs inside the window and compare with each PIN.
codes := make(map[uint64]uint64)
ok := false
for count := 0; count <= t.WindowSize-1; count++ {
tryNum := -(t.WindowSize-1)/2 + count
timestamp := time.Now().Unix() + (int64(t.Window) * int64(tryNum))
//Verify reuse case based on stored timestamps.
// clock := uint64(time.Now().Unix() / t.Window)
clock := uint64(timestamp) / t.Window
C := make([]byte, 8)
binary.BigEndian.PutUint64(C, clock)
alg := sha1.New
switch t.Algorithm {
case "SHA256":
alg = sha256.New
case "SHA512":
alg = sha512.New
}
mac := hmac.New(alg, t.K)
mac.Write(C)
truncate := Truncate(mac.Sum(nil), t.Digit)
if truncate == code {
codes[clock] = code
ok = true
break
}
}
return codes, ok
}
// Truncate trunc hmac as the RFC says so
func Truncate(hmacres []byte, digits int) uint64 {
offset := uint64(hmacres[19] & 0xf)
binCode := uint64(int(hmacres[offset]&0x7f)<<24 | int(hmacres[offset+1]&0xff)<<16 | int(hmacres[offset+2]&0xff)<<8 | int(hmacres[offset+3]&0xff))
return uint64(binCode % uint64(math.Pow10(digits)))
}
// StdTOTP Standard TOTP
var StdTOTP = TOTP{
Window: 30,
Digit: 6,
WindowSize: 17,
Algorithm: "SHA1",
}