Compare commits
5 Commits
53330ceab9
..
v0.1.7
| Author | SHA1 | Date | |
|---|---|---|---|
| 6d21740c90 | |||
| be1009ef58 | |||
| b57b3afb2c | |||
| 30ddec3409 | |||
| 357b19d9ad |
@@ -22,7 +22,7 @@ Golang 中可以使用的常用辅助功能工具箱。主要配备以下功能
|
||||
- [x] KeyPair 生成器
|
||||
- [x] Key 导入与导出
|
||||
- [x] RSA 签名算法
|
||||
- 散列及校验和算法。
|
||||
- 散列及校验和算法
|
||||
- [x] Sha512 散列算法(便捷封装)
|
||||
- [x] Sha256 散列算法(便捷封装)
|
||||
- [x] Sha1 散列算法(便捷封装)
|
||||
@@ -32,8 +32,8 @@ Golang 中可以使用的常用辅助功能工具箱。主要配备以下功能
|
||||
- [x] CRC32 校验和算法(便捷封装)
|
||||
- [x] CRC64 校验和算法(便捷封装)
|
||||
- [x] pHash 图像感知算法(便捷封装)
|
||||
- [ ] BLAKE2 校验和算法(便捷封装)
|
||||
- [ ] BLAKE3 校验和算法(便捷封装)
|
||||
- [x] BLAKE2b 校验和算法(便捷封装)
|
||||
- [x] BLAKE3 校验和算法(便捷封装)
|
||||
- [ ] BlockHash 散列算法
|
||||
- 唯一序列号生成器
|
||||
- [x] 冰雹 ID 生成器(短主机精简日期版雪花 ID)
|
||||
|
||||
+47
-14
@@ -2,6 +2,8 @@
|
||||
package spiral
|
||||
|
||||
import (
|
||||
stdaes "crypto/aes"
|
||||
"crypto/cipher"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
@@ -26,17 +28,51 @@ func generateKey(key string) []byte {
|
||||
return keyBytes[4:36]
|
||||
}
|
||||
|
||||
// 使用原始密钥进行AES-CBC-256加密(不经过二次SHA256处理)
|
||||
func encryptWithRawKey(data []byte, key []byte, ivGenerator aes.IVGenerator) ([]byte, error) {
|
||||
block, err := stdaes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建加密单元失败,%w", err)
|
||||
}
|
||||
|
||||
var key32 [32]byte
|
||||
copy(key32[:], key)
|
||||
iv := ivGenerator(key32)
|
||||
|
||||
plainText := encryption.Padding(data, block.BlockSize(), encryption.PKCS7Padding)
|
||||
cipherText := make([]byte, len(plainText))
|
||||
mode := cipher.NewCBCEncrypter(block, iv[:])
|
||||
mode.CryptBlocks(cipherText, plainText)
|
||||
|
||||
return cipherText, nil
|
||||
}
|
||||
|
||||
// 使用原始密钥进行AES-CBC-256解密(不经过二次SHA256处理)
|
||||
func decryptWithRawKey(data []byte, key []byte, ivGenerator aes.IVGenerator) ([]byte, error) {
|
||||
block, err := stdaes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建加密单元失败,%w", err)
|
||||
}
|
||||
|
||||
var key32 [32]byte
|
||||
copy(key32[:], key)
|
||||
iv := ivGenerator(key32)
|
||||
|
||||
plainText := make([]byte, len(data))
|
||||
mode := cipher.NewCBCDecrypter(block, iv[:])
|
||||
mode.CryptBlocks(plainText, data)
|
||||
|
||||
return encryption.Unpadding(plainText, encryption.PKCS7Padding), nil
|
||||
}
|
||||
|
||||
// 对给定的数据进行加密。
|
||||
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
|
||||
}
|
||||
// 为了与Rust版本兼容,固定使用PrefixIVGenerator
|
||||
ivGen := aes.PrefixIVGenerator
|
||||
key := verifyCode.RandStr(20)
|
||||
keyBytes := generateKey(key)
|
||||
cipherData, err := aes.Encrypt([]byte(data), keyBytes, encryption.PKCS7Padding, ivGen)
|
||||
// 直接使用keyBytes,不经过aes包的二次SHA256处理
|
||||
cipherData, err := encryptWithRawKey([]byte(data), keyBytes, ivGen)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("加密计算失败,%w", err)
|
||||
}
|
||||
@@ -49,12 +85,8 @@ func Encrypt(data string, strength ...Strength) (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
|
||||
}
|
||||
// 为了与Rust版本兼容,固定使用PrefixIVGenerator
|
||||
ivGen := aes.PrefixIVGenerator
|
||||
if message, found := strings.CutPrefix(data, "["); found {
|
||||
if len(message) > 20 {
|
||||
keySeed := message[:20]
|
||||
@@ -63,7 +95,8 @@ func Decrypt(data string, strength ...Strength) (string, error) {
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("密文损坏无法解析,%w", err)
|
||||
}
|
||||
plainText, err := aes.Decrypt(cipherData, key, encryption.PKCS7Padding, ivGen)
|
||||
// 直接使用key,不经过aes包的二次SHA256处理
|
||||
plainText, err := decryptWithRawKey(cipherData, key, ivGen)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("密文解密计算失败,%w", err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package spiral
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestDecode(t *testing.T) {
|
||||
var origin = "[q3XvNHL7oTfVpHmZ2bOAnyVY/Q1Bm2dqsI8hfVA74R9CQb4vyksTD+Y9l4TT62o="
|
||||
decoded, err := Decrypt(origin)
|
||||
if err != nil {
|
||||
t.Fatalf("Decode failed: %v", err)
|
||||
}
|
||||
expected := "TmFRS0w6BIrAPA1Raj"
|
||||
if decoded != expected {
|
||||
t.Fatalf("Decoded value mismatch. Got: %s, Expected: %s", decoded, expected)
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,15 @@
|
||||
module archgrid.xyz/ag/toolsbox
|
||||
|
||||
go 1.20
|
||||
go 1.24.0
|
||||
|
||||
require go.uber.org/zap v1.24.0
|
||||
|
||||
require (
|
||||
github.com/azr/gift v1.1.2 // indirect
|
||||
github.com/disintegration/imaging v1.6.2 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
|
||||
golang.org/x/image v0.0.0-20220722155232-062f8c9fd539 // indirect
|
||||
golang.org/x/sys v0.36.0 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -17,4 +19,6 @@ require (
|
||||
github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f
|
||||
go.uber.org/atomic v1.7.0 // indirect
|
||||
go.uber.org/multierr v1.6.0 // indirect
|
||||
golang.org/x/crypto v0.42.0
|
||||
lukechampine.com/blake3 v1.4.1
|
||||
)
|
||||
|
||||
@@ -10,6 +10,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/howeyc/crc16 v0.0.0-20171223171357-2b2a61e366a6 h1:IIVxLyDUYErC950b8kecjoqDet8P5S4lcVRUOM6rdkU=
|
||||
github.com/howeyc/crc16 v0.0.0-20171223171357-2b2a61e366a6/go.mod h1:JslaLRrzGsOKJgFEPBP65Whn+rdwDQSk0I0MCRFe2Zw=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f h1:1R9KdKjCNSd7F8iGTxIpoID9prlYH8nuNYKt0XvweHA=
|
||||
github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f/go.mod h1:vQhwQ4meQEDfahT5kd61wLAF5AAeh5ZPLVI4JJ/tYo8=
|
||||
@@ -21,9 +23,15 @@ go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
|
||||
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
||||
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
|
||||
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
|
||||
golang.org/x/crypto v0.42.0 h1:chiH31gIWm57EkTXpwnqf8qeuMUi0yekh6mT2AvFlqI=
|
||||
golang.org/x/crypto v0.42.0/go.mod h1:4+rDnOTJhQCx2q7/j6rAN5XDw8kPjeaXEUR2eL94ix8=
|
||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||
golang.org/x/image v0.0.0-20220722155232-062f8c9fd539 h1:/eM0PCrQI2xd471rI+snWuu251/+/jpBpZqir2mPdnU=
|
||||
golang.org/x/image v0.0.0-20220722155232-062f8c9fd539/go.mod h1:doUCurBvlfPMKfmIpRIywoHmhN3VyhnoFDbvIEWF4hY=
|
||||
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
|
||||
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
lukechampine.com/blake3 v1.4.1 h1:I3Smz7gso8w4/TunLKec6K2fn+kyKtDxr/xcQEN84Wg=
|
||||
lukechampine.com/blake3 v1.4.1/go.mod h1:QFosUxmjB8mnrWFSNwKmvxHpfY72bmD2tQ0kBMM3kwo=
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
// 提供Blake2B系列散列算法函数和校验和函数。
|
||||
package blake2b
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"hash"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"golang.org/x/crypto/blake2b"
|
||||
)
|
||||
|
||||
// 根据给定的位数返回一个散列算法的Hash实例。
|
||||
func hasherSelect(bitSize int) hash.Hash {
|
||||
switch bitSize {
|
||||
case 224:
|
||||
hasher, _ := blake2b.New(28, nil)
|
||||
return hasher
|
||||
case 256:
|
||||
hasher, _ := blake2b.New256(nil)
|
||||
return hasher
|
||||
case 384:
|
||||
hasher, _ := blake2b.New384(nil)
|
||||
return hasher
|
||||
case 512:
|
||||
hasher, _ := blake2b.New512(nil)
|
||||
return hasher
|
||||
default:
|
||||
hasher, _ := blake2b.New256(nil)
|
||||
return hasher
|
||||
}
|
||||
}
|
||||
|
||||
// 计算给定字节数组的Blake2B校验和,返回字节数组。
|
||||
func Blake2b(data []byte) []byte {
|
||||
hasher := hasherSelect(512)
|
||||
hasher.Write(data)
|
||||
return hasher.Sum(nil)
|
||||
}
|
||||
|
||||
// 计算给定字节数组的Blake2B/256校验和,返回字节数组。
|
||||
func Blake2b_256(data []byte) []byte {
|
||||
hasher := hasherSelect(256)
|
||||
hasher.Write(data)
|
||||
return hasher.Sum(nil)
|
||||
}
|
||||
|
||||
// 计算给定字节数组的Blake2B/384校验和,返回字节数组。
|
||||
func Blake2b_384(data []byte) []byte {
|
||||
hasher := hasherSelect(384)
|
||||
hasher.Write(data)
|
||||
return hasher.Sum(nil)
|
||||
}
|
||||
|
||||
// 计算给定字节数组的Blake2B/224校验和,返回字节数组。
|
||||
func Blake2b_224(data []byte) []byte {
|
||||
hasher := hasherSelect(224)
|
||||
hasher.Write(data)
|
||||
return hasher.Sum(nil)
|
||||
}
|
||||
|
||||
// 计算给定字节数组的Blake2B校验和,返回十六进制字符串。
|
||||
func Blake2bHex(data []byte) string {
|
||||
return hex.EncodeToString(Blake2b(data))
|
||||
}
|
||||
|
||||
// 计算给定字节数组的Blake2B/256校验和,返回十六进制字符串。
|
||||
func Blake2b_256Hex(data []byte) string {
|
||||
return hex.EncodeToString(Blake2b_256(data))
|
||||
}
|
||||
|
||||
// 计算给定字节数组的Blake2B/384校验和,返回十六进制字符串。
|
||||
func Blake2b_384Hex(data []byte) string {
|
||||
return hex.EncodeToString(Blake2b_384(data))
|
||||
}
|
||||
|
||||
// 计算给定字节数组的Blake2B/224校验和,返回十六进制字符串。
|
||||
func Blake2b_224Hex(data []byte) string {
|
||||
return hex.EncodeToString(Blake2b_224(data))
|
||||
}
|
||||
|
||||
// 根据给定位数计算一个字节数组的Blake2B校验和,返回字节数组。
|
||||
func Sum(data []byte, bitSize ...int) []byte {
|
||||
hasher := hasherSelect(append(bitSize, 512)[0])
|
||||
hasher.Write(data)
|
||||
return hasher.Sum(nil)
|
||||
}
|
||||
|
||||
// 根据给定位数计算一个字节数组的Blake2B校验和,返回十六进制字符串。
|
||||
func SumHex(data []byte, bitSize ...int) string {
|
||||
return hex.EncodeToString(Sum(data, bitSize...))
|
||||
}
|
||||
|
||||
// 根据给定位数计算一个文件的Blake2B校验和,返回字节数组。
|
||||
func SumFile(file string, bitSize ...int) ([]byte, error) {
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("未能打开指定文件,%w", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
hasher := hasherSelect(append(bitSize, 512)[0])
|
||||
if _, err := io.Copy(hasher, f); err != nil {
|
||||
return nil, fmt.Errorf("未能读取指定文件的内容,%w", err)
|
||||
}
|
||||
return hasher.Sum(nil), nil
|
||||
}
|
||||
|
||||
// 根据给定位数计算一个文件的Blake2B校验和,返回十六进制字符串。
|
||||
func SumFileHex(file string, bitSize ...int) (string, error) {
|
||||
hash, err := SumFile(file, bitSize...)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(hash), nil
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
// 提供Blake3系列散列算法函数和校验和函数。
|
||||
package blake3
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"hash"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"lukechampine.com/blake3"
|
||||
)
|
||||
|
||||
// 根据给定的位数返回一个散列算法的Hash实例。
|
||||
func hasherSelect(bitSize int) hash.Hash {
|
||||
switch bitSize {
|
||||
case 224:
|
||||
return blake3.New(28, nil)
|
||||
case 256:
|
||||
return blake3.New(32, nil)
|
||||
case 384:
|
||||
return blake3.New(48, nil)
|
||||
case 512:
|
||||
return blake3.New(64, nil)
|
||||
default:
|
||||
return blake3.New(32, nil)
|
||||
}
|
||||
}
|
||||
|
||||
// 计算给定字节数组的Blake3校验和,返回字节数组。
|
||||
func Blake3(data []byte) []byte {
|
||||
hasher := hasherSelect(512)
|
||||
hasher.Write(data)
|
||||
return hasher.Sum(nil)
|
||||
}
|
||||
|
||||
// 计算给定字节数组的Blake3/256校验和,返回字节数组。
|
||||
func Blake3_256(data []byte) []byte {
|
||||
hasher := hasherSelect(256)
|
||||
hasher.Write(data)
|
||||
return hasher.Sum(nil)
|
||||
}
|
||||
|
||||
// 计算给定字节数组的Blake3/384校验和,返回字节数组。
|
||||
func Blake3_384(data []byte) []byte {
|
||||
hasher := hasherSelect(384)
|
||||
hasher.Write(data)
|
||||
return hasher.Sum(nil)
|
||||
}
|
||||
|
||||
// 计算给定字节数组的Blake3/224校验和,返回字节数组。
|
||||
func Blake3_224(data []byte) []byte {
|
||||
hasher := hasherSelect(224)
|
||||
hasher.Write(data)
|
||||
return hasher.Sum(nil)
|
||||
}
|
||||
|
||||
// 计算给定字节数组的Blake3校验和,返回十六进制字符串。
|
||||
func Blake3Hex(data []byte) string {
|
||||
return hex.EncodeToString(Blake3(data))
|
||||
}
|
||||
|
||||
// 计算给定字节数组的Blake3/256校验和,返回十六进制字符串。
|
||||
func Blake3_256Hex(data []byte) string {
|
||||
return hex.EncodeToString(Blake3_256(data))
|
||||
}
|
||||
|
||||
// 计算给定字节数组的Blake3/384校验和,返回十六进制字符串。
|
||||
func Blake3_384Hex(data []byte) string {
|
||||
return hex.EncodeToString(Blake3_384(data))
|
||||
}
|
||||
|
||||
// 计算给定字节数组的Blake3/224校验和,返回十六进制字符串。
|
||||
func Blake3_224Hex(data []byte) string {
|
||||
return hex.EncodeToString(Blake3_224(data))
|
||||
}
|
||||
|
||||
// 根据给定位数计算一个字节数组的Blake3校验和,返回字节数组。
|
||||
func Sum(data []byte, bitSize ...int) []byte {
|
||||
hasher := hasherSelect(append(bitSize, 512)[0])
|
||||
hasher.Write(data)
|
||||
return hasher.Sum(nil)
|
||||
}
|
||||
|
||||
// 根据给定位数计算一个字节数组的Blake3校验和,返回十六进制字符串。
|
||||
func SumHex(data []byte, bitSize ...int) string {
|
||||
return hex.EncodeToString(Sum(data, bitSize...))
|
||||
}
|
||||
|
||||
// 根据给定位数计算一个文件的Blake3校验和,返回字节数组。
|
||||
func SumFile(file string, bitSize ...int) ([]byte, error) {
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("未能打开指定文件,%w", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
hasher := hasherSelect(append(bitSize, 512)[0])
|
||||
if _, err := io.Copy(hasher, f); err != nil {
|
||||
return nil, fmt.Errorf("未能读取指定文件的内容,%w", err)
|
||||
}
|
||||
return hasher.Sum(nil), nil
|
||||
}
|
||||
|
||||
// 根据给定位数计算一个文件的Blake3校验和,返回十六进制字符串。
|
||||
func SumFileHex(file string, bitSize ...int) (string, error) {
|
||||
hash, err := SumFile(file, bitSize...)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(hash), nil
|
||||
}
|
||||
Reference in New Issue
Block a user