使用Go语言实现常见hash算法

 更新时间:2024年01月14日 09:05:03   作者: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算法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • golang将切片或数组根据某个字段进行分组操作

    golang将切片或数组根据某个字段进行分组操作

    这篇文章主要介绍了golang将切片或数组根据某个字段进行分组操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • go语言实现sqrt的方法

    go语言实现sqrt的方法

    这篇文章主要介绍了go语言实现sqrt的方法,实例分析了Go语言实现计算平方根的技巧,需要的朋友可以参考下
    2015-03-03
  • Go语言通过chan进行数据传递的方法详解

    Go语言通过chan进行数据传递的方法详解

    这篇文章主要为大家详细介绍了Go语言如何通过chan进行数据传递的功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起了解一下
    2023-06-06
  • goland远程调试k8s上容器的实现

    goland远程调试k8s上容器的实现

    本文主要介绍了goland远程调试k8s上容器的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • Go GORM版本2.0新特性介绍

    Go GORM版本2.0新特性介绍

    这篇文章主要为大家介绍了Go GORM版本2.0新特性的使用示例介绍,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • Go扩展原语之SingleFlight的用法详解

    Go扩展原语之SingleFlight的用法详解

    Go语言扩展包同步原语singleflight.Group能够再一个服务中抑制对下游的多次重复请求,它能够限制对同一个键值对的多次重复请求,减少对下游的瞬时流量,接下来小编就给大家讲讲Go SingleFlight的具体用法,需要的朋友可以参考下
    2023-07-07
  • 浅谈go语言renderer包代码分析

    浅谈go语言renderer包代码分析

    本篇文章主要介绍了浅谈go语言renderer包代码分析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • Golang泛型的使用方法详解

    Golang泛型的使用方法详解

    这篇文章主要介绍了Golang中泛型的使用,Go和Python语言不同,处理不同数据类型非常严格。如Python可以定义函数带两个数值类型并返回较大的数值,但可以不严格指定参数类型为float或integer
    2022-12-12
  • Go基础教程系列之WaitGroup用法实例详解

    Go基础教程系列之WaitGroup用法实例详解

    这篇文章主要介绍了Go基础教程系列之WaitGroup用法实例详解,需要的朋友可以参考下
    2022-04-04
  • 高效封禁:利用Go封装功能,提升封禁操作效率

    高效封禁:利用Go封装功能,提升封禁操作效率

    在网络安全领域,封禁操作是一项重要的任务,用于阻止恶意行为和保护系统安全,而利用Go语言封装功能可以提升封禁操作的效率,Go语言具有高效的并发性能和简洁的语法,使得开发者可以快速构建高性能的封禁系统,
    2023-10-10

最新评论