Browse Source

Add MD5 Hash string.

develop
Mingcai SHEN 7 years ago
parent
commit
a6778b0d30
1 changed files with 33 additions and 0 deletions
  1. +33
    -0
      utils/md5.go

+ 33
- 0
utils/md5.go View File

@ -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
}

Loading…
Cancel
Save