一、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>
<span id="shaKey"></span>
加密后的值为:
<span id="shaValue"></span>
</body>
<script>
var string = 'hello';
var key = '123456';
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
五、相关地址
|