-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.go
More file actions
41 lines (35 loc) · 711 Bytes
/
hash.go
File metadata and controls
41 lines (35 loc) · 711 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package rose
import (
"crypto/md5"
"encoding/hex"
"io"
)
func Md5HashStr(s string) string {
hashMd5 := md5.New()
hashMd5.Write([]byte(s))
return hex.EncodeToString(hashMd5.Sum(nil))
}
func Md5HashJoinStr(s ...string) string {
str := StrJoin(s...)
return Md5HashStr(str)
}
func Md5HashBuf(buf []byte) string {
hashMd5 := md5.New()
hashMd5.Write(buf)
return hex.EncodeToString(hashMd5.Sum(nil))
}
func Md5HashFile(reader io.Reader) string {
var buf = make([]byte, 4096)
hashMd5 := md5.New()
for {
n, err := reader.Read(buf)
if err == io.EOF && n == 0 {
break
}
if err != nil && err != io.EOF {
break
}
hashMd5.Write(buf[:n])
}
return hex.EncodeToString(hashMd5.Sum(nil))
}