-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobfuscate.ts
More file actions
65 lines (60 loc) · 2.08 KB
/
obfuscate.ts
File metadata and controls
65 lines (60 loc) · 2.08 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
/**
* Obfuscate a secret string using a key string.
* @param secret The secret string to obfuscate.
* @param key The key string used for obfuscation.
* @param rotNum The number of rotations to apply (defaults to 13).
* @returns The obfuscated string.
*/
async function obfuscateString(
secret: string,
key: string,
rotNum = 13,
): Promise<string> {
const keyHash = await hashString(key);
const xored = xorStrings(secret, keyHash);
const rotated = rotateString(xored, rotNum);
return btoa(rotated);
}
/**
* Hash a string using the Web Crypto API.
* @param str The string to hash.
* @returns The hash of the string as a hexadecimal string.
*/
async function hashString(str: string): Promise<string> {
const encoder = new TextEncoder();
const data = encoder.encode(str);
const hash = await crypto.subtle.digest("SHA-256", data);
return Array.from(new Uint8Array(hash))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
}
/**
* XOR two strings of the same length.
* @param str1 The first string.
* @param str2 The second string.
* @returns The result of XORing the two strings.
*/
function xorStrings(str1: string, str2: string): string {
const result = [];
for (let i = 0; i < str1.length; i++) {
// @ts-expect-error Not sure why TypeScript doesn't like this, but I have a headache and this works
result.push(String.fromCharCode(str1.charCodeAt(i) ^ str2.charCodeAt(i)));
}
return result.join("");
}
/**
* Rotate a string by a given number of positions (positive or negative).
* @param str The string to rotate.
* @param n The number of positions to rotate (positive for left rotation, negative for right rotation).
* @returns The rotated string.
*/
function rotateString(str: string, n: number): string {
const chars = str.split("");
n = n % chars.length;
if (n < 0) n += chars.length;
return chars.slice(n).concat(chars.slice(0, n)).join("");
}
const secret = process.env.HONEYCOMB_KEY;
const key = process.env.VITE_OBFUSCATION_SECRET;
const rot = process.env.VITE_OBFUSCATION_ROT;
obfuscateString(secret!, key!, Number(rot!)).then(console.log);