简体   繁体   English

如何在浏览器中复制Node的Crypto.createHmac('sha256',buffer)?

[英]How to replicate Node's Crypto.createHmac( 'sha256', buffer) in the browser?

How to get to "feature parity" between Node's Crypto.createHmac( 'sha256', buffer) and CryptoJS.HmacSHA256(..., secret) ? 如何获得Node的Crypto.createHmac( 'sha256', buffer)CryptoJS.HmacSHA256(..., secret)之间的“功能奇偶校验”?

I have a 3rd party code that does what you can see here as the method node1 . 我有一个第三方代码,该代码执行的方法为node1 I would need to achieve the same result in the browser. 我需要在浏览器中实现相同的结果。 Seemingly, the difference is in the that the secret is base64 decoded on the node side. 看来,区别在于secret是在节点侧进行base64解码的。 But I still can't get the same output. 但是我仍然无法获得相同的输出。

const CryptoJS = require('crypto-js')
const Crypto = require('crypto')

const message = "Message"
const secret = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="

function node1() {
  return Crypto.createHmac("sha256", Buffer.from(secret, 'base64'))
      .update(message, "utf8")
      .digest("base64");
}

function node2() {
  return Crypto.createHmac("sha256", Buffer.from(secret, 'base64').toString('base64'))
      .update(message, "utf8")
      .digest("base64");
}

function browser() {
  const crypted = CryptoJS.HmacSHA256(message, secret)
  return CryptoJS.enc.Base64.stringify(crypted)
}

console.log('node1', node1())
console.log('node2', node2())
console.log('browser-like', browser())

// node1 agitai8frSJpJuXwd4HMJC/t2tluUJPMZy8CeYsEHTE=
// node2 fxJQFWs5W3A4otaAlnlV0kh4yfQPb4Y1ChSVZsUAAXA=
// browser-like fxJQFWs5W3A4otaAlnlV0kh4yfQPb4Y1ChSVZsUAAXA=

So, I can reproduce a naive browser-like behaviour in node. 因此,我可以在节点中重现幼稚的类似浏览器的行为。 This gave me the idea to use atob in the browser, to reproduce node's behaviour. 这使我atob在浏览器中使用atob来重现节点行为的想法。 The following sign method is my best guess on the browser side. 以下sign方法是我在浏览器端的最佳猜测。

function sign(message) {
  const crypted = CryptoJS.HmacSHA256(message, atob(secret));
  return CryptoJS.enc.Base64.stringify(crypted)
}

function signNotDecoded(message) {
  const crypted = CryptoJS.HmacSHA256(message, secret);
  return CryptoJS.enc.Base64.stringify(crypted)
}

console.log('browser', sign('Message'))
console.log('browser-like', signNotDecoded('Message'))

// browser dnVm5jBgIBNV6pFd4J9BJTjx3BFsm7K32SCcEQX7RHA= 
// browser-like fxJQFWs5W3A4otaAlnlV0kh4yfQPb4Y1ChSVZsUAAXA=

So, running signDecoded() in the browser, and running browser() in node gives the same output. 因此,在浏览器中运行signDecoded()和在node中运行browser()会得到相同的输出。 Running both node2() and browser() in node again provide the same output, but still sign() differs from node1() . 再次在node中运行node2()browser()都提供相同的输出,但是sign()node1()仍然不同。

Based on the above, I'm quite sure that the problem is with my usage of atob, but what do I miss there? 基于上述情况,我很确定问题出在我使用atob的问题上,但是我想念的是什么?

Change 更改

atob(secret)

To

CryptoJS.enc.Base64.parse(secret)

Because if you pass a raw string as key to the function it will be re-parsed as UTF-8. 因为如果您将原始字符串作为函数的键传递,它将被重新解析为UTF-8。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 需要与 crypto.createHmac 方法的这个特定实现等效的浏览器 - Need browser equivalent of this specific implementation of crypto.createHmac method HOTP 问题:Node.js crypto.createHmac 仅在计数为 0 时有效 - HOTP issue: Node.js crypto.createHmac works only when count is 0 将 JS crypto.createHmac 翻译成 Xojo Crypto.HMAC - Translating JS crypto.createHmac to Xojo Crypto.HMAC 如何在 Java 中复制 javascript sha256 哈希并获得相同的十六进制输出? - How can I replicate a javascript sha256 hash in Java and get the same Hex output? 如何将此代码从golang转换为crypto hmac sha256 hex中的reactjs - How can convert this code from golang to reactjs in crypto hmac sha256 hex typescript crypto-js 如何使用 sha256 算法和密钥散列数据 - typescript crypto-js how to hash data using sha256 algorithm and key 使用angular 4获得TypeError的woocommerce集成:crypto.createHmac不是函数 - Integration of woocommerce using angular 4 getting TypeError: crypto.createHmac is not a function 将哈希HMAC SHA256的端口JS加密代码移植到PHP - Port JS crypto code for hash HMAC SHA256 to PHP Svelte Cognito RollupJs 错误:node_modules/@aws-crypto/sha256-js/build/index.js 未导出“Sha256” - Svelte Cognito RollupJs error: 'Sha256' is not exported by node_modules/@aws-crypto/sha256-js/build/index.js 如何在 Javascript 上正确计算 sha256? - How calculate correctly sha256 on Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM