目录
前言
一、首先以md4为例
(一)16进制字符串的md4
(二)字符串的md4
(三)16进制字符串、字符串封装
二、md4、md5、sha1、ripemd160、sha256、sha512
(一)导包
(二)单个使用
(三)md4、md5、sha1、ripemd160、sha256、sha512封装
三、两次sha256
(一)返回两次哈希256后字节数组
(二)返回两次哈希256后字符串
前言
一、首先以md4为例
(一)16进制字符串的md4
1、"5A24"=>0x5A24。
arr, _ := hex.DecodeString("5A24")
2、完整代码:
// 参数为16进制字符串(必须偶数个,两位16进制为1个字节)
func MD4HexString(text string) string {
var hashInstance hash.Hash // 定义哈希实例
hashInstance = md4.New() // 实例赋值 "golang.org/x/crypto/md4"
arr, _ := hex.DecodeString(text) // 十六进制字符串转为十六进制字节数组
hashInstance.Write(arr) // 写入哈希实例对象
bytes := hashInstance.Sum(nil) // 哈希值追加到参数后面,只获取原始值,不用追加,用nil,返回哈希值字节数组
return fmt.Sprintf("%x", bytes) // 格式化输出哈希值
}
(二)字符串的md4
1、将字符串转换为字节数组
arr := []byte("字符串")
2、完整代码
// 参数为字符串
func MD4String(text string) string {
var hashInstance hash.Hash // 定义哈希实例
hashInstance = md4.New() // 实例赋值 "golang.org/x/crypto/md4"
arr := []byte(text) // 十六进制字符串转为十六进制字节数组
hashInstance.Write(arr) // 写入哈希实例对象
bytes := hashInstance.Sum(nil) // 哈希值追加到参数后面,只获取原始值,不用追加,用nil,返回哈希值字节数组
return fmt.Sprintf("%x", bytes) // 格式化输出哈希值
}
(三)16进制字符串、字符串封装
// 16进制字符串哈希:
// 第1个参数为16进制字符串(必须偶数个)时,第2个参数为:true。
// 字符串哈希:
// 第1个参数为字符串时,第2个参数为:false。
func MD4(text string, isHex bool) string {
var hashInstance hash.Hash // 定义哈希实例
hashInstance = md4.New() // 实例赋值
if isHex {
arr, _ := hex.DecodeString(text) // 16进制字符串转为16进制字节数组
hashInstance.Write(arr) // 写入哈希实例对象
} else {
hashInstance.Write([]byte(text)) // 将字符串转换为字节数组,写入哈希对象
}
bytes := hashInstance.Sum(nil) // 哈希值追加到参数后面,只获取原始值,不用追加,用nil,返回哈希值字节数组
return fmt.Sprintf("%x", bytes) // 格式化输出哈希值
}
二、md4、md5、sha1、ripemd160、sha256、sha512
(一)导包
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
"hash"
"golang.org/x/crypto/md4"
"golang.org/x/crypto/ripemd160"
)
(二)单个使用
- 举一反三,根据md4的使用,我们可以将代码简单的修改以实现其他哈希算法。
- 只需要将md4代码中的一句代码按照需求进行修改即可完成,不要忘记导包:
hashInstance = md4.New() // md4
hashInstance = md5.New() // md5
hashInstance = sha1.New() // sha1
hashInstance = ripemd160.New() // ripemd160
hashInstance = sha256.New() // sha256
hashInstance = sha512.New() // sha512
(三)md4、md5、sha1、ripemd160、sha256、sha512封装
// 根据不同哈希类型进行哈希:md4、md5、sha1、ripemd160、sha256、sha512
func HASH(text string, hashType string, isHex bool) string {
var hashInstance hash.Hash // 定义哈希实例
switch hashType { // 选择哈希类型
case "md4":
hashInstance = md4.New() // "golang.org/x/crypto/md4"
case "md5":
hashInstance = md5.New()
case "sha1":
hashInstance = sha1.New()
case "ripemd160":
hashInstance = ripemd160.New() // "golang.org/x/crypto/ripemd160"
case "sha256":
hashInstance = sha256.New()
case "sha512":
hashInstance = sha512.New()
}
if isHex {
arr, _ := hex.DecodeString(text) // 十六进制字符串转为十六进制字节数组
hashInstance.Write(arr) // 写入哈希实例对象
} else {
hashInstance.Write([]byte(text)) // 将字符串转换为字节数组,写入哈希对象
}
bytes := hashInstance.Sum(nil) // 哈希值追加到参数后面,只获取原始值,不用追加,用nil,返回哈希值字节数组
return fmt.Sprintf("%x", bytes) // 格式化输出哈希值
}
(四)使用crypto.Hash简化代码
func HASH(text string, myhash crypto.Hash, isHex bool) string {
var hashInstance hash.Hash // 定义哈希实例
hashInstance = myhash.New()
if isHex {
arr, _ := hex.DecodeString(text) // 十六进制字符串转为十六进制字节数组
hashInstance.Write(arr) // 写入哈希实例对象
} else {
hashInstance.Write([]byte(text)) // 将字符串转换为字节数组,写入哈希对象
}
bytes := hashInstance.Sum(nil) // 哈希值追加到参数后面,只获取原始值,不用追加,用nil,返回哈希值字节数组
return fmt.Sprintf("%x", bytes) // 格式化输出哈希值
}
三、两次sha256
- 有连续两次哈希需求时,为了不重复创建哈希对象,造成内存的浪费,可以单独封装函数。
- 哈希完成一次后,使用以下语句,清除哈希对象内容,然后进行第二次哈希。
hashInstance.Reset() // 重置哈希实例
(一)返回两次哈希256后字节数组
// 两次哈希256后的字节数组,第二次是将第一次哈希后的16进制进行哈希
func SHA256Double(text string, isHex bool) []byte {
hashInstance := sha256.New() // 实例赋值
if isHex {
arr, _ := hex.DecodeString(text) // 十六进制字符串转为十六进制字节数组
hashInstance.Write(arr) // 写入哈希实例对象
} else {
hashInstance.Write([]byte(text)) // 将字符串转换为字节数组,写入哈希对象
}
bytes := hashInstance.Sum(nil) // 哈希值追加到参数后面,只获取原始值,不用追加,用nil,返回哈希值字节数组
hashInstance.Reset() // 重置哈希实例
hashInstance.Write(bytes) // 将第一次哈希值写入哈希对象
bytes = hashInstance.Sum(nil) // 获取第二次哈希字节数组
return bytes
}
(二)返回两次哈希256后字符串
// 两次哈希256后的哈希字符串,第二次是将第一次哈希后的16进制进行哈希
func SHA256DoubleString(text string, isHex bool) string {
return fmt.Sprintf("%x", SHA256Double(text, isHex))
}