-
Notifications
You must be signed in to change notification settings - Fork 0
/
crc.go
70 lines (53 loc) · 1.58 KB
/
crc.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
// Copyright 2022 FishGoddess. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package hash
import (
"hash/crc32"
"hash/crc64"
"github.com/FishGoddess/cryptox"
)
var (
// TableIEEE is ieee table for crc32.
TableIEEE = crc32.IEEETable
// TableISO is iso table for crc64.
TableISO = crc64.MakeTable(crc64.ISO)
// TableECMA is ecma table for crc64.
TableECMA = crc64.MakeTable(crc64.ECMA)
)
// Table32 is an alias of crc32.Table.
type Table32 = crc32.Table
// Table64 is an alias of crc64.Table.
type Table64 = crc64.Table
// CRC32 uses given table to checksum data.
// Use IEEE table if passed table is nil.
func CRC32(data cryptox.Bytes, table *Table32) (cryptox.Bytes, uint32) {
if table == nil {
table = TableIEEE
}
hash32 := crc32.New(table)
hash32.Write(data)
return hash32.Sum(nil), hash32.Sum32()
}
// CRC32IEEE uses ieee table to checksum data.
func CRC32IEEE(data cryptox.Bytes) (cryptox.Bytes, uint32) {
return CRC32(data, TableIEEE)
}
// CRC64 uses given table to checksum data.
// Use ISO table if passed table is nil.
func CRC64(data cryptox.Bytes, table *Table64) (cryptox.Bytes, uint64) {
if table == nil {
table = TableISO
}
hash64 := crc64.New(table)
hash64.Write(data)
return hash64.Sum(nil), hash64.Sum64()
}
// CRC64ISO uses iso table to checksum data.
func CRC64ISO(data cryptox.Bytes) (cryptox.Bytes, uint64) {
return CRC64(data, TableISO)
}
// CRC64ECMA uses ecma table to checksum data.
func CRC64ECMA(data cryptox.Bytes) (cryptox.Bytes, uint64) {
return CRC64(data, TableECMA)
}