aboutsummaryrefslogtreecommitdiff
path: root/client/src/utils/encryption.js
blob: e0e6c3317a7b4344ef39c8ae22dfaeb795b2492c (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
async function generateAESKey() {
  try {
    const key = await window.crypto.subtle.generateKey(
      {
        name: "AES-GCM",
        length: 256,
      },
      true,
      ["encrypt", "decrypt"],
    );
    return key;
  } catch (error) {
    console.error("Error generating AES key:", error);
  }
}

async function keyToString(key) {
  try {
    const exportedKey = await window.crypto.subtle.exportKey("raw", key);
    const keyString = btoa(String.fromCharCode(...new Uint8Array(exportedKey)));
    return keyString.replace(/\+/g, "-").replace(/\//g, "_");
  } catch (error) {
    console.error("Error converting key to string:", error);
  }
}

async function stringToKey(keyString) {
  try {
    const originalKeyString = keyString.replace(/-/g, "+").replace(/_/g, "/");
    const buffer = Uint8Array.from(atob(originalKeyString), (c) =>
      c.charCodeAt(0),
    ).buffer;

    const key = await window.crypto.subtle.importKey(
      "raw",
      buffer,
      { name: "AES-GCM", length: 256 },
      true,
      ["encrypt", "decrypt"],
    );

    return key;
  } catch (error) {
    console.error("Error converting string to key:", error);
  }
}

async function encryptAES(plaintext, key) {
  try {
    const iv = window.crypto.getRandomValues(new Uint8Array(12));
    const encrypted = await window.crypto.subtle.encrypt(
      {
        name: "AES-GCM",
        iv: iv,
      },
      key,
      new TextEncoder().encode(plaintext),
    );
    return { encrypted, iv };
  } catch (error) {
    console.error("Error encrypting:", error);
  }
}

async function decryptAES(encrypted, key, iv) {
  try {
    const decrypted = await window.crypto.subtle.decrypt(
      {
        name: "AES-GCM",
        iv: iv,
      },
      key,
      encrypted,
    );
    return new TextDecoder().decode(decrypted);
  } catch (error) {
    console.error("Error decrypting:", error);
  }
}

export { generateAESKey, keyToString, stringToKey, encryptAES, decryptAES };