230-go语言中使用HMAC

时间:2019-01-17
本文章向大家介绍230-go语言中使用HMAC,主要包括230-go语言中使用HMAC使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。






go语言中使用HMAC



我们先说一下
要使用的两个函数

第一个
hmac.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.
// Note that unlike other hash implementations in the standard library,
// the returned Hash does not implement encoding.BinaryMarshaler
// or encoding.BinaryUnmarshaler.
func New(h func() hash.Hash, key []byte) hash.Hash {
	hm := new(hmac)
	hm.outer = h()
	hm.inner = h()
	hm.size = hm.inner.Size()
	hm.blocksize = hm.inner.BlockSize()
	hm.ipad = make([]byte, hm.blocksize)
	hm.opad = make([]byte, hm.blocksize)
	if len(key) > hm.blocksize {
		// If key is too big, hash it.
		hm.outer.Write(key)
		key = hm.outer.Sum(nil)
	}
	copy(hm.ipad, key)
	copy(hm.opad, key)
	for i := range hm.ipad {
		hm.ipad[i] ^= 0x36
	}
	for i := range hm.opad {
		hm.opad[i] ^= 0x5c
	}
	hm.inner.Write(hm.ipad)
	return hm
}

翻译一下
通过传入的hash和key,返回一个HMAC的hash接口,
注意:这个返回的hash接口和标准库的不一样
这个hash接口不实现
encoding.BinaryMarshaler 和 encoding.BinaryUnmarshaler

然后我们看下参数
参数1: 创建⼀个新的使⽤哈希校验算法的hash.Hash接⼝, 如:
md5.New()
sha1.New()
sha256.New()
参数2: 使⽤的秘钥
返回值: 通过该哈希接⼝添加数据和计算消息认证码
添加数据: Write(p []byte) (n int, err error)
计算结果: Sum(b []byte) []byte






第二个
hmac.Equal方法
func Equal(mac1, mac2 []byte) bool

我们看下源码

func Equal(mac1, mac2 []byte) bool {
	// We don't have to be constant time if the lengths of the MACs are
	// different as that suggests that a completely different hash function
	// was used.
	return subtle.ConstantTimeCompare(mac1, mac2) == 1
}

这个方法很简单
就是比较一下两个mac值





现在我们来点代码

func main() {

	text := "你好 世界 hello world"
	key := "abcabc123123"

	HMAC := GenerateHMAC(text, key)

	fmt.Println(HMAC)
}

func GenerateHMAC(text string, key string) string {

	textBytes := []byte(text)
	keyBytes := []byte(key)

	hash := hmac.New(sha256.New, keyBytes)

	hash.Write(textBytes)

	result := hash.Sum(nil)

	return string(result)

}




很简单吧





然后我们验证一下生成的HMAC


func main() {

	text := "你好 世界 hello world"
	key := "abcabc123123"

	HMAC := GenerateHMAC(text, key)
	fmt.Println(HMAC)

	verifyHMAC := VerifyHMAC(HMAC, text, key)
	fmt.Println(verifyHMAC)
}

func VerifyHMAC(HMAC string, text string, key string) bool {

	HMACBytes := []byte(HMAC)

	nowHMAC := GenerateHMAC(text, key)
	nowHMACBytes := []byte(nowHMAC)

	return hmac.Equal(HMACBytes, nowHMACBytes)
}

func GenerateHMAC(text string, key string) string {

	textBytes := []byte(text)
	keyBytes := []byte(key)

	hash := hmac.New(sha256.New, keyBytes)

	hash.Write(textBytes)

	result := hash.Sum(nil)

	return string(result)
}