[英]How can convert this code from golang to reactjs in crypto hmac sha256 hex
Golang code is as below 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.EncodeToString(token.Sum(nil))
}
How can I convert from this to reactjs code. 我如何从此转换为reactjs代码。 I am trying like this
我正在尝试这样
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))
}
but result is not same with golang result. 但结果与golang结果不同。 What am I wrong?
我怎么了 and How will I do?
以及我该怎么办?
Go code: https://play.golang.org/p/7pXgn5GPQm 转到代码: https : //play.golang.org/p/7pXgn5GPQm
React: 阵营:
Inside a React Component, a function: 在React组件内部,一个函数:
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))
}
Inside render() 内部render()
const secret = "test";
const user = "Dennis";
const timestamp = "1";
const info = "qwerty";
this.generateClientToken(secret, user, timestamp, info);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.