Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
QuiteAFancyEmerald
GitHub Repository: QuiteAFancyEmerald/Holy-Unblocker
Path: blob/master/lib/rammerhead/src/util/StrShuffler.js
6530 views
1
/*
2
3
baseDictionary originally generated with (certain characters was removed to avoid breaking pages):
4
5
let str = '';
6
for (let i = 32; i <= 126; i++) {
7
let c = String.fromCharCode(i);
8
if (c !== '/' && c !== '_' && encodeURI(c).length === 1) str += c;
9
}
10
11
*/
12
13
const mod = (n, m) => ((n % m) + m) % m;
14
const baseDictionary = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~-';
15
const shuffledIndicator = '_rhs';
16
const generateDictionary = function () {
17
let str = '';
18
const split = baseDictionary.split('');
19
while (split.length > 0) {
20
str += split.splice(Math.floor(Math.random() * split.length), 1)[0];
21
}
22
return str;
23
};
24
class StrShuffler {
25
constructor(dictionary = generateDictionary()) {
26
this.dictionary = dictionary;
27
}
28
shuffle(str) {
29
if (str.startsWith(shuffledIndicator)) {
30
return str;
31
}
32
let shuffledStr = '';
33
for (let i = 0; i < str.length; i++) {
34
const char = str.charAt(i);
35
const idx = baseDictionary.indexOf(char);
36
if (char === '%' && str.length - i >= 3) {
37
shuffledStr += char;
38
shuffledStr += str.charAt(++i);
39
shuffledStr += str.charAt(++i);
40
} else if (idx === -1) {
41
shuffledStr += char;
42
} else {
43
shuffledStr += this.dictionary.charAt(mod(idx + i, baseDictionary.length));
44
}
45
}
46
return shuffledIndicator + shuffledStr;
47
}
48
unshuffle(str) {
49
if (!str.startsWith(shuffledIndicator)) {
50
return str;
51
}
52
53
str = str.slice(shuffledIndicator.length);
54
55
let unshuffledStr = '';
56
for (let i = 0; i < str.length; i++) {
57
const char = str.charAt(i);
58
const idx = this.dictionary.indexOf(char);
59
if (char === '%' && str.length - i >= 3) {
60
unshuffledStr += char;
61
unshuffledStr += str.charAt(++i);
62
unshuffledStr += str.charAt(++i);
63
} else if (idx === -1) {
64
unshuffledStr += char;
65
} else {
66
unshuffledStr += baseDictionary.charAt(mod(idx - i, baseDictionary.length));
67
}
68
}
69
return unshuffledStr;
70
}
71
}
72
73
StrShuffler.baseDictionary = baseDictionary;
74
StrShuffler.shuffledIndicator = shuffledIndicator;
75
StrShuffler.generateDictionary = generateDictionary;
76
77
module.exports = StrShuffler;
78
79