mercredi 17 octobre 2018

Is it good idea to generate random string until success with "crypto/rand"?

Is it a good idea to generate a secure random hex string until the process succeeds?

All examples I've come across show that if rand.Read returns error, we should panic, os.Exit(1) or return empty string and the error.

I need my program to continue to function in case of such errors and wait until a random string is generated. Is it a good idea to loop until the string is generated, any pitfalls with that?

func RandomHex() string {
    var buf [16]byte
    for {
        _, err := rand.Read(buf[:])
        if err == nil {
            break
        }
    }
    return hex.EncodeToString(buf[:])
}




Aucun commentaire:

Enregistrer un commentaire