一、go代码

package main

import (
	"crypto/hmac"
	"crypto/sha256"
	"encoding/hex"
	"fmt"
)

func main() {
	s := []byte("hello")
	key := []byte("123456")
	m := hmac.New(sha256.New, key )
	m.Write( s )
	signature := hex.EncodeToString(m.Sum(nil))
	fmt.Print(signature + "\n\r")
}

二、PHP代码

$string = "hello";
$key = "123456";
$signature = hash_hmac('sha256', $string, $key);

三、Js代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试</title>
</head>
<script src="https://cdn.bootcss.com/crypto-js/3.1.9-1/crypto-js.min.js"></script>
<body>
    <span id="shaString"></span>&nbsp;
    <span id="shaKey"></span>&nbsp;
    加密后的值为:&nbsp;&nbsp;
    <span id="shaValue"></span>&nbsp;
</body>
<script>
    //定义字符串、秘钥
    var string = 'hello';
    var key = '123456';

    //调方法加密,并转为string类型
    var hash = CryptoJS.HmacSHA256(string, key);
    var result = hash.toString();

    //给标签赋值
    document.getElementById('shaString').innerText = string;
    document.getElementById('shaKey').innerText = key;
    document.getElementById('shaValue').innerText = result;
</script>
</html>

四、结果

  • 这三种方式得出的结果都是:ac28d602c767424d0c809edebf73828bed5ce99ce1556f4df8e223faeec60edd

五、相关地址