Skip to content

Latest commit

 

History

History
74 lines (66 loc) · 2.02 KB

RSA_SSA.md

File metadata and controls

74 lines (66 loc) · 2.02 KB

RSASSA-PKCS1-v1_5

W3 specification

Operations

Operation Parameters Result
generateKey RsaHashedKeyGenParams CryptoKeyPair
importKey RsaHashedImportParams CryptoKey
exportKey None JsonWebKey or BufferSource
sign None ArrayBuffer
verify None Boolean

Generate key

const keys = await crypto.subtle.generateKey(
  {
    name: "RSASSA-PKCS1-v1_5",
    hash: "SHA-256",     // SHA-1, SHA-256, SHA-384, or SHA-512
    publicExponent: new Uint8Array([1, 0, 1]), // 0x03 or 0x010001
    modulusLength: 2048, // 1024, 2048, or 4096
  },
  false,
  ["sign", "verify"],
);

Import key

const publicKey = await crypto.subtle.importKey(
  "jwk",
  {
    alg: "RS256",
    ext: true,
    key_ops: ["verify"],
    kty: "RSA",
    e: "AQAB",
    n: "vqpvdxuyZ6rKYnWTj_ZzDBFZAAAlpe5hpoiYHqa2j5kK7v8U5EaPY2bLib9m4B40j-n3FV9xUCGiplWdqMJJKT-4PjGO5E3S4N9kjFhu57noYT7z7302J0sJXeoFbXxlgE-4G55Oxlm52ID2_RJesP5nzcGTriQwoRbrJP5OEt0",
  },
  {
    name: "RSASSA-PKCS1-v1_5",
    hash: "SHA-256",
  },
  false,
  ["verify"],
);

Export key

const jwk = await crypto.subtle.exportKey(
  "jwk",
  publicKey);

Sign

const signature = await crypto.subtle.sign(
  "RSASSA-PKCS1-v1_5",
  privateKey, // RSA private key
  data,       // BufferSource
);

Verify

const ok = await crypto.subtle.verify(
  "RSASSA-PKCS1-v1_5",
  publicKey, // RSA public key
  signature, // BufferSource
  data,      // BufferSource
);