forked from 928799934/googleAuthenticator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google.go
executable file
·113 lines (105 loc) · 2.58 KB
/
google.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*******************************************************
File Name: google.go
Author: An
Mail:[email protected]
Created Time: 14/11/26 - 10:25:26
Modify Time: 14/11/26 - 10:25:26
*******************************************************/
package googleAuthenticator
import (
"encoding/base32"
"encoding/hex"
"fmt"
"math"
"math/rand"
"strconv"
"strings"
"time"
)
type GAuth struct {
codeLen float64
table map[string]int
}
func NewGAuth() *GAuth {
rand.Seed(time.Now().UnixNano())
return &GAuth{
codeLen: 6,
table: arrayFlip(Table),
}
}
// SetCodeLength Set the code length, should be >=6
func (this *GAuth) SetCodeLength(length float64) error {
if length < 6 {
return ErrSecretLengthLss
}
this.codeLen = length
return nil
}
// CreateSecret create new secret
// 16 characters, randomly chosen from the allowed base32 characters.
func (this *GAuth) CreateSecret(lens ...int) (string, error) {
var (
length int
secret []string
)
// init length
switch len(lens) {
case 0:
length = 16
case 1:
length = lens[0]
default:
return "", ErrParam
}
for i := 0; i < length; i++ {
secret = append(secret, Table[rand.Intn(len(Table))])
}
return strings.Join(secret, ""), nil
}
// VerifyCode Check if the code is correct. This will accept codes starting from $discrepancy*30sec ago to $discrepancy*30sec from now
func (this *GAuth) VerifyCode(secret, code string, discrepancy int64) (bool, error) {
// now time
curTimeSlice := time.Now().Unix() / 30
for i := -discrepancy; i <= discrepancy; i++ {
calculatedCode, err := this.GetCode(secret, curTimeSlice+i)
if err != nil {
return false, err
}
if calculatedCode == code {
return true, nil
}
}
return false, nil
}
// GetCode Calculate the code, with given secret and point in time
func (this *GAuth) GetCode(secret string, timeSlices ...int64) (string, error) {
var timeSlice int64
switch len(timeSlices) {
case 0:
timeSlice = time.Now().Unix() / 30
case 1:
timeSlice = timeSlices[0]
default:
return "", ErrParam
}
secret = strings.ToUpper(secret)
secretKey, err := base32.StdEncoding.DecodeString(secret)
if err != nil {
return "", err
}
tim, err := hex.DecodeString(fmt.Sprintf("%016x", timeSlice))
if err != nil {
return "", err
}
hm := HmacSha1(secretKey, tim)
offset := hm[len(hm)-1] & 0x0F
hashpart := hm[offset : offset+4]
value, err := strconv.ParseInt(hex.EncodeToString(hashpart), 16, 0)
if err != nil {
return "", err
}
value = value & 0x7FFFFFFF
modulo := int64(math.Pow(10, this.codeLen))
format := fmt.Sprintf("%%0%dd", int(this.codeLen))
return fmt.Sprintf(format, value%modulo), nil
}