0

Generate an RSA PSS key pair

by
Published Jan 15, 2023

Generate a license key pair using RSA PSS for creating signature, useful for generating license keys for instance

Script helper Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 6 days ago
1
//native
2

3
export async function main() {
4
  const keys = await window.crypto.subtle.generateKey(
5
    {
6
      name: "RSA-PSS",
7
      modulusLength: 1024,
8
      publicExponent: new Uint8Array([1, 0, 1]),
9
      hash: "SHA-256",
10
    },
11
    true,
12
    ["sign", "verify"],
13
  );
14
  return {
15
    private: await exportKey(keys.privateKey, true),
16
    public: await exportKey(keys.publicKey, false),
17
  };
18
}
19

20
async function exportKey(key: CryptoKey, isPrivateKey: boolean) {
21
  const exported = await window.crypto.subtle.exportKey(
22
    isPrivateKey ? "pkcs8" : "spki",
23
    key,
24
  );
25
  const exportedAsString = ab2str(exported);
26
  const exportedAsBase64 = window.btoa(exportedAsString);
27
  const pemExported = `-----BEGIN ${
28
    isPrivateKey ? "PRIVATE" : "PUBLIC"
29
  } KEY-----\n${exportedAsBase64}\n-----END PRIVATE KEY-----`;
30

31
  return pemExported;
32
}
33

34
function ab2str(buf: ArrayBuffer) {
35
  return String.fromCharCode.apply(null, new Uint8Array(buf));
36
}
37

Other submissions
  • Submitted by admin Deno
    Created 398 days ago
    1
    export async function main() {
    2
      const keys = await window.crypto.subtle.generateKey(
    3
        {
    4
          name: "RSA-PSS",
    5
          modulusLength: 1024,
    6
          publicExponent: new Uint8Array([1, 0, 1]),
    7
          hash: "SHA-256",
    8
        },
    9
        true,
    10
        ["sign", "verify"],
    11
      );
    12
      return {
    13
        private: await exportKey(keys.privateKey, true),
    14
        public: await exportKey(keys.publicKey, false),
    15
      };
    16
    }
    17
    
    
    18
    async function exportKey(key: CryptoKey, isPrivateKey: boolean) {
    19
      const exported = await window.crypto.subtle.exportKey(
    20
        isPrivateKey ? "pkcs8" : "spki",
    21
        key,
    22
      );
    23
      const exportedAsString = ab2str(exported);
    24
      const exportedAsBase64 = window.btoa(exportedAsString);
    25
      const pemExported = `-----BEGIN ${
    26
        isPrivateKey ? "PRIVATE" : "PUBLIC"
    27
      } KEY-----\n${exportedAsBase64}\n-----END PRIVATE KEY-----`;
    28
    
    
    29
      return pemExported;
    30
    }
    31
    
    
    32
    function ab2str(buf: ArrayBuffer) {
    33
      return String.fromCharCode.apply(null, new Uint8Array(buf));
    34
    }
    35