Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
118 lines (91 loc) · 3.84 KB
/
Copy pathindex.js
File metadata and controls
118 lines (91 loc) · 3.84 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
export class DelibAPI {
static #toUint8Array = str => (new TextEncoder()).encode(str);
static #toBase64 = bytes => btoa(bytes.reduce((s, b) => s + String.fromCharCode(b), ""));
static #fromBase64 = base64 => Uint8Array.from(atob(base64), char => char.charCodeAt(0));
static #isIPv6(ip) {
if (typeof ip !== "string") return;
try {
const url = new URL(`http://[${ip}]/`);
return url.hostname.startsWith("[") && url.hostname.endsWith("]");
} catch {
return;
}
}
static #isIPv4(ip) {
if (typeof ip !== "string") return;
const seg = ip.trim().split(".");
if (seg.length !== 4) return;
return seg.every(part =>
/^(0|[1-9]\d{0,2})$/.test(part) && Number(part) <= 255
);
}
/** True whenever ip is valid */
static validateIP = ip => this.#isIPv4(ip) || this.#isIPv6(ip);
/** True whenever data is valid */
static validateData = data => typeof data === "string" && data.trim().length >= 1;
/** True whenever secret (AES-GCM private key) is valid */
static validateSecret = secret_str => new TextEncoder().encode(secret_str).length == 32;
/** Encrypts data using aes-256-gcm.
* `data` - plain text data to encrypt;
* `secret_str` - 32-bytes key as a string.
*/
static async encrypt(data, secret_str) {
if (typeof data !== "string") return;
if (!this.validateSecret(secret_str)) return;
const secret_bytes = this.#toUint8Array(secret_str);
const iv = crypto.getRandomValues(new Uint8Array(12));
const secret = await crypto.subtle.importKey(
"raw", secret_bytes, { name: "AES-GCM" }, false, ["encrypt"]
);
const cipher_n_tag = new Uint8Array(await crypto.subtle.encrypt(
{ name: "AES-GCM", iv, tagLength: 128 }, secret, this.#toUint8Array(data)
));
const result = new Uint8Array(iv.length + cipher_n_tag.length);
result.set(iv, 0);
result.set(cipher_n_tag, iv.length);
return this.#toBase64(result);
}
/**
* Decrypts data using aes-256-gcm.
* `enc_base64` - an encrypted base64 hash string to decrypt.
* `secret_str` - 32-bytes key as a string.
* */
static async decrypt(enc_base64, secret_str) {
if (typeof enc_base64 !== "string") return;
if (!this.validateSecret(secret_str)) return;
const secret_bytes = this.#toUint8Array(secret_str);
const data = this.#fromBase64(enc_base64);
const iv_len = 12;
const tag_size = 16; // 16 * 8 = 128bit gcm tag
if (data.length < iv_len + tag_size) return;
const iv = data.slice(0, iv_len); // iv slice
const cipher_n_tag = data.slice(iv_len); // cipher + tag slice
const secret = await crypto.subtle.importKey(
"raw", secret_bytes, { name: "AES-GCM" }, false, ["decrypt"]
);
const result = await crypto.subtle.decrypt(
{ name: "AES-GCM", iv, tagLength: 128 }, secret, cipher_n_tag
);
return (new TextDecoder()).decode(result);
}
/** Sends async request with following arguments:
*
* `url_str` - Request URL as a `string`.
*
* `secret_str` - 32-bytes private key (AES_256_GCM) used to encrypt data transfer as a `string`.
*
* `data` - Data to send as a `string`.
*/
static async request(url_str, secret_str, data) {
let response = true;
const body = await this.encrypt(data, secret_str);
if (!body) return;
const req = await fetch(url_str, { method: "POST", body });
if (!req.ok) return;
const enc_base64 = await req.text();
if (enc_base64) { // if response has a body
response = await this.decrypt(enc_base64, secret_str);
}
return response;
}
}