简体   繁体   English

需要与 crypto.createHmac 方法的这个特定实现等效的浏览器

[英]Need browser equivalent of this specific implementation of crypto.createHmac method

So, I have this piece of code that's written in Node.js所以,我有这段用 Node.js 编写的代码

crypto.createHmac('sha256', secret).update(orderedParams).digest('hex')

I wish to bring this piece of code in the browser but that doesn't work since the 'crypto' library is not supported on the browser.我希望将这段代码带到浏览器中,但这不起作用,因为浏览器不支持“加密”库。 Can somebody just help me re-create the same method in the browser?有人可以帮我在浏览器中重新创建相同的方法吗?

You can try to use crypto-browserify .您可以尝试使用crypto-browserify

It's a reimplementation of crypto , made it so that it can run on the Browser.这是对crypto的重新实现,使其可以在浏览器上运行。

An HMAC can be determined by most crypto libraries, eg CryptoJS or WebCrypto API . HMAC 可以由大多数加密库确定,例如CryptoJSWebCrypto API

The following example uses CryptoJS:以下示例使用 CryptoJS:

 var secret = 'my secret'; var orderedParams = 'the ordered params'; // Short var hmac3 = CryptoJS.HmacSHA256(orderedParams, secret).toString(); console.log(hmac3.replace(/(.{48})/g,'$1\n')); // Progressive var hmac2 = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, secret).update(orderedParams).finalize().toString(); console.log(hmac2.replace(/(.{48})/g,'$1\n'));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>

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

相关问题 如何在浏览器中复制Node的Crypto.createHmac(&#39;sha256&#39;,buffer)? - How to replicate Node's Crypto.createHmac( 'sha256', buffer) in the browser? 将 JS crypto.createHmac 翻译成 Xojo Crypto.HMAC - Translating JS crypto.createHmac to Xojo Crypto.HMAC 使用angular 4获得TypeError的woocommerce集成:crypto.createHmac不是函数 - Integration of woocommerce using angular 4 getting TypeError: crypto.createHmac is not a function 为什么我的crypto.createHmac()为相同的输入生成不同的HMAC? - Why does my crypto.createHmac() generate a different HMAC for the same input? 为什么我的crypto.createHmac()为相同的输入生成不同的HMAC? - Why does my crypto.createHmac() generate a different HMAC for the same input? HOTP 问题:Node.js crypto.createHmac 仅在计数为 0 时有效 - HOTP issue: Node.js crypto.createHmac works only when count is 0 为什么我的crypto.sublte.digest实现会产生相等长度字符串的等效十六进制哈希值? - Why does my implementation of crypto.sublte.digest result in equivalent hex hashes for equal length strings? 如何将这种签名方法从crypto(node)转换为crypto-js(browser)? - How to convert this signature method from crypto (node) to crypto-js (browser)? IE9和Firefox的Crypto.getRandomValues实现 - Crypto.getRandomValues Implementation for IE9 and Firefox 相当于 node.js 中的 window.crypto() - Equivalent of window.crypto() in node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM