mirror of
https://github.com/abhinavxd/libredesk.git
synced 2025-11-04 14:03:19 +00:00
41 lines
870 B
Go
41 lines
870 B
Go
// Package stringutil provides string utility functions.
|
|
package stringutil
|
|
|
|
import (
|
|
"crypto/rand"
|
|
)
|
|
|
|
// RandomAlNumString generates a random alphanumeric string of length n.
|
|
func RandomAlNumString(n int) (string, error) {
|
|
const dictionary = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
bytes := make([]byte, n)
|
|
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
for k, v := range bytes {
|
|
bytes[k] = dictionary[v%byte(len(dictionary))]
|
|
}
|
|
|
|
return string(bytes), nil
|
|
}
|
|
|
|
// RandomNumericString generates a random numeric string of length n.
|
|
func RandomNumericString(n int) (string, error) {
|
|
const dictionary = "0123456789"
|
|
|
|
bytes := make([]byte, n)
|
|
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
for k, v := range bytes {
|
|
bytes[k] = dictionary[v%byte(len(dictionary))]
|
|
}
|
|
|
|
return string(bytes), nil
|
|
}
|