-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
89 lines (76 loc) · 1.51 KB
/
utils.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
package main
import (
"crypto/rand"
"crypto/sha512"
"encoding/base32"
"encoding/base64"
"encoding/hex"
"fmt"
mathRandom "math/rand"
"net/url"
"time"
)
var noPadding rune = -1
func isValidChallenge(challenge string) bool {
return supportedChallengesMap[challenge]
}
func hashHex(s string) string {
h := sha512.New()
h.Write([]byte(s))
return hex.EncodeToString(h.Sum(nil))
}
func base64Hash(s string) string {
h := sha512.New()
h.Write([]byte(s))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
func randomBase64() string {
b := make([]byte, 512)
_, err := rand.Read(b)
if err != nil {
panic(err.Error())
}
hash := sha512.New()
hash.Write(b)
return base64.StdEncoding.EncodeToString(hash.Sum(nil))
}
func minMaxDefault64(value int64, min int64, max int64) int64 {
if value < min {
return min
}
if value > max {
return max
}
return value
}
func minMaxDefault(value int, min int, max int) int {
if value < min {
return min
}
if value > max {
return max
}
return value
}
func totpGenerate() string {
b := make([]byte, 512)
_, err := rand.Read(b)
if err != nil {
panic(err.Error())
}
hash := sha512.New()
hash.Write(b)
enc := base32.StdEncoding.WithPadding(noPadding)
return enc.EncodeToString(hash.Sum(nil))[0:16]
}
func getURLPath(u *url.URL) string {
if u.RawQuery != "" {
return fmt.Sprintf("%s?%s", u.Path, u.RawQuery)
}
return u.Path
}
func getHumanEmoji() string {
mathRandom.Seed(time.Now().Unix())
i := mathRandom.Int() % len(humanEmojis)
return humanEmojis[i]
}