generateRandomString generates a cryptographically random, alphanumeric string of length n.
(n int)
| 75 | |
| 76 | // generateRandomString generates a cryptographically random, alphanumeric string of length n. |
| 77 | func generateRandomString(n int) (string, error) { |
| 78 | const dictionary = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
| 79 | var bytes = make([]byte, n) |
| 80 | |
| 81 | if _, err := rand.Read(bytes); err != nil { |
| 82 | return "", err |
| 83 | } |
| 84 | for k, v := range bytes { |
| 85 | bytes[k] = dictionary[v%byte(len(dictionary))] |
| 86 | } |
| 87 | |
| 88 | return string(bytes), nil |
| 89 | } |
| 90 | |
| 91 | // strHasLen checks if the given string has a length within min-max. |
| 92 | func strHasLen(str string, min, max int) bool { |
no outgoing calls
no test coverage detected