2018-10-15 17:41:41 -04:00
|
|
|
package id
|
|
|
|
|
|
|
|
|
|
import (
|
2020-09-22 22:14:10 +00:00
|
|
|
"crypto/rand"
|
2021-03-30 12:44:22 -04:00
|
|
|
"fmt"
|
2018-10-15 17:41:41 -04:00
|
|
|
)
|
|
|
|
|
|
2020-09-22 22:14:10 +00:00
|
|
|
// GenerateRandomString creates a random string of characters of the given
|
|
|
|
|
// length from the given dictionary of possible characters.
|
|
|
|
|
//
|
|
|
|
|
// This example generates a hexadecimal string 6 characters long:
|
|
|
|
|
// GenerateRandomString("0123456789abcdef", 6)
|
|
|
|
|
func GenerateRandomString(dictionary string, l int) string {
|
|
|
|
|
var bytes = make([]byte, l)
|
|
|
|
|
rand.Read(bytes)
|
|
|
|
|
for k, v := range bytes {
|
|
|
|
|
bytes[k] = dictionary[v%byte(len(dictionary))]
|
|
|
|
|
}
|
|
|
|
|
return string(bytes)
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-15 17:41:41 -04:00
|
|
|
// GenSafeUniqueSlug generatees a reasonably unique random slug from the given
|
|
|
|
|
// original slug. It's "safe" because it uses 0-9 b-z excluding vowels.
|
|
|
|
|
func GenSafeUniqueSlug(slug string) string {
|
2020-09-22 22:14:10 +00:00
|
|
|
return fmt.Sprintf("%s-%s", slug, GenerateRandomString("0123456789bcdfghjklmnpqrstvwxyz", 4))
|
2018-10-15 17:41:41 -04:00
|
|
|
}
|
2021-03-30 12:44:22 -04:00
|
|
|
|
|
|
|
|
// Generate62RandomString creates a random string with the given length
|
|
|
|
|
// consisting of characters in [A-Za-z0-9].
|
|
|
|
|
func Generate62RandomString(l int) string {
|
|
|
|
|
return GenerateRandomString("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", l)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GenerateFriendlyRandomString creates a random string of characters with the
|
|
|
|
|
// given length consisting of characters in [a-z0-9].
|
|
|
|
|
func GenerateFriendlyRandomString(l int) string {
|
|
|
|
|
return GenerateRandomString("0123456789abcdefghijklmnopqrstuvwxyz", l)
|
|
|
|
|
}
|