/
crc.go
59 lines (46 loc) · 1.25 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
// Copyright 2023 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 = crc32.IEEETable
tableISO = crc64.MakeTable(crc64.ISO)
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 bs.
// Use IEEE table if passed table is nil.
func CRC32(bs cryptox.Bytes, table *Table32) uint32 {
if table == nil {
table = tableIEEE
}
return crc32.Checksum(bs, table)
}
// CRC32IEEE uses ieee table to checksum bs.
func CRC32IEEE(bs cryptox.Bytes) uint32 {
return CRC32(bs, tableIEEE)
}
// CRC64 uses given table to checksum bs.
// Use ISO table if passed table is nil.
func CRC64(bs cryptox.Bytes, table *Table64) uint64 {
if table == nil {
table = tableISO
}
return crc64.Checksum(bs, table)
}
// CRC64ISO uses iso table to checksum bs.
func CRC64ISO(bs cryptox.Bytes) uint64 {
return CRC64(bs, tableISO)
}
// CRC64ECMA uses ecma table to checksum bs.
func CRC64ECMA(bs cryptox.Bytes) uint64 {
return CRC64(bs, tableECMA)
}