summaryrefslogtreecommitdiff
path: root/src/util/generator.ts
blob: 596110d66555f961200eabf31a20041ce38ebc9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import crypto from "crypto";
import { customAlphabet } from "nanoid";

// This alphabet (used to generate all event, group, etc. IDs) is missing '-'
// because ActivityPub doesn't like it in IDs
const nanoid = customAlphabet(
    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_",
    21,
);

const generateAlphanumericString = (length: number) => {
    return Array(length)
        .fill(0)
        .map((x) => Math.random().toString(36).charAt(2))
        .join("");
};

export const generateEventID = () => nanoid();

export const generateEditToken = () => generateAlphanumericString(32);

export const generateRSAKeypair = () => {
    return crypto.generateKeyPairSync("rsa", {
        modulusLength: 4096,
        publicKeyEncoding: {
            type: "spki",
            format: "pem",
        },
        privateKeyEncoding: {
            type: "pkcs8",
            format: "pem",
        },
    });
};