SecureAlphanumeric generates a secure random alphanumeric string using standard library
(length int)
| 168 | |
| 169 | // SecureAlphanumeric generates a secure random alphanumeric string using standard library |
| 170 | func SecureAlphanumeric(length int) string { |
| 171 | if length < 8 { |
| 172 | length = 8 |
| 173 | } |
| 174 | |
| 175 | // Calculate bytes needed for desired length |
| 176 | // base32 encoding: 5 bytes -> 8 chars |
| 177 | numBytes := (length*5 + 7) / 8 |
| 178 | |
| 179 | b := make([]byte, numBytes) |
| 180 | must(io.ReadFull(rand.Reader, b)) |
| 181 | |
| 182 | // Use standard library's base32 without padding |
| 183 | return strings.ToLower(base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(b))[:length] |
| 184 | } |