-
Notifications
You must be signed in to change notification settings - Fork 0
/
crc32.go
75 lines (57 loc) · 1.52 KB
/
crc32.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
68
69
70
71
72
73
74
75
package hash
import (
"strconv"
"hash/crc32"
)
// CRC32IEEE 哈希值
func CRC32IEEE(s string) string {
data := []byte(s)
res := crc32.ChecksumIEEE(data)
return strconv.FormatInt(int64(res), 16)
}
// CRC32IEEE 哈希值
func (this Hash) CRC32IEEE() Hash {
return this.FuncHash(func(data ...[]byte) (string, error) {
newData := ""
for _, v := range data {
newData += string(v)
}
return CRC32IEEE(newData), nil
})
}
// =======================
// CRC32Castagnoli 哈希值
func CRC32Castagnoli(s string) string {
data := []byte(s)
tab := crc32.MakeTable(crc32.Castagnoli)
res := crc32.Checksum(data, tab)
return strconv.FormatInt(int64(res), 16)
}
// CRC32Castagnoli 哈希值
func (this Hash) CRC32Castagnoli() Hash {
return this.FuncHash(func(data ...[]byte) (string, error) {
newData := ""
for _, v := range data {
newData += string(v)
}
return CRC32Castagnoli(newData), nil
})
}
// =======================
// CRC32Koopman 哈希值
func CRC32Koopman(s string) string {
data := []byte(s)
tab := crc32.MakeTable(crc32.Koopman)
res := crc32.Checksum(data, tab)
return strconv.FormatInt(int64(res), 16)
}
// CRC32Koopman 哈希值
func (this Hash) CRC32Koopman() Hash {
return this.FuncHash(func(data ...[]byte) (string, error) {
newData := ""
for _, v := range data {
newData += string(v)
}
return CRC32Koopman(newData), nil
})
}