mirror of
https://github.com/Mrs4s/MiraiGo.git
synced 2025-05-04 11:07:40 +08:00
24 lines
490 B
Go
24 lines
490 B
Go
package utils
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/rand"
|
|
"math/big"
|
|
)
|
|
|
|
func RandomString(len int) string {
|
|
return RandomStringRange(len, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
|
|
}
|
|
|
|
func RandomStringRange(len int, str string) string {
|
|
var res string
|
|
b := bytes.NewBufferString(str)
|
|
length := b.Len()
|
|
bigInt := big.NewInt(int64(length))
|
|
for i := 0; i < len; i++ {
|
|
randomInt, _ := rand.Int(rand.Reader, bigInt)
|
|
res += string(str[randomInt.Int64()])
|
|
}
|
|
return res
|
|
}
|