分享
go语言之各种加密算法的使用
bojie5744 · · 4703 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
go语言之各种加密算法的使用
加密算法常用于数据传输中的封装,下面看看如何使用go语言库封装好的加密算法。
直接看代码。
packagemain
import(
"encoding/base64"
"crypto/md5"
"encoding/hex"
"fmt"
)
constbase64Table="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
varcoder=base64.NewEncoding(base64Table)
funcbase64Encode(encode_byte[]byte)[]byte{
return[]byte(coder.EncodeToString(encode_byte))
}
funcbase64Decode(decode_byte[]byte)([]byte,error){
returncoder.DecodeString(string(decode_byte))
}
funcMD5(b[]byte)string{
h:=md5.New()
h.Write(b)
x:=h.Sum(nil)
y:=make([]byte,32)
hex.Encode(y,x)
returnstring(y)
}
funcmain(){
encode_str:="博客地址:http://blog.csdn.net/bojie5744"
decode_byte:=base64Encode([]byte(encode_str))
fmt.Printf("%s\n",string(decode_byte))
encode_byte,_:=base64Decode(decode_byte)
fmt.Println(string(encode_byte))
fmt.Println(MD5([]byte(encode_str)))
}
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信4703 次点击
上一篇:[go语言]控制语句
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
go语言之各种加密算法的使用
加密算法常用于数据传输中的封装,下面看看如何使用go语言库封装好的加密算法。
直接看代码。
packagemain
import(
"encoding/base64"
"crypto/md5"
"encoding/hex"
"fmt"
)
constbase64Table="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
varcoder=base64.NewEncoding(base64Table)
funcbase64Encode(encode_byte[]byte)[]byte{
return[]byte(coder.EncodeToString(encode_byte))
}
funcbase64Decode(decode_byte[]byte)([]byte,error){
returncoder.DecodeString(string(decode_byte))
}
funcMD5(b[]byte)string{
h:=md5.New()
h.Write(b)
x:=h.Sum(nil)
y:=make([]byte,32)
hex.Encode(y,x)
returnstring(y)
}
funcmain(){
encode_str:="博客地址:http://blog.csdn.net/bojie5744"
decode_byte:=base64Encode([]byte(encode_str))
fmt.Printf("%s\n",string(decode_byte))
encode_byte,_:=base64Decode(decode_byte)
fmt.Println(string(encode_byte))
fmt.Println(MD5([]byte(encode_str)))
}