mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 20:10:56 +00:00
21 lines
474 B
Go
21 lines
474 B
Go
package utils
|
|
|
|
import (
|
|
"crypto/rand"
|
|
)
|
|
|
|
// RandomCreateBytes generate random []byte by specify chars.
|
|
func RandomCreateBytes(n int, alphabets ...byte) []byte {
|
|
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
var bytes = make([]byte, n)
|
|
rand.Read(bytes)
|
|
for i, b := range bytes {
|
|
if len(alphabets) == 0 {
|
|
bytes[i] = alphanum[b%byte(len(alphanum))]
|
|
} else {
|
|
bytes[i] = alphabets[b%byte(len(alphabets))]
|
|
}
|
|
}
|
|
return bytes
|
|
}
|