refactor(crc):重构CRC系列算法中对于模式的定义,限制其取值。
This commit is contained in:
@@ -9,28 +9,43 @@ import (
|
||||
"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 {
|
||||
switch table {
|
||||
case "CRC8":
|
||||
func hasherSelect(mode CRC8Mode) *crc8.Table {
|
||||
switch mode {
|
||||
case ORIGIN:
|
||||
return crc8.MakeTable(crc8.CRC8)
|
||||
case "CDMA2000":
|
||||
case CDMA2000:
|
||||
return crc8.MakeTable(crc8.CRC8_CDMA2000)
|
||||
case "DARC":
|
||||
case DARC:
|
||||
return crc8.MakeTable(crc8.CRC8_DARC)
|
||||
case "DVB-S2":
|
||||
case DVB_S2:
|
||||
return crc8.MakeTable(crc8.CRC8_DVB_S2)
|
||||
case "EBU":
|
||||
case EBU:
|
||||
return crc8.MakeTable(crc8.CRC8_EBU)
|
||||
case "I-CODE":
|
||||
case I_CODE:
|
||||
return crc8.MakeTable(crc8.CRC8_I_CODE)
|
||||
case "ITU":
|
||||
case ITU:
|
||||
return crc8.MakeTable(crc8.CRC8_ITU)
|
||||
case "MAXIM":
|
||||
case MAXIM:
|
||||
return crc8.MakeTable(crc8.CRC8_MAXIM)
|
||||
case "ROHC":
|
||||
case ROHC:
|
||||
return crc8.MakeTable(crc8.CRC8_ROHC)
|
||||
case "WCDMA":
|
||||
case WCDMA:
|
||||
return crc8.MakeTable(crc8.CRC8_WCDMA)
|
||||
default:
|
||||
return crc8.MakeTable(crc8.CRC8)
|
||||
@@ -38,25 +53,25 @@ func hasherSelect(table string) *crc8.Table {
|
||||
}
|
||||
|
||||
// 计算给定字节数组的CRC8校验和,返回字节数组
|
||||
func CRC8(data []byte, table ...string) []byte {
|
||||
crcTable := append(table, "CRC8")
|
||||
func CRC8(data []byte, mode ...CRC8Mode) []byte {
|
||||
crcTable := append(mode, ORIGIN)
|
||||
return []byte{crc8.Checksum(data, hasherSelect(crcTable[0]))}
|
||||
}
|
||||
|
||||
// 计算给定字节数组的CRC8校验和,返回十六进制字符串
|
||||
func CRC8Hex(data []byte, table ...string) string {
|
||||
return hex.EncodeToString(CRC8(data, table...))
|
||||
func CRC8Hex(data []byte, mode ...CRC8Mode) string {
|
||||
return hex.EncodeToString(CRC8(data, mode...))
|
||||
}
|
||||
|
||||
// 计算指定文件的CRC8校验和,返回字节数组
|
||||
func SumFile(file string, table ...string) ([]byte, error) {
|
||||
func SumFile(file string, mode ...CRC8Mode) ([]byte, error) {
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("未能打开指定文件,%w", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
crcTable := append(table, "CRC8")
|
||||
crcTable := append(mode, ORIGIN)
|
||||
var buf = make([]byte, 0)
|
||||
if _, err := f.Read(buf); err != nil {
|
||||
return nil, fmt.Errorf("读取指定文件内容出错,%w", err)
|
||||
@@ -65,8 +80,8 @@ func SumFile(file string, table ...string) ([]byte, error) {
|
||||
}
|
||||
|
||||
// 计算指定文件的CRC8校验和,返回十六进制字符串
|
||||
func SumFileHex(file string, table ...string) (string, error) {
|
||||
crc, err := SumFile(file, table...)
|
||||
func SumFileHex(file string, mode ...CRC8Mode) (string, error) {
|
||||
crc, err := SumFile(file, mode...)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
Reference in New Issue
Block a user