Golang生成隨機數和MD5加密
阿新 • • 發佈:2019-02-14
import (
"crypto/md5"
"encoding/hex"
"math/rand"
"time"
)
//生成隨機字串 func GetRandomString(length int) string{ str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" bytes := []byte(str) result := []byte{} r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < length; i++ { result = append(result, bytes[r.Intn(len(bytes))]) } return string(result) }
// 生成32位MD5
func MD5(text string) string{
ctx := md5.New()
ctx.Write([]byte(text))
return hex.EncodeToString(ctx.Sum(nil))
}