refactor(crc):重构CRC系列算法中对于模式的定义,限制其取值。
This commit is contained in:
parent
732c01e36c
commit
1f99378655
|
@ -10,18 +10,28 @@ import (
|
||||||
"github.com/howeyc/crc16"
|
"github.com/howeyc/crc16"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type CRC16Mode int
|
||||||
|
|
||||||
|
const (
|
||||||
|
CCITT CRC16Mode = iota
|
||||||
|
CCITT_FALSE
|
||||||
|
SCSI
|
||||||
|
IBM
|
||||||
|
MBUS
|
||||||
|
)
|
||||||
|
|
||||||
// 根据给定的校验表类型生成对应的校验器
|
// 根据给定的校验表类型生成对应的校验器
|
||||||
func hasherSelect(table string) crc16.Hash16 {
|
func hasherSelect(mdoe CRC16Mode) crc16.Hash16 {
|
||||||
switch table {
|
switch mdoe {
|
||||||
case "CCITT":
|
case CCITT:
|
||||||
return crc16.NewCCITT()
|
return crc16.NewCCITT()
|
||||||
case "CCITT-FALSE":
|
case CCITT_FALSE:
|
||||||
return crc16.New(crc16.MakeTable(crc16.CCITTFalse))
|
return crc16.New(crc16.MakeTable(crc16.CCITTFalse))
|
||||||
case "SCSI":
|
case SCSI:
|
||||||
return crc16.NewSCSI()
|
return crc16.NewSCSI()
|
||||||
case "IBM":
|
case IBM:
|
||||||
return crc16.NewIBM()
|
return crc16.NewIBM()
|
||||||
case "MBUS":
|
case MBUS:
|
||||||
return crc16.New(crc16.MakeTable(crc16.MBUS))
|
return crc16.New(crc16.MakeTable(crc16.MBUS))
|
||||||
default:
|
default:
|
||||||
return crc16.NewIBM()
|
return crc16.NewIBM()
|
||||||
|
@ -29,27 +39,27 @@ func hasherSelect(table string) crc16.Hash16 {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算给定字节数组的CRC16校验和,返回字节数组
|
// 计算给定字节数组的CRC16校验和,返回字节数组
|
||||||
func CRC16(data []byte, table ...string) []byte {
|
func CRC16(data []byte, mode ...CRC16Mode) []byte {
|
||||||
crcTable := append(table, "IBM")
|
crcTable := append(mode, IBM)
|
||||||
hasher := hasherSelect(crcTable[0])
|
hasher := hasherSelect(crcTable[0])
|
||||||
hasher.Write(data)
|
hasher.Write(data)
|
||||||
return hasher.Sum(nil)
|
return hasher.Sum(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算给定字节数组的CRC16校验和,返回十六进制字符串
|
// 计算给定字节数组的CRC16校验和,返回十六进制字符串
|
||||||
func CRC16Hex(data []byte, table ...string) string {
|
func CRC16Hex(data []byte, mode ...CRC16Mode) string {
|
||||||
return hex.EncodeToString(CRC16(data, table...))
|
return hex.EncodeToString(CRC16(data, mode...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算指定文件的CRC16校验和,返回字节数组
|
// 计算指定文件的CRC16校验和,返回字节数组
|
||||||
func SumFile(file string, table ...string) ([]byte, error) {
|
func SumFile(file string, mode ...CRC16Mode) ([]byte, error) {
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("未能打开指定文件,%w", err)
|
return nil, fmt.Errorf("未能打开指定文件,%w", err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
crcTable := append(table, "IBM")
|
crcTable := append(mode, IBM)
|
||||||
hasher := hasherSelect(crcTable[0])
|
hasher := hasherSelect(crcTable[0])
|
||||||
if _, err := io.Copy(hasher, f); err != nil {
|
if _, err := io.Copy(hasher, f); err != nil {
|
||||||
return nil, fmt.Errorf("未能读取指定文件的内容,%w", err)
|
return nil, fmt.Errorf("未能读取指定文件的内容,%w", err)
|
||||||
|
@ -58,8 +68,8 @@ func SumFile(file string, table ...string) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算指定文件的CRC16校验和,返回十六进制字符串
|
// 计算指定文件的CRC16校验和,返回十六进制字符串
|
||||||
func SumFileHex(file string, table ...string) (string, error) {
|
func SumFileHex(file string, mode ...CRC16Mode) (string, error) {
|
||||||
hash, err := SumFile(file, table...)
|
hash, err := SumFile(file, mode...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,14 +9,22 @@ import (
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type CRC32Mode int
|
||||||
|
|
||||||
|
const (
|
||||||
|
IEEE CRC32Mode = iota
|
||||||
|
Castagnoli
|
||||||
|
Koopman
|
||||||
|
)
|
||||||
|
|
||||||
// 选择一个CRC32校验表
|
// 选择一个CRC32校验表
|
||||||
func tableSelect(table string) *crc32.Table {
|
func tableSelect(mode CRC32Mode) *crc32.Table {
|
||||||
switch table {
|
switch mode {
|
||||||
case "IEEE":
|
case IEEE:
|
||||||
return crc32.IEEETable
|
return crc32.IEEETable
|
||||||
case "Castagnoli":
|
case Castagnoli:
|
||||||
return crc32.MakeTable(crc32.Castagnoli)
|
return crc32.MakeTable(crc32.Castagnoli)
|
||||||
case "Koopman":
|
case Koopman:
|
||||||
return crc32.MakeTable(crc32.Koopman)
|
return crc32.MakeTable(crc32.Koopman)
|
||||||
default:
|
default:
|
||||||
return crc32.IEEETable
|
return crc32.IEEETable
|
||||||
|
@ -24,27 +32,27 @@ func tableSelect(table string) *crc32.Table {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算给定字节数组的CRC32校验和,返回字节数组
|
// 计算给定字节数组的CRC32校验和,返回字节数组
|
||||||
func CRC32(data []byte, table ...string) []byte {
|
func CRC32(data []byte, mode ...CRC32Mode) []byte {
|
||||||
crcTable := append(table, "IEEE")
|
crcTable := append(mode, IEEE)
|
||||||
hasher := crc32.New(tableSelect(crcTable[0]))
|
hasher := crc32.New(tableSelect(crcTable[0]))
|
||||||
hasher.Write(data)
|
hasher.Write(data)
|
||||||
return hasher.Sum(nil)
|
return hasher.Sum(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算给定字节数组的CRC32校验和,返回十六进制字符串
|
// 计算给定字节数组的CRC32校验和,返回十六进制字符串
|
||||||
func CRC32Hex(data []byte, table ...string) string {
|
func CRC32Hex(data []byte, mode ...CRC32Mode) string {
|
||||||
return hex.EncodeToString(CRC32(data, table...))
|
return hex.EncodeToString(CRC32(data, mode...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算指定文件的CRC32校验和,返回字节数组
|
// 计算指定文件的CRC32校验和,返回字节数组
|
||||||
func SumFile(file string, table ...string) ([]byte, error) {
|
func SumFile(file string, mode ...CRC32Mode) ([]byte, error) {
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("未能打开指定文件,%w", err)
|
return nil, fmt.Errorf("未能打开指定文件,%w", err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
crcTable := append(table, "IEEE")
|
crcTable := append(mode, IEEE)
|
||||||
hasher := crc32.New(tableSelect(crcTable[0]))
|
hasher := crc32.New(tableSelect(crcTable[0]))
|
||||||
if _, err := io.Copy(hasher, f); err != nil {
|
if _, err := io.Copy(hasher, f); err != nil {
|
||||||
return nil, fmt.Errorf("未能读取指定文件的内容,%w", err)
|
return nil, fmt.Errorf("未能读取指定文件的内容,%w", err)
|
||||||
|
@ -53,8 +61,8 @@ func SumFile(file string, table ...string) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算指定文件的CRC32校验和,返回十六进制字符串
|
// 计算指定文件的CRC32校验和,返回十六进制字符串
|
||||||
func SumFileHex(file string, table ...string) (string, error) {
|
func SumFileHex(file string, mode ...CRC32Mode) (string, error) {
|
||||||
hash, err := SumFile(file, table...)
|
hash, err := SumFile(file, mode...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,12 +9,19 @@ import (
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type CRC64Mode int
|
||||||
|
|
||||||
|
const (
|
||||||
|
ECMA CRC64Mode = iota
|
||||||
|
ISO
|
||||||
|
)
|
||||||
|
|
||||||
// 选择一个CRC64校验表。
|
// 选择一个CRC64校验表。
|
||||||
func tableSelect(table string) *crc64.Table {
|
func tableSelect(mode CRC64Mode) *crc64.Table {
|
||||||
switch table {
|
switch mode {
|
||||||
case "ECMA":
|
case ECMA:
|
||||||
return crc64.MakeTable(crc64.ECMA)
|
return crc64.MakeTable(crc64.ECMA)
|
||||||
case "ISO":
|
case ISO:
|
||||||
return crc64.MakeTable(crc64.ISO)
|
return crc64.MakeTable(crc64.ISO)
|
||||||
default:
|
default:
|
||||||
return crc64.MakeTable(crc64.ISO)
|
return crc64.MakeTable(crc64.ISO)
|
||||||
|
@ -22,27 +29,27 @@ func tableSelect(table string) *crc64.Table {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算给定字节数组的CRC64校验和,返回字节数组。
|
// 计算给定字节数组的CRC64校验和,返回字节数组。
|
||||||
func CRC64(data []byte, table ...string) []byte {
|
func CRC64(data []byte, mode ...CRC64Mode) []byte {
|
||||||
crcTable := append(table, "ISO")
|
crcTable := append(mode, ISO)
|
||||||
hasher := crc64.New(tableSelect(crcTable[0]))
|
hasher := crc64.New(tableSelect(crcTable[0]))
|
||||||
hasher.Write(data)
|
hasher.Write(data)
|
||||||
return hasher.Sum(nil)
|
return hasher.Sum(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算给定字节数组的CRC64校验和,返回十六进制字符串。
|
// 计算给定字节数组的CRC64校验和,返回十六进制字符串。
|
||||||
func CRC64Hex(data []byte, table ...string) string {
|
func CRC64Hex(data []byte, mode ...CRC64Mode) string {
|
||||||
return hex.EncodeToString(CRC64(data, table...))
|
return hex.EncodeToString(CRC64(data, mode...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算一个指定文件的CRC64校验和,返回字节数组。
|
// 计算一个指定文件的CRC64校验和,返回字节数组。
|
||||||
func SumFile(file string, table ...string) ([]byte, error) {
|
func SumFile(file string, mode ...CRC64Mode) ([]byte, error) {
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("未能打开指定文件,%w", err)
|
return nil, fmt.Errorf("未能打开指定文件,%w", err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
crcTable := append(table, "ISO")
|
crcTable := append(mode, ISO)
|
||||||
hasher := crc64.New(tableSelect(crcTable[0]))
|
hasher := crc64.New(tableSelect(crcTable[0]))
|
||||||
if _, err := io.Copy(hasher, f); err != nil {
|
if _, err := io.Copy(hasher, f); err != nil {
|
||||||
return nil, fmt.Errorf("未能读取指定文件的内容,%w", err)
|
return nil, fmt.Errorf("未能读取指定文件的内容,%w", err)
|
||||||
|
@ -51,8 +58,8 @@ func SumFile(file string, table ...string) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算一个指定文件的CRC64校验和,返回十六进制字符串。
|
// 计算一个指定文件的CRC64校验和,返回十六进制字符串。
|
||||||
func SumFileHex(file string, table ...string) (string, error) {
|
func SumFileHex(file string, mode ...CRC64Mode) (string, error) {
|
||||||
hash, err := SumFile(file, table...)
|
hash, err := SumFile(file, mode...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,28 +9,43 @@ import (
|
||||||
"github.com/sigurn/crc8"
|
"github.com/sigurn/crc8"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type CRC8Mode int
|
||||||
|
|
||||||
|
const (
|
||||||
|
ORIGIN CRC8Mode = iota
|
||||||
|
CDMA2000
|
||||||
|
DARC
|
||||||
|
DVB_S2
|
||||||
|
EBU
|
||||||
|
I_CODE
|
||||||
|
ITU
|
||||||
|
MAXIM
|
||||||
|
ROHC
|
||||||
|
WCDMA
|
||||||
|
)
|
||||||
|
|
||||||
// 根据提供的校验表名称生成对应的校验表
|
// 根据提供的校验表名称生成对应的校验表
|
||||||
func hasherSelect(table string) *crc8.Table {
|
func hasherSelect(mode CRC8Mode) *crc8.Table {
|
||||||
switch table {
|
switch mode {
|
||||||
case "CRC8":
|
case ORIGIN:
|
||||||
return crc8.MakeTable(crc8.CRC8)
|
return crc8.MakeTable(crc8.CRC8)
|
||||||
case "CDMA2000":
|
case CDMA2000:
|
||||||
return crc8.MakeTable(crc8.CRC8_CDMA2000)
|
return crc8.MakeTable(crc8.CRC8_CDMA2000)
|
||||||
case "DARC":
|
case DARC:
|
||||||
return crc8.MakeTable(crc8.CRC8_DARC)
|
return crc8.MakeTable(crc8.CRC8_DARC)
|
||||||
case "DVB-S2":
|
case DVB_S2:
|
||||||
return crc8.MakeTable(crc8.CRC8_DVB_S2)
|
return crc8.MakeTable(crc8.CRC8_DVB_S2)
|
||||||
case "EBU":
|
case EBU:
|
||||||
return crc8.MakeTable(crc8.CRC8_EBU)
|
return crc8.MakeTable(crc8.CRC8_EBU)
|
||||||
case "I-CODE":
|
case I_CODE:
|
||||||
return crc8.MakeTable(crc8.CRC8_I_CODE)
|
return crc8.MakeTable(crc8.CRC8_I_CODE)
|
||||||
case "ITU":
|
case ITU:
|
||||||
return crc8.MakeTable(crc8.CRC8_ITU)
|
return crc8.MakeTable(crc8.CRC8_ITU)
|
||||||
case "MAXIM":
|
case MAXIM:
|
||||||
return crc8.MakeTable(crc8.CRC8_MAXIM)
|
return crc8.MakeTable(crc8.CRC8_MAXIM)
|
||||||
case "ROHC":
|
case ROHC:
|
||||||
return crc8.MakeTable(crc8.CRC8_ROHC)
|
return crc8.MakeTable(crc8.CRC8_ROHC)
|
||||||
case "WCDMA":
|
case WCDMA:
|
||||||
return crc8.MakeTable(crc8.CRC8_WCDMA)
|
return crc8.MakeTable(crc8.CRC8_WCDMA)
|
||||||
default:
|
default:
|
||||||
return crc8.MakeTable(crc8.CRC8)
|
return crc8.MakeTable(crc8.CRC8)
|
||||||
|
@ -38,25 +53,25 @@ func hasherSelect(table string) *crc8.Table {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算给定字节数组的CRC8校验和,返回字节数组
|
// 计算给定字节数组的CRC8校验和,返回字节数组
|
||||||
func CRC8(data []byte, table ...string) []byte {
|
func CRC8(data []byte, mode ...CRC8Mode) []byte {
|
||||||
crcTable := append(table, "CRC8")
|
crcTable := append(mode, ORIGIN)
|
||||||
return []byte{crc8.Checksum(data, hasherSelect(crcTable[0]))}
|
return []byte{crc8.Checksum(data, hasherSelect(crcTable[0]))}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算给定字节数组的CRC8校验和,返回十六进制字符串
|
// 计算给定字节数组的CRC8校验和,返回十六进制字符串
|
||||||
func CRC8Hex(data []byte, table ...string) string {
|
func CRC8Hex(data []byte, mode ...CRC8Mode) string {
|
||||||
return hex.EncodeToString(CRC8(data, table...))
|
return hex.EncodeToString(CRC8(data, mode...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算指定文件的CRC8校验和,返回字节数组
|
// 计算指定文件的CRC8校验和,返回字节数组
|
||||||
func SumFile(file string, table ...string) ([]byte, error) {
|
func SumFile(file string, mode ...CRC8Mode) ([]byte, error) {
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("未能打开指定文件,%w", err)
|
return nil, fmt.Errorf("未能打开指定文件,%w", err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
crcTable := append(table, "CRC8")
|
crcTable := append(mode, ORIGIN)
|
||||||
var buf = make([]byte, 0)
|
var buf = make([]byte, 0)
|
||||||
if _, err := f.Read(buf); err != nil {
|
if _, err := f.Read(buf); err != nil {
|
||||||
return nil, fmt.Errorf("读取指定文件内容出错,%w", err)
|
return nil, fmt.Errorf("读取指定文件内容出错,%w", err)
|
||||||
|
@ -65,8 +80,8 @@ func SumFile(file string, table ...string) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算指定文件的CRC8校验和,返回十六进制字符串
|
// 计算指定文件的CRC8校验和,返回十六进制字符串
|
||||||
func SumFileHex(file string, table ...string) (string, error) {
|
func SumFileHex(file string, mode ...CRC8Mode) (string, error) {
|
||||||
crc, err := SumFile(file, table...)
|
crc, err := SumFile(file, mode...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user