Created
February 24, 2017 15:58
-
-
Save tarcisiozf/bdb582e8917b7533190705b7551d2929 to your computer and use it in GitHub Desktop.
ENIGMA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
Array.prototype.clone = function() { | |
return this.slice(0); | |
} | |
class Enigma { | |
constructor() { | |
this.alphabet = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']; | |
this.original_rotors = [ | |
['C','D','Z','R','M','G','H','X','A','W','Q','V','U','F','S','B','P','L','T','O','E','I','Y','N','K','J'], | |
['V','E','W','O','A','S','X','D','H','R','C','Y','Q','J','N','P','T','K','B','L','M','F','U','Z','G','I'], | |
['I','P','R','N','F','M','W','O','Z','J','Q','C','X','G','T','D','B','K','E','L','Y','V','A','H','S','U'], | |
['D','Q','N','T','S','X','Z','R','O','J','B','A','K','C','G','E','Y','P','U','I','H','F','L','M','V','W'], | |
['B','K','Q','S','L','P','M','D','C','A','N','Y','H','G','I','R','Z','V','J','F','T','U','X','E','O','W'], | |
// REFLECTOR | |
['O','K','Y','X','V','G','F','S','T','Q','B','W','N','M','A','U','J','Z','H','I','P','E','L','D','C','R'] | |
]; | |
this.num_plugs = 0; | |
this.num_rotors = 0; | |
this.rotors = [ | |
this.alphabet.clone() // entry rotor | |
]; | |
} | |
setPlug(letter1, letter2) { | |
if ( this.num_plugs == 6 ) { | |
return; | |
} | |
// change positions in the entry rotor | |
this.rotors[0][this.alphabet.indexOf(letter1)] = letter2; | |
this.rotors[0][this.alphabet.indexOf(letter2)] = letter1; | |
this.num_plugs++; | |
} | |
initalizeRotor(rotorIndex, entryLetter) { | |
if ( this.num_rotors == 3 ) { | |
return; | |
} | |
let entryIndex = this.alphabet.indexOf(entryLetter); | |
let rotor = this.original_rotors[rotorIndex - 1].clone(); | |
let new_rotor = []; | |
for(let i = entryIndex; i < 26; i++) { | |
new_rotor.push( rotor[i] ); | |
} | |
for(let i = 0; i < entryIndex; i++) { | |
new_rotor.push( rotor[i] ); | |
} | |
this.rotors.push(new_rotor); | |
this.num_rotors++; | |
// PLACE REFLECTOR AFTER ALL ROTORS | |
if ( this.num_rotors == 3 ) { | |
this.rotors.push( this.original_rotors[5].clone() ); | |
} | |
} | |
// Place the first letter in the end and move all to the left | |
rotateRotor(rotorIndex) { | |
let rotor = this.rotors[rotorIndex].clone(); | |
let temp = rotor[0]; | |
let new_rotor = rotor.slice(1, 26); | |
new_rotor.push(temp); | |
this.rotors[rotorIndex] = new_rotor; | |
} | |
encodeChar(char) { | |
let index; | |
// From the first rotor to the reflector | |
for(let i = 0; i < 5; i++) { | |
index = this.alphabet.indexOf(char); | |
// Not a alphabetical letter | |
if ( index < 0 ) { | |
return char; | |
} | |
char = this.rotors[i][index]; | |
} | |
// From the third rotor to the entry rotor | |
for(let i = 3; i >= 0; i--) { | |
index = this.rotors[i].indexOf(char); | |
char = this.alphabet[index]; | |
} | |
this.rotateRotor(3); | |
return char; | |
} | |
encode(text) { | |
return text.split('') | |
.map(c => this.encodeChar(c)) | |
.join(''); | |
} | |
} | |
let enigma = new Enigma; | |
enigma.setPlug('X', 'R'); | |
enigma.setPlug('Q', 'S'); | |
enigma.setPlug('J', 'M'); | |
enigma.setPlug('U', 'T'); | |
enigma.setPlug('C', 'F'); | |
enigma.setPlug('W', 'L'); | |
enigma.initalizeRotor(4, 'V'); | |
enigma.initalizeRotor(5, 'D'); | |
enigma.initalizeRotor(3, 'O'); | |
console.log(enigma.encode('HELLO WORLD')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment