Go实战--go中使用hmac sha256(The way to go)
来源:互联网 发布:scrum 软件项目管理 编辑:程序博客网 时间:2024/04/26 16:44
声明不止,继续 go go go !!!
上一篇blog介绍了base64加密,今天继续介绍一下关于加密方面的实战应用,也就是hmac sha256.
何为hmac?
wiki:
In cryptography, a keyed-hash message authentication code (HMAC) is a specific type of message authentication code (MAC) involving a cryptographic hash function and a secret cryptographic key.
百度百科:
HMAC是密钥相关的哈希运算消息认证码,HMAC运算利用哈希算法,以一个密钥和一个消息为输入,生成一个消息摘要作为输出。
主要用于验证接口签名~
何为sha256?
哈希值用作表示大量数据的固定大小的唯一值。数据的少量更改会在哈希值中产生不可预知的大量更改。
SHA256 算法的哈希值大小为 256 位。
crypto/hmac
Package hmac implements the Keyed-Hash Message Authentication Code (HMAC) as defined in U.S. Federal Information Processing Standards Publication 198. An HMAC is a cryptographic hash that uses a key to sign a message. The receiver verifies the hash by recomputing it using the same key.
func New
func New(h func() hash.Hash, key []byte) hash.Hash
New returns a new HMAC hash using the given hash.Hash type and key.
crypto/sha256
Package sha256 implements the SHA224 and SHA256 hash algorithms as defined in FIPS 180-4.
func New
func New() hash.Hash
New returns a new hash.Hash computing the SHA256 checksum.
使用:
package mainimport ( "crypto/sha256" "fmt")func main() { h := sha256.New() h.Write([]byte("hello world\n")) fmt.Printf("%x", h.Sum(nil))}
应用
package mainimport ( "crypto/hmac" "crypto/sha256" "fmt" "io")func main() { c := getSha256Code("test@example.com") fmt.Println(c) c = getHmacCode("test@example.com") fmt.Println(c)}func getHmacCode(s string) string { h := hmac.New(sha256.New, []byte("ourkey")) io.WriteString(h, s) return fmt.Sprintf("%x", h.Sum(nil))}func getSha256Code(s string) string { h := sha256.New() h.Write([]byte(s)) return fmt.Sprintf("%x", h.Sum(nil))}
输出:
973dfe463ec85785f5f95af5ba3906eedb2d931c24e69824a89ea65dba4e813b
453159a62580804892cc90a27dfb8bfdf2309107336445dcfd0186674111ee71
- Go实战--go中使用hmac sha256(The way to go)
- Go实战--go中使用libphonenumber(The way to go)
- Go实战--go中使用cookie(The way to go)
- Go实战--go中使用rpc(The way to go)
- Go实战--go中编码转换(The way to go)
- Go实战--go中使用google/protobuf(The way to go)
- Go实战--go中使用base64加密(The way to go)
- Go实战--go中函数(function)和方法(method)的使用(The way to go)
- Go实战--go中函数递归(recursion)的使用(The way to go)
- Go实战--net/http中JSON的使用(The way to go)
- Go实战--go语言中执行shell脚本(The way to go)
- Go实战--go中一些有用的代码片段(The way to go)
- The way to go !
- Go实战--golang新手入门常见错误(The way to go)
- Go实战--golang生成uuid(The way to go)
- Go实战--go语言操作sqlite数据库(The way to go)
- [读书笔记] The.Way.To.Go
- Go实战--实现一个自己的网络请求日志httplogger(The way to go)
- 51Nod1222:最小公倍数计数 (莫比乌斯反演)
- 自学Java之Java类库(在用户界面上排列组件)(011day)
- 单目视觉ADAS的技术与体验升级之路|硬创公开课
- mysql大小写敏感
- vmware workstation 12 实现虚拟机克隆,并修改相关配置
- Go实战--go中使用hmac sha256(The way to go)
- linux vim 快捷键
- CSS设置滚动条样式
- thinkphp整合系列之微信公众号支付
- appium自动化测试环境部署_Android
- ubuntu 安装下载地址
- easyui 使用,注意事项
- Linux 结束进程脚本
- 如果看了此文你还不懂傅里叶变换,那就过来掐死我吧【完整版】