-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencoder.ts
73 lines (59 loc) · 2.19 KB
/
encoder.ts
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
71
72
73
import config from "./config.json" assert { type: "json" };
const alphabet = "abcdefghijklmnopqrstuvwxyz".split("");
const rotor1Alphabet = "dmtwsilruyqnkfejcazbpgxohv".split("");
const rotor2Alphabet = "hqzgpjtmoblncifdyawveusrkx".split("");
const rotor3Alphabet = "uqntlszfmrehdpxkibvygjcwoa".split("");
export class Encoder {
rotors: [number, number, number];
plugboard: Record<string, string>;
output: string;
constructor() {
this.plugboard = config.plugboard;
this.rotors = config.rotors as [number, number, number];
this.output = "";
}
plugencode(input: string) {
for (const [key, value] of Object.entries(this.plugboard)) {
if (input === key) return value;
if (input === value) return key;
return input;
}
}
encode(input: string) {
for (let letter of input) {
// Check for space
if (letter === " ") return this.output += " ";
// Plugboard
letter = this.plugencode(letter) as string;
// Turn up
this.rotors[0]++;
for (let i = 0; i < 3; i++) {
if (this.rotors[i] === 26) {
this.rotors[i] = 0;
if (i < 2) this.rotors[i + 1]++;
}
}
// Rotor encoding
let letterNum = alphabet.indexOf(letter);
if (letterNum + this.rotors[0] > 25) letterNum -= 26;
letter = rotor1Alphabet[letterNum + this.rotors[0]];
letterNum = rotor1Alphabet.indexOf(letter);
if (letterNum + this.rotors[1] > 25) letterNum -= 26;
letter = rotor2Alphabet[letterNum + this.rotors[1]];
letterNum = rotor2Alphabet.indexOf(letter);
if (letterNum + this.rotors[2] > 25) letterNum -= 26;
letter = rotor3Alphabet[letterNum + this.rotors[2]];
letterNum = rotor3Alphabet.indexOf(letter);
if (letterNum + this.rotors[1] > 25) letterNum -= 26;
letter = rotor2Alphabet[letterNum + this.rotors[1]];
letterNum = rotor2Alphabet.indexOf(letter);
if (letterNum + this.rotors[0] > 25) letterNum -= 26;
letter = rotor1Alphabet[letterNum + this.rotors[0]];
letterNum = rotor1Alphabet.indexOf(letter);
// Plugboard
letter = this.plugencode(letter) as string;
this.output += letter;
}
return this.output;
}
}