Javascript 如何将此代码从golang转换为crypto hmac sha256 hex中的reactjs
Golang代码如下所示Javascript 如何将此代码从golang转换为crypto hmac sha256 hex中的reactjs,javascript,go,cryptojs,Javascript,Go,Cryptojs,Golang代码如下所示 func GenerateClientToken(secret, user, timestamp, info string) string { token := hmac.New(sha256.New, []byte(secret)) token.Write([]byte(user)) token.Write([]byte(timestamp)) token.Write([]byte(info)) return hex.Encod
func GenerateClientToken(secret, user, timestamp, info string) string {
token := hmac.New(sha256.New, []byte(secret))
token.Write([]byte(user))
token.Write([]byte(timestamp))
token.Write([]byte(info))
return hex.EncodeToString(token.Sum(nil))
}
如何将此代码转换为reactjs代码。
我正在这样努力
import CryptoJS from 'crypto-js'
generateClientToken(secret, user, timestamp, info) {
var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, secret);
hmac.update(user);
hmac.update(timestamp);
hmac.update(info);
var hash = hmac.finalize();
console.log("hmac: ", hash.toString(CryptoJS.enc.Base64))
console.log("hmac: ", hash.toString(CryptoJS.enc.Hex))
}
但结果与戈朗结果不一致。我怎么了?我该怎么办?开始代码:
反应:
- 使用的包:“crypto js”:“^3.1.9-1”
- 反应v15.4.2
generateClientToken(secret, user, timestamp, info) {
let hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, secret);
hmac.update(user);
hmac.update(timestamp);
hmac.update(info);
let hash = hmac.finalize();
console.log("hmac: ", hash.toString(CryptoJS.enc.Hex))
}
内部渲染()
const secret = "test";
const user = "Dennis";
const timestamp = "1";
const info = "qwerty";
this.generateClientToken(secret, user, timestamp, info);