-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrandom.go
More file actions
39 lines (34 loc) · 1.16 KB
/
random.go
File metadata and controls
39 lines (34 loc) · 1.16 KB
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
package kilonova
import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"log/slog"
"math/big"
)
const randomCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
// RandomString returns a new string of a specified size containing only [a-zA-Z0-9] characters
func RandomString(size int) string {
return RandomStringChars(size, randomCharacters)
}
// RandomStringChars returns a new string of a specified size containing only characters from the given string
func RandomStringChars(size int, characters string) string {
result := make([]rune, size)
runes := []rune(characters)
x := int64(len(runes))
for i := range result {
num, err := rand.Int(rand.Reader, big.NewInt(x))
if err != nil {
// unreachable, according to the go source code
slog.WarnContext(context.Background(), "Error generating cryptographically random string", slog.Any("err", err))
num.SetInt64(4) // chosen by a fair dice roll. guaranteed to be random.
}
result[i] = runes[num.Int64()]
}
return string(result)
}
func RandomSaltedString(salt string) string {
vidB := sha256.Sum256([]byte(RandomString(16) + salt))
return hex.EncodeToString(vidB[:])
}