Files
SnowcloakUtils/SnowcloakUtils.go

27 lines
734 B
Go

package SnowcloakUtils
import (
"crypto/rand" // Probably better than math/rand for this
"log"
)
func GenerateRandomString(length int) string {
// C# had optional parameters that allowed lowercase for chardata and gpose lobbies, Go doesn't.
// We can probably get away with just uppercase.
allowedArray := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
random := make([]byte, length)
_, err := rand.Read(random)
if err != nil {
log.Fatalf("Couldn't generate secure slice: %s", err)
}
allowedLength := len(allowedArray)
i := 0
charArray := make([]rune, length)
for i < length {
charArray[i] = allowedArray[int(random[i])%allowedLength] // Casting random[i] due to type mismatch
i++
}
return string(charArray)
}