diff options
author | KushBlazingJudah <59340248+KushBlazingJudah@users.noreply.github.com> | 2021-10-30 00:28:51 -0300 |
---|---|---|
committer | FChannel <> | 2022-06-19 12:53:29 -0700 |
commit | 8040825daac4a5ba24e947c4c657d169d7763543 (patch) | |
tree | 667058707ab1c2b2af5dc01decda25cc7709f9df /util | |
parent | f76b8093b9ca69c7d4eca747d8c5d702d492e1fb (diff) |
restructuring, part 3 of many
Diffstat (limited to 'util')
-rw-r--r-- | util/key.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/util/key.go b/util/key.go new file mode 100644 index 0000000..458d7c0 --- /dev/null +++ b/util/key.go @@ -0,0 +1,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() +} |