xkcdpwgen/random.go
raw| 1 | package xkcdpwgen |
| 2 | |
| 3 | // from https://github.com/justwatchcom/gopass/blob/master/utils/pwgen/pwgen.go |
| 4 | |
| 5 | import ( |
| 6 | crand "crypto/rand" |
| 7 | "fmt" |
| 8 | "math/big" |
| 9 | "math/rand" |
| 10 | ) |
| 11 | |
| 12 | func randomInteger(max int) int { |
| 13 | i, err := crand.Int(crand.Reader, big.NewInt(int64(max))) |
| 14 | if err == nil { |
| 15 | return int(i.Int64()) |
| 16 | } |
| 17 | fmt.Println("WARNING: No crypto/rand available. Falling back to PRNG") |
| 18 | return rand.Intn(max) |
| 19 | } |