-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathchromium-cookie.ts
More file actions
43 lines (38 loc) · 1.05 KB
/
chromium-cookie.ts
File metadata and controls
43 lines (38 loc) · 1.05 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
import { pbkdf2Sync, createDecipheriv } from "node:crypto";
export function decryptChromiumCookieValue(
data: Buffer,
password: string,
iterations: number,
): string {
if (!data || data.length === 0) {
return "";
}
if (iterations < 1) {
throw new RangeError(`iterations must be >= 1, got ${iterations}`);
}
const salt = Buffer.from("saltysalt", "utf8");
const iv = Buffer.alloc(16, " ");
const key = pbkdf2Sync(password, salt, iterations, 16, "sha1");
const decipher = createDecipheriv("aes-128-cbc", key, iv);
decipher.setAutoPadding(true);
const plain = Buffer.concat([decipher.update(data), decipher.final()]);
const marker = Buffer.from("xoxd-");
const idx = plain.indexOf(marker);
if (idx === -1) {
return plain.toString("utf8");
}
let end = idx;
while (end < plain.length) {
const b = plain[end]!;
if (b < 0x21 || b > 0x7e) {
break;
}
end++;
}
const rawToken = plain.subarray(idx, end).toString("utf8");
try {
return decodeURIComponent(rawToken);
} catch {
return rawToken;
}
}