Documentation
¶
Overview ¶
Package hash 提供多种哈希算法和HMAC计算功能,支持泛型编程
核心特性:
- 支持多种哈希算法:MD5、SHA1、SHA256、SHA512
- 支持HMAC(基于哈希的消息认证码)计算
- 支持Bcrypt密码哈希(用于安全的密码存储和验证)
- 提供泛型函数,可扩展支持自定义哈希算法
- 支持多种输出格式:原始字节、16位/32位/完整长度十六进制字符串
- 提供便捷方法,通过 util.Hash 实例快速调用
- 支持 io.Reader 数据源,可处理流式数据
基本用法:
// 计算SHA256哈希值(返回十六进制字符串)
hashStr := util.Hash.SHA256String("hello world")
fmt.Println(hashStr) // b94d27b9...
// 计算HMAC-SHA256(需要密钥)
hmacStr := util.Hash.HMACSHA256String("data", "secret-key")
fmt.Println(hmacStr)
// 计算MD5并返回16位短格式
md5Short := util.Hash.MD5String16("filename")
// 使用 Bcrypt 哈希密码(安全存储)
hashedPassword, err := util.Hash.BcryptHash("mypassword")
if err != nil {
logger.Fatal("密码哈希失败", "error", err)
}
// 验证密码
isValid := util.Hash.BcryptVerify("mypassword", hashedPassword)
// 使用泛型函数计算哈希值
hashBytes := hash.HashGeneric("data", hash.SHA256Algorithm{})
// 从文件计算哈希值
file, _ := os.Open("file.txt")
defer file.Close()
fileHash, err := hash.HashReaderStringGeneric(file, hash.SHA256Algorithm{})
if err != nil {
logger.Fatal("文件哈希计算失败", "error", err)
}
泛型支持:
通过泛型函数 HashGeneric 和 HMACGeneric,可以扩展支持自定义哈希算法。
只需实现 HashAlgorithm 接口即可:
type CustomAlgorithm struct{}
func (CustomAlgorithm) Hash() hash.Hash {
return sha3.New256() // 或其他哈希算法
}
result := hash.HashGeneric("data", CustomAlgorithm{})
输出格式:
Package hash 支持以下输出格式: - FormatBytes: 原始字节数组 - FormatHexShort: 16位十六进制字符串(常用于MD5短格式) - FormatHexMedium: 32位十六进制字符串 - FormatHexFull: 完整长度十六进制字符串(默认)
Index ¶
- Constants
- Variables
- func HMACBytesGeneric[T HashAlgorithm](data []byte, key []byte, algorithm T) []byte
- func HMACBytesHexGeneric[T HashAlgorithm](data []byte, key []byte, algorithm T, format HashOutputFormat) string
- func HMACGeneric[T HashAlgorithm](data string, key string, algorithm T) []byte
- func HMACHexGeneric[T HashAlgorithm](data string, key string, algorithm T, format HashOutputFormat) string
- func HMACReaderGeneric[T HashAlgorithm](r io.Reader, key []byte, algorithm T) ([]byte, error)
- func HMACReaderHexGeneric[T HashAlgorithm](r io.Reader, key []byte, algorithm T, format HashOutputFormat) (string, error)
- func HMACReaderStringGeneric[T HashAlgorithm](r io.Reader, key []byte, algorithm T) (string, error)
- func HMACStringGeneric[T HashAlgorithm](data string, key string, algorithm T) string
- func HashBytesGeneric[T HashAlgorithm](data []byte, algorithm T) []byte
- func HashBytesHexGeneric[T HashAlgorithm](data []byte, algorithm T, format HashOutputFormat) string
- func HashGeneric[T HashAlgorithm](data string, algorithm T) []byte
- func HashHexGeneric[T HashAlgorithm](data string, algorithm T, format HashOutputFormat) string
- func HashReaderGeneric[T HashAlgorithm](r io.Reader, algorithm T) ([]byte, error)
- func HashReaderHexGeneric[T HashAlgorithm](r io.Reader, algorithm T, format HashOutputFormat) (string, error)
- func HashReaderStringGeneric[T HashAlgorithm](r io.Reader, algorithm T) (string, error)
- func HashStringGeneric[T HashAlgorithm](data string, algorithm T) string
- type HashAlgorithm
- type HashOutputFormat
- type MD5Algorithm
- type SHA1Algorithm
- type SHA256Algorithm
- type SHA512Algorithm
Constants ¶
const BcryptDefaultCost = bcrypt.DefaultCost
BcryptDefaultCost 默认 bcrypt 成本因子
Variables ¶
var Hash = &hashEngine{}
Hash 默认的哈希操作实例
Functions ¶
func HMACBytesGeneric ¶
func HMACBytesGeneric[T HashAlgorithm](data []byte, key []byte, algorithm T) []byte
HMACBytesGeneric 计算字节数组的HMAC哈希值
func HMACBytesHexGeneric ¶
func HMACBytesHexGeneric[T HashAlgorithm](data []byte, key []byte, algorithm T, format HashOutputFormat) string
HMACBytesHexGeneric 计算字节数组的HMAC哈希值并返回指定格式的十六进制字符串
func HMACGeneric ¶
func HMACGeneric[T HashAlgorithm](data string, key string, algorithm T) []byte
HMACGeneric 计算HMAC哈希值
func HMACHexGeneric ¶
func HMACHexGeneric[T HashAlgorithm](data string, key string, algorithm T, format HashOutputFormat) string
HMACHexGeneric 计算HMAC哈希值并返回指定格式的十六进制字符串
func HMACReaderGeneric ¶
HMACReaderGeneric 从io.Reader计算HMAC哈希值
func HMACReaderHexGeneric ¶
func HMACReaderHexGeneric[T HashAlgorithm](r io.Reader, key []byte, algorithm T, format HashOutputFormat) (string, error)
HMACReaderHexGeneric 从io.Reader计算HMAC哈希值并返回指定格式的十六进制字符串
func HMACReaderStringGeneric ¶
HMACReaderStringGeneric 从io.Reader计算HMAC并返回完整十六进制字符串
func HMACStringGeneric ¶
func HMACStringGeneric[T HashAlgorithm](data string, key string, algorithm T) string
HMACStringGeneric 计算HMAC并返回完整十六进制字符串
func HashBytesGeneric ¶
func HashBytesGeneric[T HashAlgorithm](data []byte, algorithm T) []byte
HashBytesGeneric 计算字节数组的哈希值
func HashBytesHexGeneric ¶
func HashBytesHexGeneric[T HashAlgorithm](data []byte, algorithm T, format HashOutputFormat) string
HashBytesHexGeneric 计算字节数组的哈希值并返回指定格式的十六进制字符串
func HashGeneric ¶
func HashGeneric[T HashAlgorithm](data string, algorithm T) []byte
HashGeneric 计算任意哈希算法的值
func HashHexGeneric ¶
func HashHexGeneric[T HashAlgorithm](data string, algorithm T, format HashOutputFormat) string
HashHexGeneric 计算哈希值并返回指定格式的十六进制字符串
func HashReaderGeneric ¶
func HashReaderGeneric[T HashAlgorithm](r io.Reader, algorithm T) ([]byte, error)
HashReaderGeneric 从io.Reader计算哈希值
func HashReaderHexGeneric ¶
func HashReaderHexGeneric[T HashAlgorithm](r io.Reader, algorithm T, format HashOutputFormat) (string, error)
HashReaderHexGeneric 从io.Reader计算哈希值并返回指定格式的十六进制字符串
func HashReaderStringGeneric ¶
func HashReaderStringGeneric[T HashAlgorithm](r io.Reader, algorithm T) (string, error)
HashReaderStringGeneric 从io.Reader计算哈希并返回完整十六进制字符串
func HashStringGeneric ¶
func HashStringGeneric[T HashAlgorithm](data string, algorithm T) string
HashStringGeneric 计算哈希并返回完整十六进制字符串
Types ¶
type HashOutputFormat ¶
type HashOutputFormat int
HashOutputFormat 哈希输出格式枚举
const ( // FormatBytes 原始字节数组格式 FormatBytes HashOutputFormat = iota // FormatHexShort 16位十六进制字符串(通常用于MD5短格式) FormatHexShort // FormatHexMedium 32位十六进制字符串(通常用于MD5、SHA256短格式) FormatHexMedium // FormatHexFull 完整长度十六进制字符串 FormatHexFull )
type MD5Algorithm ¶
type MD5Algorithm struct{}
MD5Algorithm MD5算法实现
func (MD5Algorithm) Hash ¶
func (MD5Algorithm) Hash() hash.Hash
type SHA1Algorithm ¶
type SHA1Algorithm struct{}
SHA1Algorithm SHA1算法实现
func (SHA1Algorithm) Hash ¶
func (SHA1Algorithm) Hash() hash.Hash
type SHA256Algorithm ¶
type SHA256Algorithm struct{}
SHA256Algorithm SHA256算法实现
func (SHA256Algorithm) Hash ¶
func (SHA256Algorithm) Hash() hash.Hash
type SHA512Algorithm ¶
type SHA512Algorithm struct{}
SHA512Algorithm SHA512算法实现
func (SHA512Algorithm) Hash ¶
func (SHA512Algorithm) Hash() hash.Hash