Edits history of script submission #22420 for ' Generate an RSA PSS key pair (helper)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export async function main() {
      const keys = await window.crypto.subtle.generateKey(
        {
          name: "RSA-PSS",
          modulusLength: 1024,
          publicExponent: new Uint8Array([1, 0, 1]),
          hash: "SHA-256",
        },
        true,
        ["sign", "verify"],
      );
      return {
        private: await exportKey(keys.privateKey, true),
        public: await exportKey(keys.publicKey, false),
      };
    }
    
    async function exportKey(key: CryptoKey, isPrivateKey: boolean) {
      const exported = await window.crypto.subtle.exportKey(
        isPrivateKey ? "pkcs8" : "spki",
        key,
      );
      const exportedAsString = ab2str(exported);
      const exportedAsBase64 = window.btoa(exportedAsString);
      const pemExported = `-----BEGIN ${
        isPrivateKey ? "PRIVATE" : "PUBLIC"
      } KEY-----\n${exportedAsBase64}\n-----END PRIVATE KEY-----`;
    
      return pemExported;
    }
    
    function ab2str(buf: ArrayBuffer) {
      return String.fromCharCode.apply(null, new Uint8Array(buf));
    }
    

    Submitted by hugo989 6 days ago