随机密码生成 by Golang 2020-01-16 go 次阅读 文章目录 Talk is too slow Show me the code 【注意】最后更新于 January 16, 2020,文中内容可能已过时,请谨慎使用。 Talk is too slow Show me the code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 package main import ( "crypto/md5" "encoding/hex" "fmt" "math/rand" "time" ) // 生成32位MD5 func MD5(text string) string { ctx := md5.New() ctx.Write([]byte(text)) return hex.EncodeToString(ctx.Sum(nil)) } // return len=8 salt func GetRandomSalt() string { return GetRandomString(32) } //生成随机字符串 func GetRandomString(len int) string { str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" bytes := []byte(str) result := []byte{} r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < len; i++ { result = append(result, bytes[r.Intn(62)]) } return string(result) } //生成随机字符串小写 func GetRandomStringLow(l int) string { str := "0123456789abcdefghijklmnopqrstuvwxyz" 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 main() { fmt.Println(GetRandomStringLow(10)) fmt.Println(GetRandomString(10)) } OUTPUT 1 2 3 4 5 [Running] go run "/opt/gopath/src/test/golang/pwd/random.go" 58k92996a7 LXNppqnT0Y [Done] exited with code=0 in 0.473 second 文章作者 lixueping 上次更新 2020-01-16 赞赏支持 微信打赏 支付宝打赏