Dire Vorrei generare un int casuale sicura tra 0 e 27 con:Come posso generare un int random usando il pacchetto "crypto/rand"?
func Int(rand io.Reader, max *big.Int) (n *big.Int, err error)
nel pacchetto "crypto/rand"
.
Come faccio?
Non ho veramente capito come funziona, perché non restituisce uno dei Go in built invece del puntatore ad un tipo big.Int?
EDIT:
Sarebbe questo essere considerato abbastanza sicuro per i token?
func getToken(length int) string {
token := ""
codeAlphabet := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
codeAlphabet += "abcdefghijklmnopqrstuvwxyz"
codeAlphabet += ""
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))
}
Questa uscita sarebbe qualcosa di simile:
qZDbuPwNQGrgVmZCU9A7FUWbp8eIfn0Z
EwZVoQ5D5SEfdhiRsDfH6dU6tAovILCZ
cOqzODVP0GwbiNBwtmqLA78rFgV9d3VT
Hai davvero bisogno di un "numero pseudocasuale crittograficamente sicuro"? (dal doc https://godoc.org/crypto/rand) Se non lo fai, puoi usare https://godoc.org/math/rand – HectorJ
Ho bisogno di generare token sicuri. Come quello che ho mostrato sopra. Mi piacerebbe se potesse includere tutti i caratteri dell'alfabeto, compresi i numeri. – Aiden
Hai appena visto la tua modifica. Un modo alternativo potrebbe essere quello di generare un int molto casuale (come tra 0 e il massimo int64) e codificarlo in esadecimale o base32. Lo aggiungerò alla mia risposta – HectorJ