0

Generate a license key using RSA PSS for a customer

by
Published Jan 15, 2023

Require key to be generated by https://hub.windmill.dev/scripts/helper/1509/generate-an-rsa-pss-key-pair-helper

Script helper Verified

The script

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

3
import * as wmill from "windmill-client@1";
4

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

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

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

24
  return licenseKey;
25
}
26

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

31
function importPrivateKey(pem: string) {
32
  const binaryDerString = atob(pem);
33
  const binaryDer = str2ab(binaryDerString);
34

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

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

Other submissions
  • Submitted by admin Deno
    Created 398 days ago
    1
    import * as wmill from "https://deno.land/x/[email protected]/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