summaryrefslogtreecommitdiff
path: root/src/util/generator.ts
blob: 18dcf3217e1f87529aadc106a9f3699b5883b42b (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
35
36
37
38
39
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 generateMagicLinkToken = () => generateAlphanumericString(32);

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

export const hashString = (input: string) =>
    crypto.createHash("sha256").update(input).digest("hex");