feat(hash): 添加 BLAKE3 校验和算法支持
- 在 `README.md` 中将 BLAKE3 校验和算法标记为已完成 - 引入 `lukechampine.com/blake3` 依赖以实现 BLAKE3 算法功能 - 新增 `hash/blake3` 包,提供多种 BLAKE3 哈希计算函数 - 支持 BLAKE3、BLAKE3/224、BLAKE3/256、BLAKE3/384 等变种 - 提供字节数组与文件的哈希计算及十六进制编码结果 - 更新 `go.mod` 和 `go.sum` 以包含新增依赖项
This commit is contained in:
112
hash/blake3/blake3.go
Normal file
112
hash/blake3/blake3.go
Normal file
@@ -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