Edits history of script submission #22419 for ' Generate a license key using RSA PSS for a customer (helper)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    import * as wmill from "windmill-client@1";
    
    export async function main(client: string) {
      const encoded = new TextEncoder().encode(client);
    
      // key generated by https://hub.windmill.dev/scripts/helper/1509/generate-an-rsa-pss-key-pair-helper
      let privateKey = await importPrivateKey(
        (await wmill.getVariable("u/user/your_private_key"))!,
      );
      let signature = await crypto.subtle.sign(
        {
          name: "RSA-PSS",
          saltLength: 32,
        },
        privateKey,
        encoded,
      );
    
      // Combine the encoded data and signature to create a license key
      const licenseKey = `${btoa(client)}.${abToBase64(signature)}`;
    
      return licenseKey;
    }
    
    function abToBase64(arrayBuffer: ArrayBuffer) {
      return btoa(String.fromCharCode.apply(null, new Uint8Array(arrayBuffer)));
    }
    
    function importPrivateKey(pem: string) {
      const binaryDerString = atob(pem);
      const binaryDer = str2ab(binaryDerString);
    
      return crypto.subtle.importKey(
        "pkcs8",
        binaryDer,
        {
          name: "RSA-PSS",
          hash: "SHA-256",
        },
        true,
        ["sign"],
      );
    }
    
    function str2ab(str: string) {
      const buf = new ArrayBuffer(str.length);
      const bufView = new Uint8Array(buf);
      for (let i = 0, strLen = str.length; i < strLen; i++) {
        bufView[i] = str.charCodeAt(i);
      }
      return buf;
    }
    

    Submitted by hugo989 6 days ago