使用Go语言实现常见hash算法
作者:242030
这篇文章主要为大家详细介绍了使语言实现各种常见hash算法的相关知识,文中的示例代码讲解详细,具有一定的借鉴价值,需要的小伙伴可以参考下
Go语言实现各种hash算法
1、各种hash算法的实现
package main
import (
"crypto"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
"hash"
"golang.org/x/crypto/md4"
"golang.org/x/crypto/ripemd160"
)
func main() {
str1 := HashByType("Hello World", "md4", false)
fmt.Println(str1)
str2 := HashByCrypto("Hello World", crypto.MD4, false)
fmt.Println(str2)
}
// 根据不同哈希类型进行哈希: md4、md5、sha1、ripemd160、sha256、sha512
func HashByType(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) // 格式化输出哈希值
}
// 根据不同哈希类型进行哈希: md4、md5、sha1、ripemd160、sha256、sha512
func HashByCrypto(text string, myhash crypto.Hash, isHex bool) string {
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) // 格式化输出哈希值
}
支持的Hash算法有:
const ( MD4 Hash = 1 + iota // import golang.org/x/crypto/md4 MD5 // import crypto/md5 SHA1 // import crypto/sha1 SHA224 // import crypto/sha256 SHA256 // import crypto/sha256 SHA384 // import crypto/sha512 SHA512 // import crypto/sha512 MD5SHA1 // no implementation; MD5+SHA1 used for TLS RSA RIPEMD160 // import golang.org/x/crypto/ripemd160 SHA3_224 // import golang.org/x/crypto/sha3 SHA3_256 // import golang.org/x/crypto/sha3 SHA3_384 // import golang.org/x/crypto/sha3 SHA3_512 // import golang.org/x/crypto/sha3 SHA512_224 // import crypto/sha512 SHA512_256 // import crypto/sha512 BLAKE2s_256 // import golang.org/x/crypto/blake2s BLAKE2b_256 // import golang.org/x/crypto/blake2b BLAKE2b_384 // import golang.org/x/crypto/blake2b BLAKE2b_512 // import golang.org/x/crypto/blake2b maxHash )
2、两次hash
- 有连续两次哈希需求时,为了不重复创建哈希对象,造成内存的浪费,可以单独封装函数。
- 哈希完成一次后,使用以下语句,清除哈希对象内容,然后进行第二次哈希。
hashInstance.Reset() // 重置哈希实例
完整代码:
package main
import (
"crypto"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
"hash"
"golang.org/x/crypto/md4"
"golang.org/x/crypto/ripemd160"
)
func main() {
str1 := HashByType("Hello World", "md4", false)
fmt.Println(str1)
str2 := HashByCrypto("Hello World", crypto.MD4, false)
fmt.Println(str2)
str3 := DoubleHashByCrypto("Hello World", crypto.MD4, false)
fmt.Println(str3)
}
// 根据不同哈希类型进行哈希: md4、md5、sha1、ripemd160、sha256、sha512
func HashByType(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) // 格式化输出哈希值
}
// 根据不同哈希类型进行哈希: md4、md5、sha1、ripemd160、sha256、sha512
func HashByCrypto(text string, myhash crypto.Hash, isHex bool) string {
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) // 格式化输出哈希值
}
// 根据不同哈希类型进行哈希: md4、md5、sha1、ripemd160、sha256、sha512
// 两次哈希256后的字节数组,第二次是将第一次哈希后的16进制进行哈希
func DoubleHashByCrypto(text string, myhash crypto.Hash, isHex bool) string {
hashInstance := myhash.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 fmt.Sprintf("%x", bytes) // 格式化输出哈希值
}
输出
77a781b995cf1cfaf39d9e2f5910c2cf
77a781b995cf1cfaf39d9e2f5910c2cf
487b5e8e42dcd5b4cc218c2e275561fb
到此这篇关于使用Go语言实现常见hash算法的文章就介绍到这了,更多相关Go hash算法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
