golang中字符串MD5生成方式总结


Posted in Golang onJuly 04, 2021

方案一

func md5V(str string) string  {
    h := md5.New()
    h.Write([]byte(str))
    return hex.EncodeToString(h.Sum(nil))
}

方案二

func md5V2(str string) string {
    data := []byte(str)
    has := md5.Sum(data)
    md5str := fmt.Sprintf("%x", has)
    return md5str
}

方案三

func md5V3(str string) string {
    w := md5.New()
    io.WriteString(w, str)
    md5str := fmt.Sprintf("%x", w.Sum(nil))
    return md5str
}

整体测试代码

package main

import (
    "crypto/md5"
    "encoding/hex"
    "fmt"
    "io"
)
func main() {
    str := "MD5testing"
    md5Str := md5V(str)
    fmt.Println(md5Str)
    fmt.Println(md5V2(str))
    fmt.Println(md5V3(str))
}
// 输出结果:
f7bb96d1dcd6cfe0e5ce1f03e35f84bf
f7bb96d1dcd6cfe0e5ce1f03e35f84bf
f7bb96d1dcd6cfe0e5ce1f03e35f84bf

到此这篇关于golang中字符串MD5生成方式总结的文章就介绍到这了,更多相关golang中字符串MD5生成方式内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Golang 相关文章推荐
golang中的空slice案例
Apr 27 Golang
golang通过递归遍历生成树状结构的操作
Apr 28 Golang
golang goroutine顺序输出方式
Apr 29 Golang
golang DNS服务器的简单实现操作
Apr 30 Golang
基于Golang 高并发问题的解决方案
May 08 Golang
go语言基础 seek光标位置os包的使用
May 09 Golang
试了下Golang实现try catch的方法
Jul 01 Golang
golang fmt格式“占位符”的实例用法详解
Jul 04 Golang
golang用type-switch判断interface的实际存储类型
Apr 14 Golang
Golang解析JSON对象
Apr 30 Golang
Golang并发工具Singleflight
May 06 Golang
Go gorilla securecookie库的安装使用详解
Aug 14 Golang
golang fmt格式“占位符”的实例用法详解
Jul 04 #Golang
Go语言空白表示符_的实例用法
Jul 04 #Golang
Go 语言结构实例分析
Jul 04 #Golang
Go语言基础知识点介绍
Jul 04 #Golang
详解Go语言Slice作为函数参数的使用
Jul 02 #Golang
golang 实用库gotable的具体使用
Jul 01 #Golang
试了下Golang实现try catch的方法
Jul 01 #Golang
You might like
Re:从零开始的异世界生活 第2季 开播啦
2020/07/24 日漫
国内php原创论坛
2006/10/09 PHP
PHP4实际应用经验篇(7)
2006/10/09 PHP
PHP删除数组中的特定元素的代码
2012/06/28 PHP
ThinkPHP之R方法实例详解
2014/06/20 PHP
php使用ZipArchive提示Fatal error: Class ZipArchive not found in的解决方法
2014/11/04 PHP
搭建PhpStorm+PhpStudy开发环境的超详细教程
2020/09/17 PHP
JSON 编辑器实现代码
2009/12/06 Javascript
jquery中页面Ajax方法$.load的功能使用介绍
2014/10/20 Javascript
node.js中RPC(远程过程调用)的实现原理介绍
2014/12/05 Javascript
JavaScript之数组(Array)详解
2015/04/01 Javascript
javascript中JSON对象与JSON字符串相互转换实例
2015/07/11 Javascript
深入学习JavaScript中的Rest参数和参数默认值
2015/07/28 Javascript
js实现微博发布小功能
2017/01/12 Javascript
浅谈jQuery的bind和unbind事件(绑定和解绑事件)
2017/03/02 Javascript
Python使用matplotlib填充图形指定区域代码示例
2018/01/16 Python
Python装饰器(decorator)定义与用法详解
2018/02/09 Python
在PyCharm环境中使用Jupyter Notebook的两种方法总结
2018/05/24 Python
pytorch获取vgg16-feature层输出的例子
2019/08/20 Python
Python json模块与jsonpath模块区别详解
2020/03/05 Python
Python实现密钥密码(加解密)实例详解
2020/04/26 Python
纯CSS3打造动感漂亮时尚的扇形菜单
2014/03/18 HTML / CSS
英国高级健康和美容产品零售商:Life and Looks
2019/08/01 全球购物
普通简短的个人自我评价
2014/02/15 职场文书
写求职信有什么意义
2014/02/17 职场文书
企业贷款委托书格式
2014/09/12 职场文书
机关党总支领导班子整改方案
2014/09/20 职场文书
办公楼租房协议书范本
2014/11/25 职场文书
市场营销计划书范文
2015/01/16 职场文书
工作检讨书范文
2015/01/23 职场文书
胡雪岩故居导游词
2015/02/06 职场文书
关于职业道德的心得体会
2016/01/18 职场文书
canvas多重阴影发光效果实现
2021/04/20 Javascript
如何正确理解python装饰器
2021/06/15 Python
为了顺利买到演唱会的票用Python制作了自动抢票的脚本
2021/10/16 Python