-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.go
38 lines (34 loc) · 901 Bytes
/
misc.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
package googleauth
import (
"crypto/rand"
"encoding/base32"
)
const alphanum string = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
const alphanumBase32 string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
func GenerateRandomSecret(size int, encodeToBase32 bool) string {
var bytes = make([]byte, size)
rand.Read(bytes)
if encodeToBase32 {
for i, b := range bytes {
bytes[i] = alphanum[b%byte(len(alphanum))]
}
return base32.StdEncoding.EncodeToString(bytes)
} else {
for i, b := range bytes {
bytes[i] = alphanumBase32[b%byte(len(alphanumBase32))]
}
}
return string(bytes)
}
func ValidSecret(secret string, size int, encodeToBase32 bool) bool {
if encodeToBase32 {
if _, err := base32.StdEncoding.DecodeString(secret); err != nil {
return false
}
} else if len(secret) != size {
return false
} else {
//TODO: check alphanumBase32
}
return true
}