- 在 `hash` 模块中新增 `blake2b` 子包,实现 BLAKE2b 系列散列算法 - 支持多种输出长度:224、256、384 和 512 位 - 提供字节及十六进制字符串两种格式的散列计算函数 - 支持对字节数组和文件进行散列计算 - 更新 go.mod 使用 Go 1.24.0 并引入 golang.org/x/crypto 和 golang.org/x/sys 依赖 - 更新 README.md 标记 BLAKE2b 功能为已完成
117 lines
3.1 KiB
Go
117 lines
3.1 KiB
Go
// 提供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
|
||
} |