blob: 458d7c01413889970c518acf9edd03031f8cdd2e (
plain)
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
|
package util
import (
"crypto/sha512"
"encoding/hex"
"math/rand"
"strings"
)
const domain = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
func CreateKey(len int) string {
// TODO: provided that CreateTripCode still uses sha512, the max len can be 128 at most.
if len > 128 {
panic("len is greater than 128") // awful way to do it
}
str := CreateTripCode(RandomID(len))
return str[:len]
}
func CreateTripCode(input string) string {
out := sha512.Sum512([]byte(input))
return hex.EncodeToString(out[:])
}
func RandomID(size int) string {
rng := size
newID := strings.Builder{}
for i := 0; i < rng; i++ {
newID.WriteByte(domain[rand.Intn(len(domain))])
}
return newID.String()
}
|