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.
47 lines
1.3 KiB
47 lines
1.3 KiB
package rand
|
|
|
|
import (
|
|
"bytes"
|
|
"math/rand"
|
|
)
|
|
|
|
func (rr *Rand) randomGetSomeChar(str string, l int) string {
|
|
bytes := []byte(str)
|
|
var result []byte
|
|
//r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
for i := 0; i < l; i++ {
|
|
//result = append(result, bytes[r.Intn(len(bytes))])
|
|
result = append(result, bytes[rand.Intn(len(bytes))])
|
|
}
|
|
return string(result)
|
|
}
|
|
|
|
// 长度选填 默认32位
|
|
func (rr *Rand) GetPassword(length ...int) string {
|
|
baseNum := "023456789"
|
|
baseCharLow := "abcdefghijkmnpqrstuvwxyz"
|
|
baseCharHigh := "ABCDEFGHJKLMNOPQRSTUVWXYZ"
|
|
baseSignSpec := "_"
|
|
str := "023456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ_"
|
|
var stringBuilder bytes.Buffer
|
|
lenPassword := 32
|
|
if len(length) > 0 {
|
|
lenPassword = length[0]
|
|
}
|
|
if lenPassword < 5 {
|
|
lenPassword = 5
|
|
}
|
|
stringBuilder.WriteString(rr.randomGetSomeChar(baseSignSpec, 1))
|
|
stringBuilder.WriteString(rr.randomGetSomeChar(baseNum, 1))
|
|
stringBuilder.WriteString(rr.randomGetSomeChar(baseCharLow, 1))
|
|
stringBuilder.WriteString(rr.randomGetSomeChar(baseCharHigh, 1))
|
|
stringBuilder.WriteString(rr.randomGetSomeChar(str, lenPassword-4))
|
|
strResult := stringBuilder.String()
|
|
arrResult := []byte(strResult)
|
|
for i := 0; i < 4; i++ {
|
|
num := rand.Intn(lenPassword - 1)
|
|
arrResult[i], arrResult[num] = arrResult[num], arrResult[i]
|
|
}
|
|
return string(arrResult)
|
|
}
|