-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.js
More file actions
40 lines (35 loc) · 932 Bytes
/
cipher.js
File metadata and controls
40 lines (35 loc) · 932 Bytes
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
const { decypher } = require("./utils");
const getRandomMapping = () => {
let i = 0;
const map = {};
const a = "a".charCodeAt(0);
while (i < 26) {
const charCode = Math.floor(Math.random() * 26);
const char = String.fromCharCode(a + charCode);
if (Object.values(map).includes(char)) continue;
map[String.fromCharCode(a + i)] = char;
i++;
}
return map;
};
const offsetMap = (offset) => {
let i = 0;
const map = {};
const a = "a".charCodeAt(0);
while (i < 26) {
const charCode = (i + offset) % 26;
const char = String.fromCharCode(a + charCode);
map[String.fromCharCode(a + i)] = char;
i++;
}
return map;
};
cipherString = (string) => {
const map = getRandomMapping();
return { map, cipher: decypher(string, map) };
};
cipherCeasarString = (string) => {
const map = offsetMap(1);
return { map, cipher: decypher(string, map) };
};
module.exports = cipherString;