golang,微信小程序获取HMAC-HS256签名

一直以为HMAC-HS256是个编码加解密的,原来还能生成签名。

微信小程序支付里的sig获取方式,需要指定HMAC-HS256签名,应该这样做


func HmacHs256(message string, secret string) string {
     
	h := hmac.New(sha256.New, []byte(secret))
	io.WriteString(h, message)
	return fmt.Sprintf("%x", h.Sum(nil))
}

使用:

func main(){
     
      // 输出1ad64e8dcb2ec1dc486b7fdf01f4a15159fc623dc3422470e51cf6870734726b
      fmt.Println(HmacHs256("appid=wx1234567&offer_id=12345678&openid=odkx20ENSNa2w5y3g_qOkOvBNM1g&pf=android&ts=1507530737&zone_id=1&org_loc=/cgi-bin/midas/getbalance&method=POST&secret=zNLgAGgqsEWJOg1nFVaO5r7fAlIQxr1u", "zNLgAGgqsEWJOg1nFVaO5r7fAlIQxr1u"))
}

与文档示例一致:
https://developers.weixin.qq.com/minigame/dev/tutorial/open-ability/midas-signature.html

你可能感兴趣的:(golang,微信小程序获取HMAC-HS256签名)