StringFn create a random string for test purposes using the random number generator function passed in. Do not use these for passwords.
(n int, randReader io.Reader)
| 13 | // |
| 14 | // Do not use these for passwords. |
| 15 | func StringFn(n int, randReader io.Reader) string { |
| 16 | const ( |
| 17 | vowel = "aeiou" |
| 18 | consonant = "bcdfghjklmnpqrstvwxyz" |
| 19 | digit = "0123456789" |
| 20 | ) |
| 21 | var ( |
| 22 | pattern = []string{consonant, vowel, consonant, vowel, consonant, vowel, consonant, digit} |
| 23 | out = make([]byte, n) |
| 24 | p = 0 |
| 25 | ) |
| 26 | _, err := io.ReadFull(randReader, out) |
| 27 | if err != nil { |
| 28 | panic(fmt.Sprintf("internal error: failed to read from random reader: %v", err)) |
| 29 | } |
| 30 | for i := range out { |
| 31 | source := pattern[p] |
| 32 | p = (p + 1) % len(pattern) |
| 33 | // this generation method means the distribution is slightly biased. However these |
| 34 | // strings are not for passwords so this is deemed OK. |
| 35 | out[i] = source[out[i]%byte(len(source))] |
| 36 | } |
| 37 | return string(out) |
| 38 | } |
| 39 | |
| 40 | // String create a random string for test purposes. |
| 41 | // |