低空智控平台 后端go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

35 lines
698 B

package tool
import (
"crypto/rand"
"math/big"
)
const (
letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
digits = "0123456789"
)
// RandString 生成指定长度的随机字母数字串
func RandString(n int) string {
return randFrom(letters, n)
}
// RandDigit 生成指定长度的随机数字串(短信验证码等)
func RandDigit(n int) string {
return randFrom(digits, n)
}
func randFrom(charset string, n int) string {
b := make([]byte, n)
max := big.NewInt(int64(len(charset)))
for i := range b {
idx, err := rand.Int(rand.Reader, max)
if err != nil {
b[i] = charset[0]
continue
}
b[i] = charset[idx.Int64()]
}
return string(b)
}