deno.com

RSASSA-PKCS1-v1_5 Signature and Verification

在 Github 上编辑

This example demonstrates RSA signature and verification using Deno's built-in SubtleCrypto API.

Convert the text to a Uint8Array using TextEncoder (required for signing)
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"],
);
Sign the data using the private key
const signature = await crypto.subtle.sign(
  { name: "RSASSA-PKCS1-v1_5" },
  privateKey,
  data,
);
Log the signature as a byte array
console.log("Signature:", new Uint8Array(signature));
Verify the signature using the public key
const verification = await crypto.subtle.verify(
  { name: "RSASSA-PKCS1-v1_5" },
  publicKey,
  signature,
  data,
);

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

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

deno run https://docs.denohub.com/examples/scripts/rsa_signature.ts

你找到需要的内容了吗?

隐私政策