当前位置: 首页 > news >正文

Go语言实现各种hash算法

Go语言实现各种hash算法

1、各种hash算法的实现

package mainimport ("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/md4MD5                         // import crypto/md5SHA1                        // import crypto/sha1SHA224                      // import crypto/sha256SHA256                      // import crypto/sha256SHA384                      // import crypto/sha512SHA512                      // import crypto/sha512MD5SHA1                     // no implementation; MD5+SHA1 used for TLS RSARIPEMD160                   // import golang.org/x/crypto/ripemd160SHA3_224                    // import golang.org/x/crypto/sha3SHA3_256                    // import golang.org/x/crypto/sha3SHA3_384                    // import golang.org/x/crypto/sha3SHA3_512                    // import golang.org/x/crypto/sha3SHA512_224                  // import crypto/sha512SHA512_256                  // import crypto/sha512BLAKE2s_256                 // import golang.org/x/crypto/blake2sBLAKE2b_256                 // import golang.org/x/crypto/blake2bBLAKE2b_384                 // import golang.org/x/crypto/blake2bBLAKE2b_512                 // import golang.org/x/crypto/blake2bmaxHash
)

2、两次hash

  • 有连续两次哈希需求时,为了不重复创建哈希对象,造成内存的浪费,可以单独封装函数。
  • 哈希完成一次后,使用以下语句,清除哈希对象内容,然后进行第二次哈希。
hashInstance.Reset() // 重置哈希实例

完整代码:

package mainimport ("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

相关文章:

  • C //练习 4-4 在栈操作中添加几个命令,分别用在不弹出元素的情况下打印栈顶元素;复制栈顶元素;交换栈顶两个元素的值。另外增加一个命令用于清空栈。
  • ssm基于Javaweb的网上奶茶店系统的设计与实现论文
  • xtu-c语言考试复习-2
  • RAG(检索增强生成 )
  • vercel部署twikoo后评论收不到通知邮件问题解决方法
  • 飞桨分子动力学模拟-论文复现第六期:复现TorchMD
  • 奇怪的事情记录:外置网卡和外置显示器不兼容
  • Ceph入门到精通-通过 CloudBerry Explorer 管理对象bucket
  • ssh远程登陆
  • Kubernetes 集群管理—日志架构
  • iOS14 Widget 小组件调研
  • UniRepLKNet实战:使用 UniRepLKNet实现图像分类任务(二)
  • 7个Linux搜索和过滤命令
  • Jmeter 性能-监控服务器
  • 多种采购方式下,数智化招标采购系统建设解决方案
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • C学习-枚举(九)
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • ERLANG 网工修炼笔记 ---- UDP
  • ESLint简单操作
  • HashMap ConcurrentHashMap
  • IDEA 插件开发入门教程
  • magento2项目上线注意事项
  • Meteor的表单提交:Form
  • Spring Cloud Alibaba迁移指南(一):一行代码从 Hystrix 迁移到 Sentinel
  • windows下如何用phpstorm同步测试服务器
  • 得到一个数组中任意X个元素的所有组合 即C(n,m)
  • 基于web的全景—— Pannellum小试
  • 简单数学运算程序(不定期更新)
  • 如何在 Tornado 中实现 Middleware
  • 深度解析利用ES6进行Promise封装总结
  • 微服务框架lagom
  • scrapy中间件源码分析及常用中间件大全
  • 阿里云ACE认证学习知识点梳理
  • 正则表达式-基础知识Review
  • ​比特币大跌的 2 个原因
  • "无招胜有招"nbsp;史上最全的互…
  • #include<初见C语言之指针(5)>
  • (007)XHTML文档之标题——h1~h6
  • (Redis使用系列) SpirngBoot中关于Redis的值的各种方式的存储与取出 三
  • (zt)最盛行的警世狂言(爆笑)
  • (论文阅读40-45)图像描述1
  • (转)母版页和相对路径
  • (转载)hibernate缓存
  • ****Linux下Mysql的安装和配置
  • .NET MVC 验证码
  • .net redis定时_一场由fork引发的超时,让我们重新探讨了Redis的抖动问题
  • .NET 反射的使用
  • .Net 应用中使用dot trace进行性能诊断
  • .Net 知识杂记
  • .Net 转战 Android 4.4 日常笔记(4)--按钮事件和国际化
  • .NET/C# 使用 SpanT 为字符串处理提升性能
  • .NET框架
  • .NET实现之(自动更新)
  • .net下的富文本编辑器FCKeditor的配置方法