Say I would like to generate a secure random int between 0 and 27 using:
func Int(rand io.Reader, max *big.Int) (n *big.Int, err error) in the "crypto/rand" package.
How would I do that?
I do not really understand how this works, why does it not return one of the built in Go ints instead of pointer to some big.Int type?
EDIT:
Would this be considered secure enough for tokens?
func getToken(length int) string { token := "" codeAlphabet := "ABCDEFGHIJKLMNOPQRSTUVWXYZ" codeAlphabet += "abcdefghijklmnopqrstuvwxyz" codeAlphabet += "0123456789" for i := 0; i < length; i++ { token += string(codeAlphabet[cryptoRandSecure(int64(len(codeAlphabet)))]) } return token } func cryptoRandSecure(max int64) int64 { nBig, err := rand.Int(rand.Reader, big.NewInt(max)) if err != nil { log.Println(err) } return nBig.Int64() } func main() { fmt.Println(getToken(32)) } This would output something like this:
qZDbuPwNQGrgVmZCU9A7FUWbp8eIfn0Z EwZVoQ5D5SEfdhiRsDfH6dU6tAovILCZ cOqzODVP0GwbiNBwtmqLA78rFgV9d3VT