类型错误 Web 加密导入密钥算法参数



我正在实现mdn上显示的pbkdf2示例。我的测试代码是

let enc = new TextEncoder();
let password = enc.encode("password");
window.crypto.subtle.importKey(
                "raw",
                password,
                {"name": "PBKDF2"},
                false,
                ["deriveBits", "deriveKey"]
                )

这会导致以下错误(打字稿 3.3.4000(:

credential.service.ts:6:3 - error TS2345: Argument of type '{ "name": string; }' is not assignable to parameter of type 'string | AesKeyAlgorithm | EcKeyImportParams | HmacImportParams | RsaHashedImportParams | DhImportKeyParams'.
  Type '{ "name": string; }' is missing the following properties from type 'DhImportKeyParams': generator, prime
{"name": "PBKDF2"},

代码在 chrome 中工作正常。

以下(丑陋的(解决方法可以解决问题:

window.crypto.subtle.importKey(
                "raw",
                password,
                {
                    "name": "PBKDF2",
                    // the next two lines are just to trick typescript
                    "generator": new Uint8Array(12),
                    "prime": new Uint8Array(12)
                },
                false,
                ["deriveBits", "deriveKey"]
                )

它很丑陋,因为它欺骗打字稿认为PBKDF2对象与它本身不同。

我认为这个例子在这一行是错误的。当您查看上面的Parameters section / algorithm时,您将看到:

对于 PBKDF2:

传递字符串 PBKDF2。

因此,与其{name: 'PBKDF2'}只使用'PBKDF2' .

MDN: SubtleCrypto.importKey((
https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey#Parameters

相关内容

  • 没有找到相关文章

最新更新