From e490a5a8ba8cdd6bed60aaa746a5a1e35b81c44f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Tue, 9 Mar 2021 18:02:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96=E9=9A=8F?= =?UTF-8?q?=E6=9C=BA=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=8Emd5=E5=8A=A0?= =?UTF-8?q?=E5=AF=86=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/string.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 util/string.go diff --git a/util/string.go b/util/string.go new file mode 100644 index 0000000..ae4cde5 --- /dev/null +++ b/util/string.go @@ -0,0 +1,48 @@ +// Package util... +// +// Description : 字符串相关的工具 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-03-09 6:00 下午 +package util + +import ( + "crypto/md5" + "encoding/hex" + "math/rand" + "time" +) + +// GenRandomString 获取随机长度的字符串 +// +// Author : go_developer@163.com<张德满> +// +// Date : 6:01 下午 2021/3/9 +func GenRandomString(source string, length uint) string { + if length == 0 { + return "" + } + if len(source) == 0 { + //字符串为空,默认字符源为如下: + source = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + } + strByte := []byte(source) + var genStrByte = make([]byte, 0) + r := rand.New(rand.NewSource(time.Now().UnixNano())) + for i := 0; i < int(length); i++ { + genStrByte = append(genStrByte, strByte[r.Intn(len(strByte))]) + } + return string(genStrByte) +} + +// Md5 对字符串进行md5加密 +// +// Author : go_developer@163.com<张德满> +// +// Date : 6:01 下午 2021/3/9 +func Md5(str string) string { + h := md5.New() + h.Write([]byte(str)) + return hex.EncodeToString(h.Sum(nil)) +}