/
hash.go
64 lines (57 loc) · 1.13 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
package coding
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"hash"
"hash/crc32"
"hash/crc64"
)
func MD5(str string) (string, error) {
bytes, err := hashBytes("md5", str)
if err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}
func SHA1(str string) (string, error) {
bytes, err := hashBytes("sha1", str)
if err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}
func SHA2(str string) (string, error) {
bytes, err := hashBytes("sha2", str)
if err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}
func CRC32(str string) int {
tab := crc32.MakeTable(crc32.IEEE)
coded := crc32.Checksum([]byte(str), tab)
return int(coded)
}
func CRC64(str string) int64 {
tab := crc64.MakeTable(crc64.ISO)
coded := crc64.Checksum([]byte(str), tab)
return int64(coded)
}
func hashBytes(algorithm, str string) ([]byte, error) {
var h hash.Hash
switch algorithm {
case "md5":
h = md5.New()
case "sha1":
h = sha1.New()
case "sha2", "sha256":
h = sha256.New()
}
_, err := h.Write([]byte(str))
if err != nil {
return []byte{}, err
}
return h.Sum(nil), nil
}