import(
"math/rand"
)
//随机字符串 Model:字母数字符号
/*
Model 110 必须包含字母数字
Model 111 必须包含字母数字和符号
Model 210 必须包含大写字母,小写字母及数字
Model 211 必须包含大写字母,小写字母及数字及符号
Model 321 字母数字符号三选二
Model 999 字母数字符号随机
Model else 字母数字随机
*/
func MakeRandomString(Length int64, Model string) string {
AlphaLowCaseStr := "abcdefghijklmnopqrstuvwxyz"
AlphaUpCaseStr := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
AlphaStr := AlphaLowCaseStr + AlphaUpCaseStr
NumStr := "0123456789"
SymbolStr := "`~!@#$%^&*()_+-=[]{}|\\:;\"'<>/?"
var AlphaUpCaseLen, AlphaLowCaseLen, AlphaLen, NumLen, SymbolLen int64
var str string
result := []byte{}
if Length == 0 {
return ""
}
if Model == "110" { //必须包含字母数字
if Length > 1 {
AlphaLen = RandInt64(1, Length-1)
NumLen = Length - AlphaLen
} else { //长度小于等于1
var letterBytes = AlphaStr + NumStr
b := make([]byte, Length)
for i := range b {
b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]
}
return string(b)
}
} else if Model == "111" { //必须包含字母数字和符号
if Length > 2 {
AlphaLen = RandInt64(1, Length-2)
NumLen = RandInt64(1, AlphaLen)
SymbolLen = Length - AlphaLen - NumLen
} else { //长度小于等于2
var letterBytes = AlphaStr + NumStr + SymbolStr
b := make([]byte, Length)
for i := range b {
b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]
}
return string(b)
}
} else if Model == "210" { //必须包含大写字母,小写字母及数字
if Length > 2 {
AlphaUpCaseLen = RandInt64(1, Length-2)
AlphaLowCaseLen = RandInt64(1, AlphaUpCaseLen)
NumLen = Length - AlphaUpCaseLen - AlphaLowCaseLen
} else { //长度小于等于2
var letterBytes = AlphaStr + NumStr
b := make([]byte, Length)
for i := range b {
b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]
}
return string(b)
}
} else if Model == "211" { //必须包含大写字母,小写字母及数字及符号
if Length > 3 {
AlphaUpCaseLen = RandInt64(1, Length-3)
AlphaLowCaseLen = RandInt64(1, Length-AlphaUpCaseLen-2)
NumLen = RandInt64(1, Length-AlphaUpCaseLen-AlphaLowCaseLen-1)
SymbolLen = Length - AlphaUpCaseLen - AlphaLowCaseLen - NumLen
} else { //长度小于等于3
var letterBytes = AlphaStr + NumStr + SymbolStr
b := make([]byte, Length)
for i := range b {
b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]
}
return string(b)
}
} else if Model == "321" { //字母数字符号三选二
if Length > 1 {
AlphaLen = RandInt64(0, Length-1)
if AlphaLen == 0 {
NumLen = RandInt64(1, Length-1) //Alpha没选,Num至少选一个,最多选L-1
} else {
NumLen = RandInt64(0, Length-AlphaLen) //Alpha选了,Num可以不选,最多就能选Alpha剩下的
}
SymbolLen = Length - AlphaLen - NumLen
} else {
var letterBytes = AlphaStr + NumStr + SymbolStr
b := make([]byte, Length)
for i := range b {
b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]
}
return string(b)
}
} else if Model == "999" { //其他: 字母数字符号随机
str = AlphaStr + NumStr + SymbolStr
Bytes := []byte(str)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := int64(0); i < Length; i++ {
result = append(result, Bytes[r.Intn(len(Bytes))])
}
return string(result)
} else { //其他:字母数字随机
str = AlphaStr + NumStr
Bytes := []byte(str)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := int64(0); i < Length; i++ {
result = append(result, Bytes[r.Intn(len(Bytes))])
}
return string(result)
}
if AlphaUpCaseLen != 0 {
AlphaUpCaseBytes := []byte(AlphaUpCaseStr)
AlphaUpCaseRand := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := int64(0); i < AlphaUpCaseLen; i++ {
result = append(result, AlphaUpCaseBytes[AlphaUpCaseRand.Intn(len(AlphaUpCaseBytes))])
}
}
if AlphaLowCaseLen != 0 {
AlphaLowCaseBytes := []byte(AlphaLowCaseStr)
AlphaLowCaseRand := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := int64(0); i < AlphaLowCaseLen; i++ {
result = append(result, AlphaLowCaseBytes[AlphaLowCaseRand.Intn(len(AlphaLowCaseBytes))])
}
}
if NumLen != 0 {
NumBytes := []byte(NumStr)
NumRand := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := int64(0); i < NumLen; i++ {
result = append(result, NumBytes[NumRand.Intn(len(NumBytes))])
}
}
if AlphaLen != 0 {
AlphaBytes := []byte(AlphaStr)
AlphaRand := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := int64(0); i < AlphaLen; i++ {
result = append(result, AlphaBytes[AlphaRand.Intn(len(AlphaBytes))])
}
}
if SymbolLen != 0 {
SymbolBytes := []byte(SymbolStr)
SymbolRand := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := int64(0); i < SymbolLen; i++ {
result = append(result, SymbolBytes[SymbolRand.Intn(len(SymbolBytes))])
}
}
for i := len(result) - 1; i > 0; i-- {
num := rand.Intn(i + 1)
result[i], result[num] = result[num], result[i]
}
return string(result)
}
//RandInt64 取两个数之间的随机数
func RandInt64(min, max int64) int64 {
//仅支持非负整数,且两端不能同时等于0,否则返回0
if min < 0 || max < 0 || (min == 0 && max == 0) {
return 0
}
if min >= max {
return rand.Int63n(min-max) + max
}
return rand.Int63n(max-min) + min
}
有疑问加站长微信联系(非本文作者)