0
Generate a license key using RSA PSS for a customer
One script reply has been approved by the moderators Verified
Created by admin 474 days ago Viewed 5549 times
0
Submitted by admin Deno
Verified 474 days ago
1
import * as wmill from "https://deno.land/x/windmill@v1.85.0/mod.ts";
2

3
export async function main(client: string) {
4
  const encoded = new TextEncoder().encode(client);
5

6
  // key generated by https://hub.windmill.dev/scripts/helper/1509/generate-an-rsa-pss-key-pair-helper
7
  let privateKey = await importPrivateKey(
8
    (await wmill.getVariable("u/user/your_private_key"))!,
9
  );
10
  let signature = await crypto.subtle.sign(
11
    {
12
      name: "RSA-PSS",
13
      saltLength: 32,
14
    },
15
    privateKey,
16
    encoded,
17
  );
18

19
  // Combine the encoded data and signature to create a license key
20
  const licenseKey = `${btoa(client)}.${abToBase64(signature)}`;
21

22
  return licenseKey;
23
}
24

25
function abToBase64(arrayBuffer: ArrayBuffer) {
26
  return btoa(String.fromCharCode.apply(null, new Uint8Array(arrayBuffer)));
27
}
28

29
function importPrivateKey(pem: string) {
30
  const binaryDerString = window.atob(pem);
31
  const binaryDer = str2ab(binaryDerString);
32

33
  return window.crypto.subtle.importKey(
34
    "pkcs8",
35
    binaryDer,
36
    {
37
      name: "RSA-PSS",
38
      hash: "SHA-256",
39
    },
40
    true,
41
    ["sign"],
42
  );
43
}
44

45
function str2ab(str: string) {
46
  const buf = new ArrayBuffer(str.length);
47
  const bufView = new Uint8Array(buf);
48
  for (let i = 0, strLen = str.length; i < strLen; i++) {
49
    bufView[i] = str.charCodeAt(i);
50
  }
51
  return buf;
52
}
53