-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagic.js
59 lines (49 loc) · 1.6 KB
/
magic.js
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
// "magic.js" gotten from Aarni Koskela (@akx)
// https://github.com/akx
const fs = require("fs");
function getMagic() {
// Database is not supplied, as it contains personal information.
// If you need it, get it from @akx.
if (fs.existsSync("./magic.json")) {
return new Map(require("./magic.json"));
}
return new Map();
}
const db = getMagic();
function toBase64(buffer) {
const buf = new Uint8Array(buffer);
return btoa(String.fromCharCode(...buf));
}
function fromBase64(str) {
return Uint8Array.from([...atob(str)].map((c) => c.charCodeAt(0)));
}
async function sha256(message) {
const msgBuffer = new TextEncoder().encode(message);
return await crypto.subtle.digest("SHA-256", msgBuffer);
}
async function aesCbcDecrypt(base64Bytes, key) {
const decKey = await crypto.subtle.importKey("raw", key, "AES-CBC", true, [
"decrypt",
]);
const fullBuf = fromBase64(base64Bytes);
const iv = fullBuf.slice(0, 16);
const ciphertext = fullBuf.slice(16);
return crypto.subtle.decrypt({ name: "AES-CBC", iv }, decKey, ciphertext);
}
async function doSearch(search) {
const res = [];
const ents = db.get(toBase64(await sha256("kk2024~" + search)));
if (!ents) return [];
let decKeyBits = new Array(search.length).fill(search).join("x");
const decKey = await sha256(decKeyBits);
for (const ent of ents) {
const plain = new TextDecoder().decode(await aesCbcDecrypt(ent, decKey));
const json = JSON.parse(plain);
if (json.im) {
json.im = json.im.replace("^se", "https://avatars.slack-edge.com/");
}
res.push(json);
}
return res;
}
module.exports = { doSearch };