6 Commits

9 changed files with 137 additions and 76 deletions

View File

@@ -41,8 +41,8 @@ func Encrypt(data []byte, key []byte, padding encryption.PaddingMode, keyGenerat
}
iv := keyBytes[:]
cipherText := make([]byte, len(data))
plainText := encryption.Padding(data, block.BlockSize(), padding)
cipherText := make([]byte, len(plainText))
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(cipherText, plainText)

View File

@@ -37,6 +37,9 @@ func Unpadding(data []byte, padding ...PaddingMode) []byte {
case PKCS7Padding:
length := len(data)
unpadding := int(data[length-1])
if length-unpadding < 0 {
return make([]byte, 0)
}
return data[:(length - unpadding)]
case NoPadding:
return data

View File

@@ -13,6 +13,13 @@ import (
"archgrid.xyz/ag/toolsbox/serialize/base64"
)
type Strength int
const (
Compatible Strength = iota
Enhanced
)
// 根据给定的密钥字符串生成加解密使用的密钥。
func generateKey(key string) []byte {
keyBytes := sha512.Sha512([]byte(key))
@@ -20,10 +27,16 @@ func generateKey(key string) []byte {
}
// 对给定的数据进行加密。
func Encrypt(data string) (string, error) {
func Encrypt(data string, strength ...Strength) (string, error) {
var ivGen aes.IVGenerator
if append(strength, Enhanced)[0] == Compatible {
ivGen = aes.PrefixIVGenerator
} else {
ivGen = aes.XorIVGenerator
}
key := verifyCode.RandStr(20)
keyBytes := generateKey(key)
cipherData, err := aes.Encrypt([]byte(data), keyBytes, encryption.PKCS7Padding)
cipherData, err := aes.Encrypt([]byte(data), keyBytes, encryption.PKCS7Padding, ivGen)
if err != nil {
return "", fmt.Errorf("加密计算失败,%w", err)
}
@@ -35,7 +48,13 @@ func Encrypt(data string) (string, error) {
}
// 对给定的数据进行解密。
func Decrypt(data string) (string, error) {
func Decrypt(data string, strength ...Strength) (string, error) {
var ivGen aes.IVGenerator
if append(strength, Enhanced)[0] == Compatible {
ivGen = aes.PrefixIVGenerator
} else {
ivGen = aes.XorIVGenerator
}
if message, found := strings.CutPrefix(data, "["); found {
if len(message) > 20 {
keySeed := message[:20]
@@ -44,7 +63,7 @@ func Decrypt(data string) (string, error) {
if err != nil {
return "", fmt.Errorf("密文损坏无法解析,%w", err)
}
plainText, err := aes.Decrypt(cipherData, key, encryption.PKCS7Padding)
plainText, err := aes.Decrypt(cipherData, key, encryption.PKCS7Padding, ivGen)
if err != nil {
return "", fmt.Errorf("密文解密计算失败,%w", err)
}

View File

@@ -10,18 +10,28 @@ import (
"github.com/howeyc/crc16"
)
type CRC16Mode int
const (
CCITT CRC16Mode = iota
CCITT_FALSE
SCSI
IBM
MBUS
)
// 根据给定的校验表类型生成对应的校验器
func hasherSelect(table string) crc16.Hash16 {
switch table {
case "CCITT":
func hasherSelect(mdoe CRC16Mode) crc16.Hash16 {
switch mdoe {
case CCITT:
return crc16.NewCCITT()
case "CCITT-FALSE":
case CCITT_FALSE:
return crc16.New(crc16.MakeTable(crc16.CCITTFalse))
case "SCSI":
case SCSI:
return crc16.NewSCSI()
case "IBM":
case IBM:
return crc16.NewIBM()
case "MBUS":
case MBUS:
return crc16.New(crc16.MakeTable(crc16.MBUS))
default:
return crc16.NewIBM()
@@ -29,27 +39,27 @@ func hasherSelect(table string) crc16.Hash16 {
}
// 计算给定字节数组的CRC16校验和返回字节数组
func CRC16(data []byte, table ...string) []byte {
crcTable := append(table, "IBM")
func CRC16(data []byte, mode ...CRC16Mode) []byte {
crcTable := append(mode, IBM)
hasher := hasherSelect(crcTable[0])
hasher.Write(data)
return hasher.Sum(nil)
}
// 计算给定字节数组的CRC16校验和返回十六进制字符串
func CRC16Hex(data []byte, table ...string) string {
return hex.EncodeToString(CRC16(data, table...))
func CRC16Hex(data []byte, mode ...CRC16Mode) string {
return hex.EncodeToString(CRC16(data, mode...))
}
// 计算指定文件的CRC16校验和返回字节数组
func SumFile(file string, table ...string) ([]byte, error) {
func SumFile(file string, mode ...CRC16Mode) ([]byte, error) {
f, err := os.Open(file)
if err != nil {
return nil, fmt.Errorf("未能打开指定文件,%w", err)
}
defer f.Close()
crcTable := append(table, "IBM")
crcTable := append(mode, IBM)
hasher := hasherSelect(crcTable[0])
if _, err := io.Copy(hasher, f); err != nil {
return nil, fmt.Errorf("未能读取指定文件的内容,%w", err)
@@ -58,8 +68,8 @@ func SumFile(file string, table ...string) ([]byte, error) {
}
// 计算指定文件的CRC16校验和返回十六进制字符串
func SumFileHex(file string, table ...string) (string, error) {
hash, err := SumFile(file, table...)
func SumFileHex(file string, mode ...CRC16Mode) (string, error) {
hash, err := SumFile(file, mode...)
if err != nil {
return "", err
}

View File

@@ -9,14 +9,22 @@ import (
"os"
)
type CRC32Mode int
const (
IEEE CRC32Mode = iota
Castagnoli
Koopman
)
// 选择一个CRC32校验表
func tableSelect(table string) *crc32.Table {
switch table {
case "IEEE":
func tableSelect(mode CRC32Mode) *crc32.Table {
switch mode {
case IEEE:
return crc32.IEEETable
case "Castagnoli":
case Castagnoli:
return crc32.MakeTable(crc32.Castagnoli)
case "Koopman":
case Koopman:
return crc32.MakeTable(crc32.Koopman)
default:
return crc32.IEEETable
@@ -24,27 +32,27 @@ func tableSelect(table string) *crc32.Table {
}
// 计算给定字节数组的CRC32校验和返回字节数组
func CRC32(data []byte, table ...string) []byte {
crcTable := append(table, "IEEE")
func CRC32(data []byte, mode ...CRC32Mode) []byte {
crcTable := append(mode, IEEE)
hasher := crc32.New(tableSelect(crcTable[0]))
hasher.Write(data)
return hasher.Sum(nil)
}
// 计算给定字节数组的CRC32校验和返回十六进制字符串
func CRC32Hex(data []byte, table ...string) string {
return hex.EncodeToString(CRC32(data, table...))
func CRC32Hex(data []byte, mode ...CRC32Mode) string {
return hex.EncodeToString(CRC32(data, mode...))
}
// 计算指定文件的CRC32校验和返回字节数组
func SumFile(file string, table ...string) ([]byte, error) {
func SumFile(file string, mode ...CRC32Mode) ([]byte, error) {
f, err := os.Open(file)
if err != nil {
return nil, fmt.Errorf("未能打开指定文件,%w", err)
}
defer f.Close()
crcTable := append(table, "IEEE")
crcTable := append(mode, IEEE)
hasher := crc32.New(tableSelect(crcTable[0]))
if _, err := io.Copy(hasher, f); err != nil {
return nil, fmt.Errorf("未能读取指定文件的内容,%w", err)
@@ -53,8 +61,8 @@ func SumFile(file string, table ...string) ([]byte, error) {
}
// 计算指定文件的CRC32校验和返回十六进制字符串
func SumFileHex(file string, table ...string) (string, error) {
hash, err := SumFile(file, table...)
func SumFileHex(file string, mode ...CRC32Mode) (string, error) {
hash, err := SumFile(file, mode...)
if err != nil {
return "", err
}

View File

@@ -9,12 +9,19 @@ import (
"os"
)
type CRC64Mode int
const (
ECMA CRC64Mode = iota
ISO
)
// 选择一个CRC64校验表。
func tableSelect(table string) *crc64.Table {
switch table {
case "ECMA":
func tableSelect(mode CRC64Mode) *crc64.Table {
switch mode {
case ECMA:
return crc64.MakeTable(crc64.ECMA)
case "ISO":
case ISO:
return crc64.MakeTable(crc64.ISO)
default:
return crc64.MakeTable(crc64.ISO)
@@ -22,27 +29,27 @@ func tableSelect(table string) *crc64.Table {
}
// 计算给定字节数组的CRC64校验和返回字节数组。
func CRC64(data []byte, table ...string) []byte {
crcTable := append(table, "ISO")
func CRC64(data []byte, mode ...CRC64Mode) []byte {
crcTable := append(mode, ISO)
hasher := crc64.New(tableSelect(crcTable[0]))
hasher.Write(data)
return hasher.Sum(nil)
}
// 计算给定字节数组的CRC64校验和返回十六进制字符串。
func CRC64Hex(data []byte, table ...string) string {
return hex.EncodeToString(CRC64(data, table...))
func CRC64Hex(data []byte, mode ...CRC64Mode) string {
return hex.EncodeToString(CRC64(data, mode...))
}
// 计算一个指定文件的CRC64校验和返回字节数组。
func SumFile(file string, table ...string) ([]byte, error) {
func SumFile(file string, mode ...CRC64Mode) ([]byte, error) {
f, err := os.Open(file)
if err != nil {
return nil, fmt.Errorf("未能打开指定文件,%w", err)
}
defer f.Close()
crcTable := append(table, "ISO")
crcTable := append(mode, ISO)
hasher := crc64.New(tableSelect(crcTable[0]))
if _, err := io.Copy(hasher, f); err != nil {
return nil, fmt.Errorf("未能读取指定文件的内容,%w", err)
@@ -51,8 +58,8 @@ func SumFile(file string, table ...string) ([]byte, error) {
}
// 计算一个指定文件的CRC64校验和返回十六进制字符串。
func SumFileHex(file string, table ...string) (string, error) {
hash, err := SumFile(file, table...)
func SumFileHex(file string, mode ...CRC64Mode) (string, error) {
hash, err := SumFile(file, mode...)
if err != nil {
return "", err
}

View File

@@ -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
}

View File

@@ -46,8 +46,7 @@ func Sha256_224Hex(data []byte) string {
// 根据给定位数计算一个字节数组的Sha256校验和返回字节数组。
func Sum256(data []byte, bitSize ...int) []byte {
length := append(bitSize, 256)
hasher := hasherSelect(length[0])
hasher := hasherSelect(append(bitSize, 256)[0])
hasher.Write(data)
return hasher.Sum(nil)
}
@@ -65,7 +64,7 @@ func SumFile256(file string, bitSize ...int) ([]byte, error) {
}
defer f.Close()
hasher := hasherSelect(bitSize[0])
hasher := hasherSelect(append(bitSize, 256)[0])
if _, err := io.Copy(hasher, f); err != nil {
return nil, fmt.Errorf("未能读取指定文件的内容,%w", err)
}

View File

@@ -10,17 +10,17 @@ import (
)
// 用于记录当前冰雹ID组成内容的数据结构
type _HailAlgorithm struct {
type HailAlgorithm struct {
hostSerial int64
lastTimestamp int64
lastSequence int64
lock sync.Mutex
}
var hailAlgorithmInstance *_HailAlgorithm
var hailAlgorithmInstance *HailAlgorithm
// 获取当前已经完成初始化的冰雹ID生成算法实例如果尚未完成初始化则会返回未初始化的错误。
func Get() (*_HailAlgorithm, error) {
func Get() (*HailAlgorithm, error) {
if hailAlgorithmInstance == nil {
return nil, &HailAlgorithmNotInitializedError{}
}
@@ -29,7 +29,7 @@ func Get() (*_HailAlgorithm, error) {
// 指定一个主机编号并完成冰雹ID生成算法的初始化。
func Initialize(hostSerial int64) {
hailAlgorithmInstance = &_HailAlgorithm{
hailAlgorithmInstance = &HailAlgorithm{
hostSerial: hostSerial,
lastTimestamp: types.Timestamp(),
lastSequence: 0,
@@ -38,7 +38,7 @@ func Initialize(hostSerial int64) {
}
// 返回一个可用的时间戳,如果主机发生了时间回拨,那么将等待一秒钟。
func (h *_HailAlgorithm) timestamp() int64 {
func (h *HailAlgorithm) timestamp() int64 {
for {
timestamp := types.Timestamp()
if timestamp == h.lastTimestamp {
@@ -54,7 +54,7 @@ func (h *_HailAlgorithm) timestamp() int64 {
}
// 生成一个冰雹ID。
func (h *_HailAlgorithm) Generate() int64 {
func (h *HailAlgorithm) Generate() int64 {
h.lock.Lock()
defer h.lock.Unlock()
timestamp := h.timestamp()
@@ -63,11 +63,11 @@ func (h *_HailAlgorithm) Generate() int64 {
}
// 生成一个冰雹ID并将其转换为字符串。
func (h *_HailAlgorithm) GenerateString() string {
func (h *HailAlgorithm) GenerateString() string {
return fmt.Sprintf("%017d", h.Generate())
}
// 生成一个冰雹ID将其转换为字符串并附加指定的前缀
func (h *_HailAlgorithm) GeneratePrefixedString(prefix string) string {
func (h *HailAlgorithm) GeneratePrefixedString(prefix string) string {
return fmt.Sprintf("%s%s", prefix, h.GenerateString())
}