-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvisicoding.js
More file actions
70 lines (62 loc) · 1.76 KB
/
invisicoding.js
File metadata and controls
70 lines (62 loc) · 1.76 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
66
67
68
69
70
function getKey() {
// Numbers represent character codes of invisible characters
return [8203, 8299, 8291, 8297, 8236, 8234, 8296, 8300, 8298, 8302];
/**
* To visualize or debug encoded text, you can use the following
* key instead which will instead print numbers 0-9 instead of invisible characters.
*
* return [48, 49, 50, 51, 52, 53, 54, 55, 56, 57];
*/
}
function encode(text, secret) {
let encT = "";
let raw = 0;
let enc = 0;
while (raw < text.length || enc < secret.length) {
encT += text.charAt(raw++);
encT += encodeCharToSecret(secret.charAt(enc++));
}
return encT;
}
function encodeCharToCode(char) {
return char.charCodeAt(0) - 31;
}
function encodeCharToSecret(char) {
if (char == "") {
return "";
}
if (char.length > 1) {
throw new Error("Expected a single character, got multiple");
}
const code = encodeCharToCode(char);
const secretCharacterA = String.fromCharCode(getKey()[Math.floor(code / 10)]);
const secretCharacterB = String.fromCharCode(getKey()[code % 10]);
return secretCharacterA + secretCharacterB;
}
function decodeChar(charCodeA, charCodeB) {
const codeA = getKey().indexOf(charCodeA);
const codeB = getKey().indexOf(charCodeB);
return decodeCharFromCodes(codeA, codeB);
}
function decodeCharFromCodes(codeA, codeB) {
return String.fromCharCode(codeA * 10 + codeB + 31);
}
function decode(text) {
let secret = "";
for (let i = 1; i < text.length; i++) {
const charCode = text.charAt(i).charCodeAt(0);
if (getKey().indexOf(charCode) !== -1) {
secret += decodeChar(charCode, text.charAt(++i).charCodeAt(0));
}
}
return secret;
}
module.exports = {
encode,
encodeCharToCode,
encodeCharToSecret,
decode,
decodeChar,
decodeCharFromCodes,
getKey,
};