logo
当前位置:首 页 > 编程技术 >后端开发 >Go > 查看文章

golang获取MD5

Go, 后端开发, 编程技术 你是第1238个围观者 0条评论 供稿者: 标签:,

编程中 MD5的使用也是一个比较频繁的地方

 

h := md5.New()
	h.Write([]byte(s))
	return hex.EncodeToString(h.Sum(nil))

 

tips:记得引入包

 

上次说了时间这次加上时间戳,我们试着弄一个token的key生成函数

 

 

func GetToken(){
 
	crutime := time.Now().Unix()       /// 获取当前时间
	fmt.Println("crutime-->", crutime) 
 
	h := md5.New() /// 获取md5初始化
	fmt.Println("h-->", h)
 
	fmt.Println("strconv.FormatInt(crutime, 10)-->", strconv.FormatInt(crutime, 10))
	io.WriteString(h, strconv.FormatInt(crutime, 10))
 
	fmt.Println("h-->", h)
 
	token := fmt.Sprintf("%x", h.Sum(nil))
	fmt.Println("token--->", token)
 
	fmt.Println(len(token))
 
    return token
 
}

 

这样就可以了

 

 

 

 

说说梦想,谈谈感悟 ,聊聊技术,有啥要说的来github留言吧 https://github.com/cjx2328

—— 陈 建鑫

陈建鑫
你可能也喜欢Related Posts
footer logo
未经许可请勿自行使用、转载、修改、复制、发行、出售、发表或以其它方式利用本网站之内容。站长联系:cjx2328#126.com(修改#为@)
Copyright ©ziao Studio All Rights Reserved. E-mail:cjx2328#126.com(#号改成@) 沪ICP备14052271号-3