RandomStringWithAlphabet generates a cryptographically random string with the specified length and characters set. It panics if for some reason rand.Int returns a non-nil error.
(length int, alphabet string)
| 20 | // |
| 21 | // It panics if for some reason rand.Int returns a non-nil error. |
| 22 | func RandomStringWithAlphabet(length int, alphabet string) string { |
| 23 | b := make([]byte, length) |
| 24 | max := big.NewInt(int64(len(alphabet))) |
| 25 | |
| 26 | for i := range b { |
| 27 | n, err := cryptoRand.Int(cryptoRand.Reader, max) |
| 28 | if err != nil { |
| 29 | panic(err) |
| 30 | } |
| 31 | b[i] = alphabet[n.Int64()] |
| 32 | } |
| 33 | |
| 34 | return string(b) |
| 35 | } |
| 36 | |
| 37 | // PseudorandomString generates a pseudorandom string with the specified length. |
| 38 | // |
no outgoing calls
no test coverage detected
searching dependent graphs…