-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathencoder.js
36 lines (34 loc) · 1.22 KB
/
encoder.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
// ==== IMPORTS ====
import config from './config.js';
// ==== WORD HIDING ====
export function encode(word) {
const encodingString = config.encodingString[config.language];
const radix = encodingString.length;
const offset = Math.floor(Math.random() * (radix - 1) + 1);
const step = Math.floor(Math.random() * (radix - 1) + 1);
let result = "";
for (const [i, char] of word.split("").entries()) {
let index = encodingString.indexOf(char);
index += offset + i * step;
index %= radix;
result += encodingString[index];
}
result += encodingString[offset];
result += encodingString[step];
return result;
}
export function decode(code) {
const encodingString = config.encodingString[config.language];
const radix = encodingString.length;
const offset = encodingString.indexOf(code.slice(-2, -1));
const step = encodingString.indexOf(code.slice(-1));
code = code.slice(0, -2);
let result = "";
for (const [i, char] of code.split("").entries()) {
let index = encodingString.indexOf(char);
index -= offset + i * step;
index = ((index % radix) + radix) % radix;
result += encodingString[index];
}
return result;
}