如何创建 hmac sha512 哈希?

How to create hmac sha512 hash?

我正在开发一个插件,必须创建一个 hmac sha512 哈希,但 jpm 说找不到加密模块 (https://nodejs.org/api/crypto.html#crypto_crypto_createhmac_algorithm_key) - 我不能在 firefox 插件中使用标准节点模块发展?
抱歉,我对此很陌生。
如果没有,是否有另一种创建哈希的方法?

can't I use standard node modules in firefox addon development?

你的意思是 core modules 不,你不能。

你需要look for a browser-compatible crypto module. Here is one

只能使用npm-hoisted third-party个SDK模块,目前此类模块不多,例如menuitem.

如果你想创建散列,那么你可以使用 crypto.js library.But 你不能直接在 index.js.For 中使用它,你必须创建 pageworker 并在任何时候进行消息传递创建哈希。

要在 index.js 中创建 pageworker,您的代码将如下所示:

var HashWorker = require('sdk/page-worker').Page({
   contentURL: "./hash.html",
   contentScriptFile :   ["./crypto.js","./myfile.js"]
});

在 myfile.js 中,您将使用 crypto.js 函数创建 hash.Note 所有文件 hash.html、crypto.js 和 myfile.js 必须是在你的插件的数据目录中。

hash.html 看起来像这样:

<html>
<head>
    <title></title>
</head>
<body>

</body>
</html>

完成所有这些设置后,您可以通过消息传递从 index.js 到 myfile.js 进行通信,反之亦然。

要创建某些东西的散列,您可以将消息发送到 myfile.js,看起来像这样: index.js

//send message to myfile.js to create hash of some value
HashWorker.port.emit('createHash',{data : "whose hash to create"});

//message received from myfile.js containing hash of specified value
HashWorker.port.on('hash_result',function(message){
  console.log(message.hash);
});

在 myfile.js 中,消息传递看起来像这样: myfile.js

//message received from index.js to create hash of specified value
self.port.on('createHash',function(message){
  var value = message.data
  var hash = ...//cryptojs function to create hash of above value.

  //send hash created to index.js
  self.port.emit('hash_result',{hash : hash});
});

您可以使用 Mozilla 自己的 Components

const { Cu } = require("chrome");
Cu.importGlobalProperties(["crypto"]);

导入 crypto object into the addon. Since you just asked for that, you might be aware of how to use it to create a hash。该示例说要像

一样使用它
function sha256(str) {
  // We transform the string into an arraybuffer.
  var buffer = new TextEncoder("utf-8").encode(str);
  return crypto.subtle.digest("SHA-256", buffer).then(function (hash) {
    // whatever you want to do with hash;
  });
}