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.
42 lines
1.2 KiB
42 lines
1.2 KiB
package rand
|
|
|
|
import (
|
|
"bytes"
|
|
"math/rand"
|
|
"time"
|
|
)
|
|
|
|
func (rr *Rand) randomGetSomeChar(str string, l int) string {
|
|
bytes := []byte(str)
|
|
result := []byte{}
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
for i := 0; i < l; i++ {
|
|
result = append(result, bytes[r.Intn(len(bytes))])
|
|
}
|
|
return string(result)
|
|
}
|
|
|
|
func (r *Rand) GetPassword(len int) string {
|
|
base_num := "023456789"
|
|
base_char_low := "abcdefghijkmnpqrstuvwxyz"
|
|
base_char_high := "ABCDEFGHJKLMNOPQRSTUVWXYZ"
|
|
base_sign_spec := "_"
|
|
str := "023456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ_"
|
|
var stringBuilder bytes.Buffer
|
|
if len < 5 {
|
|
len = 5
|
|
}
|
|
stringBuilder.WriteString(r.randomGetSomeChar(base_sign_spec, 1))
|
|
stringBuilder.WriteString(r.randomGetSomeChar(base_num, 1))
|
|
stringBuilder.WriteString(r.randomGetSomeChar(base_char_low, 1))
|
|
stringBuilder.WriteString(r.randomGetSomeChar(base_char_high, 1))
|
|
stringBuilder.WriteString(r.randomGetSomeChar(str, len-4))
|
|
str_result := stringBuilder.String()
|
|
arr_result := []byte(str_result)
|
|
for i := 0; i < 4; i++ {
|
|
num := rand.Intn(len - 1)
|
|
arr_result[i], arr_result[num] = arr_result[num], arr_result[i]
|
|
}
|
|
return string(arr_result)
|
|
}
|