-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathobfuscate.ts
64 lines (57 loc) · 1.55 KB
/
obfuscate.ts
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
/**
* Run this script with Bun:
*
* bun run scripts/obfuscate.ts "mailto:..."
*
* or with Node:
*
* npx tsx scripts/obfuscate.ts "mailto:..."
*/
const MAX_NUMBER = parseInt(process.env.MAX_NUMBER || '10000', 10);
const NUMBER = process.env.NUMBER ? parseInt(process.env.NUMBER || '0', 10) : undefined;
const KEY = process.env.KEY || '';
function randomInt(max: number) {
const ab = new Uint32Array(1);
crypto.getRandomValues(ab);
const randomNumber = ab[0] / (0xffffffff + 1);
return Math.floor(randomNumber * max + 1);
}
async function uInt8ArrayToBase64(ua: Uint8Array) {
return Buffer.from(ua).toString('base64');
}
function numberToUint8Array(num: number, len: number = 12) {
const ua = new Uint8Array(len);
for (let i = 0; i < len; i++) {
ua[i] = num % 256;
num = Math.floor(num / 256);
}
return ua;
}
async function obfuscateData(
raw: string,
key: string = '',
number: number = NUMBER || randomInt(MAX_NUMBER),
) {
const encoder = new TextEncoder();
const encodedData = encoder.encode(raw);
const algorithm = { name: 'AES-GCM', iv: numberToUint8Array(number) };
const keyHash = await crypto.subtle.digest(
'SHA-256',
encoder.encode(key)
);
const keyData = await crypto.subtle.importKey(
'raw',
keyHash,
algorithm,
false,
['encrypt']
);
const encryptedData = await crypto.subtle.encrypt(
algorithm,
keyData,
encodedData
);
return uInt8ArrayToBase64(new Uint8Array(encryptedData));
}
console.log(await obfuscateData(process.argv[process.argv.length - 1], KEY));
export {};