wireguard-dashboard/utils/rand.go
2024-08-09 11:45:32 +08:00

32 lines
596 B
Go

package utils
import (
"bytes"
"crypto/rand"
"math/big"
)
type random struct{}
func Random() random {
return random{}
}
// RandStr
// @description: 随机字符串
// @receiver random
// @param len
// @return string
func (random) RandStr(len int) string {
var container string
var str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
b := bytes.NewBufferString(str)
length := b.Len()
bigInt := big.NewInt(int64(length))
for i := 0; i < len; i++ {
randomInt, _ := rand.Int(rand.Reader, bigInt)
container += string(str[randomInt.Int64()])
}
return container
}