简体   繁体   English

PHP的hash_hmac()是否有任何等效的JavaScript function?

[英]Is there any equivalent JavaScript function for PHP`s hash_hmac()?

We are developing an online course website.我们正在开发一个在线课程网站。
Courses have audio and text (no video).课程有音频和文字(没有视频)。
Audio files are stored on Amazon S3 and delivered via AWS CloudFront.音频文件存储在 Amazon S3 上并通过 AWS CloudFront 交付。

Every time a user wants to play a course audio file,每次用户想要播放课程音频文件时,
website (server side) sends a request to CloudFront to get the audio file.网站(服务器端)向 CloudFront 发送请求以获取音频文件。
Each request also includes a signature and a random number.每个请求还包括一个签名和一个随机数。

We use PHP hash_hmac to generate the signature as follows:我们使用 PHP hash_hmac 生成签名如下:

$signature = hash_hmac('sha256', $random_number, $secret, true);

Both sides (website and CloudFront function) have the secret key hard-coded.双方(网站和 CloudFront 功能)都有硬编码的密钥。

Once CloudFront receives the request, CloudFront 收到请求后,
a "CloudFront Function" (new AWS feature) is validating the token. “CloudFront 功能”(新的 AWS 功能)正在验证令牌。
CloudFront Function can only use JavaScript (not NodeJS nor Python). CloudFront Function 只能使用 JavaScript(不是 NodeJS 也不是 Python)。
https://aws.amazon.com/blogs/aws/introducing-cloudfront-functions-run-your-code-at-the-edge-with-low-latency-at-any-scale/ https://aws.amazon.com/blogs/aws/introducing-cloudfront-functions-run-your-code-at-the-edge-with-low-latency-at-any-scale/

We want CloudFront function to simply run hash_hmac() using JavaScript我们希望 CloudFront function 使用 JavaScript 简单地运行 hash_hmac()
and then compare the signatures but we could not find an equivalent JS function.然后比较签名,但我们找不到等效的 JS function。

We checked extensively on StackOverflow but could not find an equivalent JS function.我们在 StackOverflow 上进行了广泛检查,但找不到等效的 JS function。
Our findings:我们的发现:
Google`s Crypto-JS library is deprecated. Google 的 Crypto-JS 库已弃用。
NodeJS Crypto module cannot be used since we use plain JavaScript.不能使用 NodeJS 加密模块,因为我们使用普通的 JavaScript。

Can someone PLEASE share a link or a code snippet on how to run hash_hmac on JavaScript?有人可以分享有关如何在 JavaScript 上运行 hash_hmac 的链接或代码片段吗?

Yes there is a native API for that:是的,有一个本机API

 (async function () { const someData = 'Very genuine data'; const key = await window.crypto.subtle.generateKey({ name: "HMAC", hash: { name: "SHA-256" } }, true, ["sign", "verify"]); let enc = new TextEncoder().encode(someData); const signature = await crypto.subtle.sign('HMAC', key, enc); console.log(signature); // ArrayBuffer(32) const valid = await crypto.subtle.verify('HMAC', key, signature, enc); console.log(valid); })();

So one way is to use JS crypto.subtle.sign as @Guerric P mentioned above.所以一种方法是使用 JS crypto.subtle.sign 作为上面提到的@Guerric P。

It turns out there is another way to achieve it.事实证明还有另一种方法可以实现它。
After reading the complete CloudFront developer guide,阅读完整的 CloudFront 开发人员指南后,
I found the following:我发现了以下内容:

NodeJS has a great module called Crypto. NodeJS 有一个很棒的模块,叫做 Crypto。

In general, we cannot add a NodeJS module to JavaScript code.一般来说,我们不能在 JavaScript 代码中添加 NodeJS 模块。

However, CloudFront Function (who use plain JS only) supports also NodeJS Crypto module:但是,CloudFront Function(仅使用纯 JS)也支持 NodeJS Crypto 模块:
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-javascript-runtime-features.html#writing-functions-javascript-features-builtin-modules-crypto https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-javascript-runtime-features.html#writing-functions-javascript-features-builtin-modules-crypto

The cryptographic module (crypto) provides standard hashing and hash-based message authentication code (HMAC) helpers.加密模块 (crypto) 提供标准散列和基于散列的消息身份验证代码 (HMAC) 帮助程序。 You can load the module using require('crypto').您可以使用 require('crypto') 加载模块。 The module provides the following methods that behave exactly as their Node.js counterparts.该模块提供了以下与 Node.js 对应的方法完全相同的方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM