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