From a6778b0d3039f3077851292d929bc742cdb15350 Mon Sep 17 00:00:00 2001 From: Mingcai SHEN Date: Wed, 27 Jun 2018 11:00:17 +0800 Subject: [PATCH] Add MD5 Hash string. --- utils/md5.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 utils/md5.go diff --git a/utils/md5.go b/utils/md5.go new file mode 100644 index 0000000..fded903 --- /dev/null +++ b/utils/md5.go @@ -0,0 +1,33 @@ +package utils + +import ( + "crypto/md5" + "encoding/hex" +) + +func MD5Hash(s string) string { + hasher := md5.New() + hasher.Write([]byte(s)) + return hex.EncodeToString(hasher.Sum(nil)) +} + +func MD5HashExt(s string, seps ...int) (string, []string) { + hasher := md5.New() + hasher.Write([]byte(s)) + ss := []rune(hex.EncodeToString(hasher.Sum(nil))) + var ret []string + if len(seps) >0 { + var pos = 0 + for _, x := range seps { + if (pos+x) > len(ss) { + panic("overload length of hex string length") + } + ret = append(ret, string(ss[pos:pos+x])) + pos += x + } + if pos<(len(ss)) { + ret = append(ret, string(ss[pos:])) + } + } + return string(ss), ret +}