-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.go
More file actions
33 lines (29 loc) · 679 Bytes
/
hash.go
File metadata and controls
33 lines (29 loc) · 679 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
package hash
import (
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
)
// get file md5
func FileMd5(filename string) (string, error) {
file, err := os.Open(filename)
if err != nil {
return "", errors.New(
fmt.Sprintf("md5.go hash.FileMd5 os open error %v", err))
}
h := md5.New()
_, err = io.Copy(h, file)
if err != nil {
return "", errors.New(fmt.Sprintf("md5.go hash.FileMd5 io copy error %v", err))
}
return hex.EncodeToString(h.Sum(nil)), nil
}
// get string md5
func StringMd5(s string) string {
md5 := md5.New()
md5.Write([]byte(s))
return hex.EncodeToString(md5.Sum(nil))
}