gpt4 book ai didi

javascript - 如何将此代码从 golang 转换为 crypto hmac sha256 hex 中的 reactjs

转载 作者:数据小太阳 更新时间:2023-10-29 03:26:05 46 4
gpt4 key购买 nike

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))
}

我如何将其转换为 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))
}

但结果与 golang 结果不同。我错了什么?我该怎么做?

最佳答案

代码:https://play.golang.org/p/7pXgn5GPQm

react :

  • 使用的包:“crypto-js”:“^3.1.9-1”
  • React v15.4.2

在 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))
}

内部渲染()

const secret = "test";
const user = "Dennis";
const timestamp = "1";
const info = "qwerty";
this.generateClientToken(secret, user, timestamp, info);

关于javascript - 如何将此代码从 golang 转换为 crypto hmac sha256 hex 中的 reactjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41521245/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com