mirror of
https://gitee.com/konyshe/goodlink.git
synced 2025-09-26 20:51:22 +08:00
21 lines
413 B
Go
21 lines
413 B
Go
package utils
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"math/big"
|
|
)
|
|
|
|
func RandomBytes(length int) []byte {
|
|
bytes := make([]byte, length)
|
|
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
for i := 0; i < length; {
|
|
bint, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
bytes[i] = charset[bint.Int64()]
|
|
i++
|
|
}
|
|
return bytes
|
|
}
|