使用来自 Firefox AddOn 的 Web Crypto API

Using the Web Crypto API from Firefox AddOn

我不知道如何从 AddOn 脚本本身(无文档)访问 Web Crypto API。

尝试从内容脚本执行此操作会导致访问权限错误 then:

JavaScript error:
  resource://gre/modules/commonjs/toolkit/loader.js ->
  resource://gre/modules/commonjs/sdk/loader/sandbox.js -> 
  resource://..../data/content-script.js,
  line 36: Error: Permission denied to access property "then"

相关代码为:

  var password = new TextEncoder("utf-8").encode('password');
  var salt = new TextEncoder("utf-8").encode('salt');
  var iterations = 1;
  var outputLen = 20;
  return window.crypto.subtle.importKey(
    "raw", password, {"name": "PBKDF2"}, false, ["deriveKey"]
  ).then(function(baseKey) {
    return window.crypto.subtle.deriveKey(
      { "name": "PBKDF2",
        "salt": salt,
        "iterations": iterations,
        "hash": "SHA-1",
      },
      baseKey,                              // input key
      {"name": "AES-CBC", "length": 32*8},  // output key use and size
      true,                                 // output key is extractable
      ["encrypt","decrypt"]);               // output key capabilities
  }).then(function(aesKey) {
    return window.crypto.subtle.exportKey("raw", aesKey);
  }).then(function(keyBytes) {
    var keyArray = Array.slice(new Uint8Array(keyBytes), 0, outputLen);
    keyArray.toHex = toHex;
    keyArray.toPassword = toPassword;
    return keyArray;
  });

是否有任何 require Web Crypto API 到附加组件中,或者我可以做些什么来完成这项工作?由于运行时问题,我真的不想使用 JS 加密库(PBKDF2 运行速度快 → 选择更多迭代 → 更安全)。

Components.utils.importGlobalProperties(['crypto']);
var blah = crypto.subtle.deriveKey({....});

取自此处:https://developer.mozilla.org/en-US/docs/Components.utils.importGlobalProperties