crypto.subtle.importKey 抛出未指定的错误

问题描述 投票:0回答:1

我正在尝试使用 WEB Crypto API 签署文本。我依赖第 3 方从智能卡中提取 certificate,该智能卡返回它的 Base64 字符串表示形式。据我了解,首先我需要将其转换为 cryptoKey 对象,以将其传递给 sign 方法,这就是错误发生的地方。 Here is how it looks in the console

这就是我正在做的:

  const res = await fetch("https://localhost:53952/getsigner",{
    method: "POST",
    headers: {
      'Content-type': 'application/json',
    },
    body: "{}"
  })

const body = await res.json();
const signCertString = body.chain[0];

const binaryCert = Uint8Array.from(atob(signCertString), c => c.charCodeAt(0));
const certBuffer = binaryCert.buffer;

crypto.subtle.importKey(
  "pkcs8",
  certBuffer,
  {
    name: "RSASSA-PKCS1-v1_5",
    hash: { name: "SHA-256" },
  },
  true,
  ["sign"]
)
  .then((cryptoKey) => {
    console.log("CryptoKey object created:", cryptoKey);
  })
  .catch((error) => {
    console.error("Error creating CryptoKey object:", error);
  });
javascript certificate digital-signature smartcard webcrypto-api
1个回答
0
投票

Uint8Array.from
仅接受 1 个参数,一个数组。

Uint8Array.from(atob(signCertString), c => c.charCodeAt(0))

你可能想做这样的事情:

Uint8Array.from([...atob(signCertString)].map(c => c.charCodeAt(0)))
© www.soinside.com 2019 - 2024. All rights reserved.