竹简文档

加密哈希

SHA-256 和 MD5 哈希计算

加密哈希

xUtil 提供 SHA-256 和 MD5 哈希计算函数。

SHA-256 哈希

Hash

计算字符串的 SHA-256 哈希:

encryption.go
func Hash(data string) string

返回: 64 位小写十六进制字符串。

hash := xUtil.Hash("password123")
// "ef92b778bafe771e89245b89ecbc08a44a4e166c06659911881f383d4473e94f"

HashBytes

计算字节切片的 SHA-256 哈希:

encryption.go
func HashBytes(data []byte) string
data := []byte("password123")
hash := xUtil.HashBytes(data)

MD5 哈希

MD5Hash

计算字符串的 MD5 哈希:

encryption.go
func MD5Hash(str string) string

返回: 32 位小写十六进制字符串。

hash := xUtil.MD5Hash("password123")
// "482c811da5d5b4bc6d497ffa98491e38"

MD5HashBytes

计算字节切片的 MD5 哈希:

encryption.go
func MD5HashBytes(data []byte) string
data := []byte("password123")
hash := xUtil.MD5HashBytes(data)

使用场景

数据签名

func SignRequest(data string, secret string) string {
    // 使用 SHA-256 生成签名
    return xUtil.Hash(data + secret)
}

文件校验

func ChecksumFile(content []byte) string {
    // 使用 MD5 生成文件校验和
    return xUtil.MD5HashBytes(content)
}

缓存键生成

func GenerateCacheKey(params map[string]string) string {
    // 使用 MD5 生成缓存键
    data, _ := json.Marshal(params)
    return "cache:" + xUtil.MD5HashBytes(data)
}

注意事项

  • MD5 不安全:MD5 已被证明存在碰撞漏洞,不应用于密码存储或安全敏感场景
  • 密码存储:请使用 xUtil.EncryptPassword 进行密码加密,它使用 bcrypt 算法
  • SHA-256:适用于数据签名、完整性校验等场景

下一步

On this page