Javascript 将crypto hmac转换为crypto js hmac字符串

Javascript 将crypto hmac转换为crypto js hmac字符串,javascript,node.js,encryption,hmac,postman-pre-request-script,Javascript,Node.js,Encryption,Hmac,Postman Pre Request Script,我正在尝试转换一个秘密的hmac字符串,以便在postman中测试我的api。邮递员预装了cryptojs。这是我在测试服务器上使用加密的过程: const crypto = require('crypto'); const generateHmac = (privateKey, ts) => { const hmac = crypto.createHmac('sha256', privateKey); hmac.update(ts); const signatur

我正在尝试转换一个秘密的hmac字符串,以便在postman中测试我的api。邮递员预装了cryptojs。这是我在测试服务器上使用加密的过程:

const crypto = require('crypto');
const generateHmac = (privateKey, ts) => {
    const hmac = crypto.createHmac('sha256', privateKey);
    hmac.update(ts);
    const signature = hmac.digest('hex');
    return signature;
}
这与邮递员中使用cryptojs生成的字符串不匹配:

const createHmacString = (privateKey, ts) => {
    const hmac = CryptoJS.HmacSHA256(ts, privateKey).toString(CryptoJS.enc.Hex)
    return hmac;
}

不知道我做错了什么。提前谢谢

好的,终于明白了-crypto js不提供实际字节,因此编码一切都是必要的:

const createHmacString = (privateKey, ts) => {
    const key = CryptoJS.enc.Utf8.parse(privateKey)
    const timestamp = CryptoJS.enc.Utf8.parse(ts)
    const hmac = CryptoJS.enc.Hex.stringify(CryptoJS.HmacSHA256(timestamp, key))

    //  const hmac = CryptoJS.HmacSHA256(ts, privateKey).toString(CryptoJS.enc.Hex)
    return hmac;
}

let ts = new Date().getTime();
const signature = createHmacString("your-private-key", ts);