-
Notifications
You must be signed in to change notification settings - Fork 1
/
hash.go
67 lines (62 loc) · 1.37 KB
/
hash.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package function
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"hash"
"io"
"io/ioutil"
"os"
)
// HashHmac 使用HMAC方法生成键控哈希值[hash_hmac]
func HashHmac(algo, msg, key string) string {
var m hash.Hash
if algo == "md5" {
m = hmac.New(md5.New, []byte(key))
} else if algo == "sha1" {
m = hmac.New(sha1.New, []byte(key))
} else if algo == "sha256" {
m = hmac.New(sha256.New, []byte(key))
} else if algo == "sha512" {
m = hmac.New(sha512.New, []byte(key))
} else {
m = hmac.New(sha256.New, []byte(key))
}
m.Write([]byte(msg))
return hex.EncodeToString(m.Sum(nil))
}
// HashFile 文件哈希[优先使用]
func HashFile(path string) (string, error) {
buf, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
h := sha256.Sum256(buf)
return hex.EncodeToString(h[:]), nil
}
// HashFileToMd5 文件哈希[MD5]
func HashFileToMd5(path string) (string, error) {
buf, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
h := md5.Sum(buf)
return hex.EncodeToString(h[:]), nil
}
// hashFile 文件哈希[不推荐使用]
func hashFile(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
hob := sha256.New()
_, err = io.Copy(hob, file)
if err != nil {
return "", err
}
return hex.EncodeToString(hob.Sum(nil)), nil
}