deno.com

RSASSA-PKCS1-v1_5 签名和验证

在 Github 上编辑

此示例演示了如何使用 Deno 内置的 SubtleCrypto API 进行 RSA 签名和验证。

使用 TextEncoder 将文本转换为 Uint8Array (签名必需)
const data = new TextEncoder().encode("Hello, Deno 2.0!");

const { publicKey, privateKey } = await crypto.subtle.generateKey(
  {
    name: "RSASSA-PKCS1-v1_5",
    modulusLength: 2048, // 2048-bit key for strong security
    publicExponent: new Uint8Array([1, 0, 1]), // Public exponent: 65537
    hash: { name: "SHA-256" },
  },
  true,
  ["verify", "sign"],
);
使用私钥对数据进行签名
const signature = await crypto.subtle.sign(
  { name: "RSASSA-PKCS1-v1_5" },
  privateKey,
  data,
);
将签名记录为字节数组
console.log("Signature:", new Uint8Array(signature));
使用公钥验证签名
const verification = await crypto.subtle.verify(
  { name: "RSASSA-PKCS1-v1_5" },
  publicKey,
  signature,
  data,
);

console.log("Verification:", verification);

使用 Deno CLI 在本地运行此示例

deno run https://docs.deno.org.cn/examples/scripts/rsa_signature.ts

您找到所需的内容了吗?

隐私政策