feat(hash): 添加 BLAKE2b 校验和算法支持
- 在 `hash` 模块中新增 `blake2b` 子包,实现 BLAKE2b 系列散列算法 - 支持多种输出长度:224、256、384 和 512 位 - 提供字节及十六进制字符串两种格式的散列计算函数 - 支持对字节数组和文件进行散列计算 - 更新 go.mod 使用 Go 1.24.0 并引入 golang.org/x/crypto 和 golang.org/x/sys 依赖 - 更新 README.md 标记 BLAKE2b 功能为已完成
This commit is contained in:
@@ -22,7 +22,7 @@ Golang 中可以使用的常用辅助功能工具箱。主要配备以下功能
|
||||
- [x] KeyPair 生成器
|
||||
- [x] Key 导入与导出
|
||||
- [x] RSA 签名算法
|
||||
- 散列及校验和算法。
|
||||
- 散列及校验和算法
|
||||
- [x] Sha512 散列算法(便捷封装)
|
||||
- [x] Sha256 散列算法(便捷封装)
|
||||
- [x] Sha1 散列算法(便捷封装)
|
||||
@@ -32,7 +32,7 @@ Golang 中可以使用的常用辅助功能工具箱。主要配备以下功能
|
||||
- [x] CRC32 校验和算法(便捷封装)
|
||||
- [x] CRC64 校验和算法(便捷封装)
|
||||
- [x] pHash 图像感知算法(便捷封装)
|
||||
- [ ] BLAKE2 校验和算法(便捷封装)
|
||||
- [x] BLAKE2b 校验和算法(便捷封装)
|
||||
- [ ] BLAKE3 校验和算法(便捷封装)
|
||||
- [ ] BlockHash 散列算法
|
||||
- 唯一序列号生成器
|
||||
|
4
go.mod
4
go.mod
@@ -1,6 +1,6 @@
|
||||
module archgrid.xyz/ag/toolsbox
|
||||
|
||||
go 1.20
|
||||
go 1.24.0
|
||||
|
||||
require go.uber.org/zap v1.24.0
|
||||
|
||||
@@ -8,6 +8,7 @@ require (
|
||||
github.com/azr/gift v1.1.2 // indirect
|
||||
github.com/disintegration/imaging v1.6.2 // indirect
|
||||
golang.org/x/image v0.0.0-20220722155232-062f8c9fd539 // indirect
|
||||
golang.org/x/sys v0.36.0 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -17,4 +18,5 @@ 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
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@@ -21,9 +21,13 @@ 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=
|
||||
|
117
hash/blake2b/blake2b.go
Normal file
117
hash/blake2b/blake2b.go
Normal file
@@ -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.New256(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
|
||||
}
|
Reference in New Issue
Block a user