|
|
@ -3,6 +3,8 @@ package utils |
|
|
|
import ( |
|
|
|
"crypto/md5" |
|
|
|
"encoding/hex" |
|
|
|
"io" |
|
|
|
"os" |
|
|
|
) |
|
|
|
|
|
|
|
func MD5Hash(s string) string { |
|
|
@ -16,18 +18,32 @@ func MD5HashExt(s string, seps ...int) (string, []string) { |
|
|
|
hasher.Write([]byte(s)) |
|
|
|
ss := []rune(hex.EncodeToString(hasher.Sum(nil))) |
|
|
|
var ret []string |
|
|
|
if len(seps) >0 { |
|
|
|
if len(seps) > 0 { |
|
|
|
var pos = 0 |
|
|
|
for _, x := range seps { |
|
|
|
if (pos+x) > len(ss) { |
|
|
|
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)) { |
|
|
|
if pos < (len(ss)) { |
|
|
|
ret = append(ret, string(ss[pos:])) |
|
|
|
} |
|
|
|
} |
|
|
|
return string(ss), ret |
|
|
|
} |
|
|
|
|
|
|
|
func FileMD5(filename string) (string, error) { |
|
|
|
if f, e := os.Open(filename); nil != e { |
|
|
|
return "", e |
|
|
|
} else { |
|
|
|
defer f.Close() |
|
|
|
h := md5.New() |
|
|
|
if _, e := io.Copy(h, f); nil != e { |
|
|
|
return "", e |
|
|
|
} else { |
|
|
|
return hex.EncodeToString(h.Sum(nil)), nil |
|
|
|
} |
|
|
|
} |
|
|
|
} |