[英]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 可以由大多数加密库确定,例如CryptoJS或WebCrypto 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.