Created
January 16, 2020 15:15
-
-
Save betandr/6f5137d2fd7cad557ed7b82d55c48078 to your computer and use it in GitHub Desktop.
Generate keys with crypto/rand
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Do not use package math/rand to generate keys, even throwaway ones. | |
// Unseeded, the generator is completely predictable. Seeded with | |
// time.Nanoseconds(), there are just a few bits of entropy. Instead, | |
// use crypto/rand's Reader, and if you need text, print to hexadecimal | |
// or base64. | |
package main | |
import ( | |
"crypto/rand" | |
"fmt" | |
) | |
func main() { | |
buf := make([]byte, 16) | |
_, err := rand.Read(buf) | |
if err != nil { | |
panic(err) // out of randomness, should never happen | |
} | |
fmt.Printf("%x\n", buf) | |
// or hex.EncodeToString(buf) | |
// or base64.StdEncoding.EncodeToString(buf) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment