Edits history of script submission #280 for ' Verify a license key (helper)'

  • deno
    import * as wmill from "https://deno.land/x/[email protected]/mod.ts";
    
    // licenseKey generated by https://hub.windmill.dev/scripts/helper/1508/generate-a-license-key-using-rsa-pss-for-a-customer-helper
    export async function main(licenseKey: string) {
      const [client, signature] = licenseKey.split('.')
      const data = new TextEncoder().encode(atob(client))
      const signatureAb = Uint8Array.from(atob(signature), c => c.charCodeAt(0))
    
      // publicKey generated by https://hub.windmill.dev/scripts/helper/1509/generate-an-rsa-pss-key-pair-helper
      let publicKey = await importPublicKey(
        (await wmill.getVariable("u/user/public_license_key"))!,
      );
      let verified = await crypto.subtle.verify(
        {
          name: "RSA-PSS",
          saltLength: 32,
        },
        publicKey,
        signatureAb,
        data
      );
    
      return verified;
    }
    
    function importPublicKey(pem: string) {
      const binaryDerString = window.atob(pem);
      const binaryDer = str2ab(binaryDerString);
    
      return window.crypto.subtle.importKey(
        "spki",
        binaryDer,
        {
          name: "RSA-PSS",
          hash: "SHA-256",
        },
        true,
        ["verify"],
      );
    }
    
    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 admin 1255 days ago