Go实战--go中使用hmac sha256(The way to go)
2017-06-05 10:48
489 查看
声明不止,继续 go go go !!!
上一篇blog介绍了base64加密,今天继续介绍一下关于加密方面的实战应用,也就是hmac sha256.
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 算法的哈希值大小为 256 位。
func New
New returns a new HMAC hash using the given hash.Hash type and key.
func New
New returns a new hash.Hash computing the SHA256 checksum.
使用:
输出:
973dfe463ec85785f5f95af5ba3906eedb2d931c24e69824a89ea65dba4e813b
453159a62580804892cc90a27dfb8bfdf2309107336445dcfd0186674111ee71
上一篇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 main import ( "crypto/sha256" "fmt" ) func main() { h := sha256.New() h.Write([]byte("hello world\n")) fmt.Printf("%x", h.Sum(nil)) }
应用
package main import ( "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中使用rpc(The way to go)
- Go实战--go中函数递归(recursion)的使用(The way to go)
- Go实战--go中使用google/protobuf(The way to go)
- Go实战--net/http中JSON的使用(The way to go)
- Go实战--go中使用libphonenumber(The way to go)
- Go实战--go中函数(function)和方法(method)的使用(The way to go)
- Go实战--go中使用base64加密(The way to go)
- Go实战--go中使用cookie(The way to go)
- Go实战--xml与json相互转换(The way to go)
- Go实战--go语言中执行shell脚本(The way to go)
- Go实战--golang新手入门常见错误(The way to go)
- Go实战--实现简单的restful api(The way to go)
- Go实战--golang生成uuid(The way to go)
- Go实战--实现一个简单聊天室chatroom(The way to go)
- Go实战--go中编码转换(The way to go)
- Go实战--压缩zip和解压缩unzip的应用(The way to go)
- Go实战--实现一个并发时钟服务器(The way to go)
- Go实战--go中一些有用的代码片段(The way to go)
- Go实战--通过net/smtp发送邮件(The way to go)
- Go实战--go语言操作sqlite数据库(The way to go)