Fixed grunt lint errors
This commit is contained in:
parent
5d01b06877
commit
938385c18b
@ -43,9 +43,9 @@ Converts a letter to uppercase (if it already isn't)
|
|||||||
@param {char} letter - letter to convert to upper case
|
@param {char} letter - letter to convert to upper case
|
||||||
@returns {char}
|
@returns {char}
|
||||||
*/
|
*/
|
||||||
export function convToUpperCase(letter){
|
export function convToUpperCase(letter) {
|
||||||
const charCode = letter.charCodeAt();
|
const charCode = letter.charCodeAt();
|
||||||
if (97<=charCode && charCode<=122){
|
if (97<=charCode && charCode<=122) {
|
||||||
return String.fromCharCode(charCode-32);
|
return String.fromCharCode(charCode-32);
|
||||||
}
|
}
|
||||||
return letter;
|
return letter;
|
||||||
@ -54,7 +54,7 @@ export function convToUpperCase(letter){
|
|||||||
/**
|
/**
|
||||||
The SIGABA machine consisting of the 3 rotor banks: cipher, control and index banks.
|
The SIGABA machine consisting of the 3 rotor banks: cipher, control and index banks.
|
||||||
*/
|
*/
|
||||||
export class SigabaMachine{
|
export class SigabaMachine {
|
||||||
/**
|
/**
|
||||||
SigabaMachine constructor
|
SigabaMachine constructor
|
||||||
|
|
||||||
@ -62,7 +62,7 @@ export class SigabaMachine{
|
|||||||
@param {Object[]} controlRotors - list of CRRotors
|
@param {Object[]} controlRotors - list of CRRotors
|
||||||
@param {object[]} indexRotors - list of IRotors
|
@param {object[]} indexRotors - list of IRotors
|
||||||
*/
|
*/
|
||||||
constructor(cipherRotors, controlRotors, indexRotors){
|
constructor(cipherRotors, controlRotors, indexRotors) {
|
||||||
this.cipherBank = new CipherBank(cipherRotors);
|
this.cipherBank = new CipherBank(cipherRotors);
|
||||||
this.controlBank = new ControlBank(controlRotors);
|
this.controlBank = new ControlBank(controlRotors);
|
||||||
this.indexBank = new IndexBank(indexRotors);
|
this.indexBank = new IndexBank(indexRotors);
|
||||||
@ -71,7 +71,7 @@ export class SigabaMachine{
|
|||||||
/**
|
/**
|
||||||
Steps all the correct rotors in the machine.
|
Steps all the correct rotors in the machine.
|
||||||
*/
|
*/
|
||||||
step(){
|
step() {
|
||||||
const controlOut = this.controlBank.goThroughControl();
|
const controlOut = this.controlBank.goThroughControl();
|
||||||
const indexOut = this.indexBank.goThroughIndex(controlOut);
|
const indexOut = this.indexBank.goThroughIndex(controlOut);
|
||||||
this.cipherBank.step(indexOut);
|
this.cipherBank.step(indexOut);
|
||||||
@ -83,12 +83,11 @@ export class SigabaMachine{
|
|||||||
@param {char} letter - letter to encrypt
|
@param {char} letter - letter to encrypt
|
||||||
@returns {char}
|
@returns {char}
|
||||||
*/
|
*/
|
||||||
encryptLetter(letter){
|
encryptLetter(letter) {
|
||||||
letter = convToUpperCase(letter);
|
letter = convToUpperCase(letter);
|
||||||
if (letter == " "){
|
if (letter === " ") {
|
||||||
letter = "Z";
|
letter = "Z";
|
||||||
}
|
} else if (letter === "Z") {
|
||||||
else if (letter == "Z") {
|
|
||||||
letter = "X";
|
letter = "X";
|
||||||
}
|
}
|
||||||
const encryptedLetter = this.cipherBank.encrypt(letter);
|
const encryptedLetter = this.cipherBank.encrypt(letter);
|
||||||
@ -102,10 +101,10 @@ export class SigabaMachine{
|
|||||||
@param {char} letter - letter to decrypt
|
@param {char} letter - letter to decrypt
|
||||||
@returns {char}
|
@returns {char}
|
||||||
*/
|
*/
|
||||||
decryptLetter(letter){
|
decryptLetter(letter) {
|
||||||
letter = convToUpperCase(letter);
|
letter = convToUpperCase(letter);
|
||||||
let decryptedLetter = this.cipherBank.decrypt(letter);
|
let decryptedLetter = this.cipherBank.decrypt(letter);
|
||||||
if (decryptedLetter == "Z"){
|
if (decryptedLetter === "Z") {
|
||||||
decryptedLetter = " ";
|
decryptedLetter = " ";
|
||||||
}
|
}
|
||||||
this.step();
|
this.step();
|
||||||
@ -118,9 +117,9 @@ export class SigabaMachine{
|
|||||||
@param {string} msg - message to encrypt
|
@param {string} msg - message to encrypt
|
||||||
@returns {string}
|
@returns {string}
|
||||||
*/
|
*/
|
||||||
encrypt(msg){
|
encrypt(msg) {
|
||||||
let ciphertext = "";
|
let ciphertext = "";
|
||||||
for (const letter of msg){
|
for (const letter of msg) {
|
||||||
ciphertext = ciphertext.concat(this.encryptLetter(letter));
|
ciphertext = ciphertext.concat(this.encryptLetter(letter));
|
||||||
}
|
}
|
||||||
return ciphertext;
|
return ciphertext;
|
||||||
@ -132,9 +131,9 @@ export class SigabaMachine{
|
|||||||
@param {string} msg - message to decrypt
|
@param {string} msg - message to decrypt
|
||||||
@returns {string}
|
@returns {string}
|
||||||
*/
|
*/
|
||||||
decrypt(msg){
|
decrypt(msg) {
|
||||||
let plaintext = "";
|
let plaintext = "";
|
||||||
for (const letter of msg){
|
for (const letter of msg) {
|
||||||
plaintext = plaintext.concat(this.decryptLetter(letter));
|
plaintext = plaintext.concat(this.decryptLetter(letter));
|
||||||
}
|
}
|
||||||
return plaintext;
|
return plaintext;
|
||||||
@ -145,13 +144,13 @@ export class SigabaMachine{
|
|||||||
/**
|
/**
|
||||||
The cipher rotor bank consists of 5 cipher rotors in either a forward or reversed orientation.
|
The cipher rotor bank consists of 5 cipher rotors in either a forward or reversed orientation.
|
||||||
*/
|
*/
|
||||||
export class CipherBank{
|
export class CipherBank {
|
||||||
/**
|
/**
|
||||||
CipherBank constructor
|
CipherBank constructor
|
||||||
|
|
||||||
@param {Object[]} rotors - list of CRRotors
|
@param {Object[]} rotors - list of CRRotors
|
||||||
*/
|
*/
|
||||||
constructor(rotors){
|
constructor(rotors) {
|
||||||
this.rotors = rotors;
|
this.rotors = rotors;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,8 +160,8 @@ export class CipherBank{
|
|||||||
@param {char} inputPos - the input position of the signal (letter to be encrypted)
|
@param {char} inputPos - the input position of the signal (letter to be encrypted)
|
||||||
@returns {char}
|
@returns {char}
|
||||||
*/
|
*/
|
||||||
encrypt(inputPos){
|
encrypt(inputPos) {
|
||||||
for (let rotor of this.rotors){
|
for (const rotor of this.rotors) {
|
||||||
inputPos = rotor.crypt(inputPos, "leftToRight");
|
inputPos = rotor.crypt(inputPos, "leftToRight");
|
||||||
}
|
}
|
||||||
return inputPos;
|
return inputPos;
|
||||||
@ -174,9 +173,9 @@ export class CipherBank{
|
|||||||
@param {char} inputPos - the input position of the signal (letter to be decrypted)
|
@param {char} inputPos - the input position of the signal (letter to be decrypted)
|
||||||
@returns {char}
|
@returns {char}
|
||||||
*/
|
*/
|
||||||
decrypt(inputPos){
|
decrypt(inputPos) {
|
||||||
const revOrderedRotors = [...this.rotors].reverse();
|
const revOrderedRotors = [...this.rotors].reverse();
|
||||||
for (let rotor of revOrderedRotors){
|
for (const rotor of revOrderedRotors) {
|
||||||
inputPos = rotor.crypt(inputPos, "rightToLeft");
|
inputPos = rotor.crypt(inputPos, "rightToLeft");
|
||||||
}
|
}
|
||||||
return inputPos;
|
return inputPos;
|
||||||
@ -187,19 +186,19 @@ export class CipherBank{
|
|||||||
|
|
||||||
@param {number[]} indexInputs - the inputs from the index rotors
|
@param {number[]} indexInputs - the inputs from the index rotors
|
||||||
*/
|
*/
|
||||||
step(indexInputs){
|
step(indexInputs) {
|
||||||
const logicDict = {0: [0,9], 1:[7,8], 2:[5,6], 3:[3,4], 4:[1,2]};
|
const logicDict = {0: [0, 9], 1: [7, 8], 2: [5, 6], 3: [3, 4], 4: [1, 2]};
|
||||||
let rotorsToMove = [];
|
const rotorsToMove = [];
|
||||||
for (const key in logicDict){
|
for (const key in logicDict) {
|
||||||
const item = logicDict[key];
|
const item = logicDict[key];
|
||||||
for (const i of indexInputs){
|
for (const i of indexInputs) {
|
||||||
if (item.includes(i)){
|
if (item.includes(i)) {
|
||||||
rotorsToMove.push(this.rotors[key]);
|
rotorsToMove.push(this.rotors[key]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (let rotor of rotorsToMove){
|
for (const rotor of rotorsToMove) {
|
||||||
rotor.step();
|
rotor.step();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -209,13 +208,13 @@ export class CipherBank{
|
|||||||
/**
|
/**
|
||||||
The control rotor bank consists of 5 control rotors in either a forward or reversed orientation. Signals to the control rotor bank always go from right-to-left.
|
The control rotor bank consists of 5 control rotors in either a forward or reversed orientation. Signals to the control rotor bank always go from right-to-left.
|
||||||
*/
|
*/
|
||||||
export class ControlBank{
|
export class ControlBank {
|
||||||
/**
|
/**
|
||||||
ControlBank constructor. The rotors have been reversed as signals go from right-to-left through the control rotors.
|
ControlBank constructor. The rotors have been reversed as signals go from right-to-left through the control rotors.
|
||||||
|
|
||||||
@param {Object[]} rotors - list of CRRotors
|
@param {Object[]} rotors - list of CRRotors
|
||||||
*/
|
*/
|
||||||
constructor(rotors){
|
constructor(rotors) {
|
||||||
this.rotors = [...rotors].reverse();
|
this.rotors = [...rotors].reverse();
|
||||||
this.numberOfMoves = 1;
|
this.numberOfMoves = 1;
|
||||||
}
|
}
|
||||||
@ -226,8 +225,8 @@ export class ControlBank{
|
|||||||
@param {char} inputPos - the input position of the signal
|
@param {char} inputPos - the input position of the signal
|
||||||
@returns {char}
|
@returns {char}
|
||||||
*/
|
*/
|
||||||
crypt(inputPos){
|
crypt(inputPos) {
|
||||||
for (let rotor of this.rotors){
|
for (const rotor of this.rotors) {
|
||||||
inputPos = rotor.crypt(inputPos, "rightToLeft");
|
inputPos = rotor.crypt(inputPos, "rightToLeft");
|
||||||
}
|
}
|
||||||
return inputPos;
|
return inputPos;
|
||||||
@ -238,14 +237,14 @@ export class ControlBank{
|
|||||||
|
|
||||||
@returns {number[]}
|
@returns {number[]}
|
||||||
*/
|
*/
|
||||||
getOutputs(){
|
getOutputs() {
|
||||||
const outputs = [this.crypt("F"), this.crypt("G"), this.crypt("H"), this.crypt("I")];
|
const outputs = [this.crypt("F"), this.crypt("G"), this.crypt("H"), this.crypt("I")];
|
||||||
const logicDict = {1:"B", 2:"C", 3:"DE", 4:"FGH", 5:"IJK", 6:"LMNO", 7:"PQRST", 8:"UVWXYZ", 9:"A"};
|
const logicDict = {1: "B", 2: "C", 3: "DE", 4: "FGH", 5: "IJK", 6: "LMNO", 7: "PQRST", 8: "UVWXYZ", 9: "A"};
|
||||||
let numberOutputs = [];
|
const numberOutputs = [];
|
||||||
for (let key in logicDict){
|
for (const key in logicDict) {
|
||||||
const item = logicDict[key];
|
const item = logicDict[key];
|
||||||
for (let output of outputs){
|
for (const output of outputs) {
|
||||||
if (item.includes(output)){
|
if (item.includes(output)) {
|
||||||
numberOutputs.push(key);
|
numberOutputs.push(key);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -257,14 +256,14 @@ export class ControlBank{
|
|||||||
/**
|
/**
|
||||||
Steps the control rotors. Only 3 of the control rotors step: one after every encryption, one after every 26, and one after every 26 squared.
|
Steps the control rotors. Only 3 of the control rotors step: one after every encryption, one after every 26, and one after every 26 squared.
|
||||||
*/
|
*/
|
||||||
step(){
|
step() {
|
||||||
const MRotor = this.rotors[1], FRotor = this.rotors[2], SRotor = this.rotors[3];
|
const MRotor = this.rotors[1], FRotor = this.rotors[2], SRotor = this.rotors[3];
|
||||||
this.numberOfMoves ++;
|
this.numberOfMoves ++;
|
||||||
FRotor.step();
|
FRotor.step();
|
||||||
if (this.numberOfMoves%26 == 0){
|
if (this.numberOfMoves%26 === 0) {
|
||||||
MRotor.step();
|
MRotor.step();
|
||||||
}
|
}
|
||||||
if (this.numberOfMoves%(26*26) == 0){
|
if (this.numberOfMoves%(26*26) === 0) {
|
||||||
SRotor.step();
|
SRotor.step();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -274,7 +273,7 @@ export class ControlBank{
|
|||||||
|
|
||||||
@returns {number[]}
|
@returns {number[]}
|
||||||
*/
|
*/
|
||||||
goThroughControl(){
|
goThroughControl() {
|
||||||
const outputs = this.getOutputs();
|
const outputs = this.getOutputs();
|
||||||
this.step();
|
this.step();
|
||||||
return outputs;
|
return outputs;
|
||||||
@ -285,13 +284,13 @@ export class ControlBank{
|
|||||||
/**
|
/**
|
||||||
The index rotor bank consists of 5 index rotors all placed in the forwards orientation.
|
The index rotor bank consists of 5 index rotors all placed in the forwards orientation.
|
||||||
*/
|
*/
|
||||||
export class IndexBank{
|
export class IndexBank {
|
||||||
/**
|
/**
|
||||||
IndexBank constructor
|
IndexBank constructor
|
||||||
|
|
||||||
@param {Object[]} rotors - list of IRotors
|
@param {Object[]} rotors - list of IRotors
|
||||||
*/
|
*/
|
||||||
constructor(rotors){
|
constructor(rotors) {
|
||||||
this.rotors = rotors;
|
this.rotors = rotors;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -301,8 +300,8 @@ export class IndexBank{
|
|||||||
@param {number} inputPos - the input position of the signal
|
@param {number} inputPos - the input position of the signal
|
||||||
@returns {number}
|
@returns {number}
|
||||||
*/
|
*/
|
||||||
crypt(inputPos){
|
crypt(inputPos) {
|
||||||
for (let rotor of this.rotors){
|
for (const rotor of this.rotors) {
|
||||||
inputPos = rotor.crypt(inputPos);
|
inputPos = rotor.crypt(inputPos);
|
||||||
}
|
}
|
||||||
return inputPos;
|
return inputPos;
|
||||||
@ -314,9 +313,9 @@ export class IndexBank{
|
|||||||
@param {number[]} - inputs from the control rotors
|
@param {number[]} - inputs from the control rotors
|
||||||
@returns {number[]}
|
@returns {number[]}
|
||||||
*/
|
*/
|
||||||
goThroughIndex(controlInputs){
|
goThroughIndex(controlInputs) {
|
||||||
let outputs = [];
|
const outputs = [];
|
||||||
for (const inp of controlInputs){
|
for (const inp of controlInputs) {
|
||||||
outputs.push(this.crypt(inp));
|
outputs.push(this.crypt(inp));
|
||||||
}
|
}
|
||||||
return outputs;
|
return outputs;
|
||||||
@ -327,7 +326,7 @@ export class IndexBank{
|
|||||||
/**
|
/**
|
||||||
Rotor class
|
Rotor class
|
||||||
*/
|
*/
|
||||||
export class Rotor{
|
export class Rotor {
|
||||||
/**
|
/**
|
||||||
Rotor constructor
|
Rotor constructor
|
||||||
|
|
||||||
@ -335,7 +334,7 @@ export class Rotor{
|
|||||||
@param {bool} rev - true if the rotor is reversed, false if it isn't
|
@param {bool} rev - true if the rotor is reversed, false if it isn't
|
||||||
@param {number} key - the starting position or state of the rotor
|
@param {number} key - the starting position or state of the rotor
|
||||||
*/
|
*/
|
||||||
constructor(wireSetting, key, rev){
|
constructor(wireSetting, key, rev) {
|
||||||
this.state = key;
|
this.state = key;
|
||||||
this.numMapping = this.getNumMapping(wireSetting, rev);
|
this.numMapping = this.getNumMapping(wireSetting, rev);
|
||||||
this.posMapping = this.getPosMapping(rev);
|
this.posMapping = this.getPosMapping(rev);
|
||||||
@ -348,14 +347,13 @@ export class Rotor{
|
|||||||
@param {bool} rev - true if reversed, false if not
|
@param {bool} rev - true if reversed, false if not
|
||||||
@returns {number[]}
|
@returns {number[]}
|
||||||
*/
|
*/
|
||||||
getNumMapping(wireSetting, rev){
|
getNumMapping(wireSetting, rev) {
|
||||||
if (rev==false){
|
if (rev===false) {
|
||||||
return wireSetting;
|
return wireSetting;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
const length = wireSetting.length;
|
const length = wireSetting.length;
|
||||||
let tempMapping = new Array(length);
|
const tempMapping = new Array(length);
|
||||||
for (let i=0; i<length; i++){
|
for (let i=0; i<length; i++) {
|
||||||
tempMapping[wireSetting[i]] = i;
|
tempMapping[wireSetting[i]] = i;
|
||||||
}
|
}
|
||||||
return tempMapping;
|
return tempMapping;
|
||||||
@ -368,22 +366,21 @@ export class Rotor{
|
|||||||
@param {bool} rev - true if reversed, false if not
|
@param {bool} rev - true if reversed, false if not
|
||||||
@returns {number[]}
|
@returns {number[]}
|
||||||
*/
|
*/
|
||||||
getPosMapping(rev){
|
getPosMapping(rev) {
|
||||||
const length = this.numMapping.length;
|
const length = this.numMapping.length;
|
||||||
let posMapping = [];
|
const posMapping = [];
|
||||||
if (rev==false){
|
if (rev===false) {
|
||||||
for (let i=this.state; i<this.state+length; i++){
|
for (let i = this.state; i < this.state+length; i++) {
|
||||||
let res = i%length;
|
let res = i%length;
|
||||||
if (res<0){
|
if (res<0) {
|
||||||
res += length;
|
res += length;
|
||||||
}
|
}
|
||||||
posMapping.push(res);
|
posMapping.push(res);
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
for (let i = this.state; i > this.state-length; i--) {
|
||||||
for (let i=this.state; i>this.state-length; i--){
|
|
||||||
let res = i%length;
|
let res = i%length;
|
||||||
if (res<0){
|
if (res<0) {
|
||||||
res += length;
|
res += length;
|
||||||
}
|
}
|
||||||
posMapping.push(res);
|
posMapping.push(res);
|
||||||
@ -399,13 +396,12 @@ export class Rotor{
|
|||||||
@param {string} direction - one of "leftToRight" and "rightToLeft", states the direction in which the signal passes through the rotor
|
@param {string} direction - one of "leftToRight" and "rightToLeft", states the direction in which the signal passes through the rotor
|
||||||
@returns {number}
|
@returns {number}
|
||||||
*/
|
*/
|
||||||
cryptNum(inputPos, direction){
|
cryptNum(inputPos, direction) {
|
||||||
const inpNum = this.posMapping[inputPos];
|
const inpNum = this.posMapping[inputPos];
|
||||||
var outNum;
|
let outNum;
|
||||||
if (direction == "leftToRight"){
|
if (direction === "leftToRight") {
|
||||||
outNum = this.numMapping[inpNum];
|
outNum = this.numMapping[inpNum];
|
||||||
}
|
} else if (direction === "rightToLeft") {
|
||||||
else if (direction == "rightToLeft") {
|
|
||||||
outNum = this.numMapping.indexOf(inpNum);
|
outNum = this.numMapping.indexOf(inpNum);
|
||||||
}
|
}
|
||||||
const outPos = this.posMapping.indexOf(outNum);
|
const outPos = this.posMapping.indexOf(outNum);
|
||||||
@ -415,7 +411,7 @@ export class Rotor{
|
|||||||
/**
|
/**
|
||||||
Steps the rotor. The number at position 0 will be moved to position 1 etc.
|
Steps the rotor. The number at position 0 will be moved to position 1 etc.
|
||||||
*/
|
*/
|
||||||
step(){
|
step() {
|
||||||
const lastNum = this.posMapping.pop();
|
const lastNum = this.posMapping.pop();
|
||||||
this.posMapping.splice(0, 0, lastNum);
|
this.posMapping.splice(0, 0, lastNum);
|
||||||
this.state = this.posMapping[0];
|
this.state = this.posMapping[0];
|
||||||
@ -426,7 +422,7 @@ export class Rotor{
|
|||||||
/**
|
/**
|
||||||
A CRRotor is a cipher (C) or control (R) rotor. These rotors are identical and interchangeable. A C or R rotor consists of 26 contacts, one for each letter, and may be put into either a forwards of reversed orientation.
|
A CRRotor is a cipher (C) or control (R) rotor. These rotors are identical and interchangeable. A C or R rotor consists of 26 contacts, one for each letter, and may be put into either a forwards of reversed orientation.
|
||||||
*/
|
*/
|
||||||
export class CRRotor extends Rotor{
|
export class CRRotor extends Rotor {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
CRRotor constructor
|
CRRotor constructor
|
||||||
@ -435,7 +431,7 @@ export class CRRotor extends Rotor{
|
|||||||
@param {char} key - initial state of rotor
|
@param {char} key - initial state of rotor
|
||||||
@param {bool} rev - true if reversed, false if not
|
@param {bool} rev - true if reversed, false if not
|
||||||
*/
|
*/
|
||||||
constructor(wireSetting, key, rev=false){
|
constructor(wireSetting, key, rev=false) {
|
||||||
wireSetting = wireSetting.split("").map(CRRotor.letterToNum);
|
wireSetting = wireSetting.split("").map(CRRotor.letterToNum);
|
||||||
super(wireSetting, CRRotor.letterToNum(key), rev);
|
super(wireSetting, CRRotor.letterToNum(key), rev);
|
||||||
}
|
}
|
||||||
@ -446,7 +442,7 @@ export class CRRotor extends Rotor{
|
|||||||
@param {char} letter - letter to convert to number
|
@param {char} letter - letter to convert to number
|
||||||
@returns {number}
|
@returns {number}
|
||||||
*/
|
*/
|
||||||
static letterToNum(letter){
|
static letterToNum(letter) {
|
||||||
return letter.charCodeAt()-65;
|
return letter.charCodeAt()-65;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -456,7 +452,7 @@ export class CRRotor extends Rotor{
|
|||||||
@param {number} num - number to convert to letter
|
@param {number} num - number to convert to letter
|
||||||
@returns {char}
|
@returns {char}
|
||||||
*/
|
*/
|
||||||
static numToLetter(num){
|
static numToLetter(num) {
|
||||||
return String.fromCharCode(num+65);
|
return String.fromCharCode(num+65);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -467,7 +463,7 @@ export class CRRotor extends Rotor{
|
|||||||
@param {string} direction - one of "leftToRight" and "rightToLeft"
|
@param {string} direction - one of "leftToRight" and "rightToLeft"
|
||||||
@returns {char}
|
@returns {char}
|
||||||
*/
|
*/
|
||||||
crypt(inputPos, direction){
|
crypt(inputPos, direction) {
|
||||||
inputPos = CRRotor.letterToNum(inputPos);
|
inputPos = CRRotor.letterToNum(inputPos);
|
||||||
const outPos = this.cryptNum(inputPos, direction);
|
const outPos = this.cryptNum(inputPos, direction);
|
||||||
return CRRotor.numToLetter(outPos);
|
return CRRotor.numToLetter(outPos);
|
||||||
@ -478,14 +474,14 @@ export class CRRotor extends Rotor{
|
|||||||
/**
|
/**
|
||||||
An IRotor is an index rotor, which consists of 10 contacts each numbered from 0 to 9. Unlike C and R rotors, they cannot be put in the reversed orientation. The index rotors do not step at any point during encryption or decryption.
|
An IRotor is an index rotor, which consists of 10 contacts each numbered from 0 to 9. Unlike C and R rotors, they cannot be put in the reversed orientation. The index rotors do not step at any point during encryption or decryption.
|
||||||
*/
|
*/
|
||||||
export class IRotor extends Rotor{
|
export class IRotor extends Rotor {
|
||||||
/**
|
/**
|
||||||
IRotor constructor
|
IRotor constructor
|
||||||
|
|
||||||
@param {string} wireSetting - the rotor wirings (string of numbers)
|
@param {string} wireSetting - the rotor wirings (string of numbers)
|
||||||
@param {char} key - initial state of rotor
|
@param {char} key - initial state of rotor
|
||||||
*/
|
*/
|
||||||
constructor(wireSetting, key){
|
constructor(wireSetting, key) {
|
||||||
wireSetting = wireSetting.split("").map(Number);
|
wireSetting = wireSetting.split("").map(Number);
|
||||||
super(wireSetting, Number(key), false);
|
super(wireSetting, Number(key), false);
|
||||||
}
|
}
|
||||||
@ -496,7 +492,7 @@ export class IRotor extends Rotor{
|
|||||||
@param {number} inputPos - the input position of the signal
|
@param {number} inputPos - the input position of the signal
|
||||||
@returns {number}
|
@returns {number}
|
||||||
*/
|
*/
|
||||||
crypt(inputPos){
|
crypt(inputPos) {
|
||||||
return this.cryptNum(inputPos, "leftToRight");
|
return this.cryptNum(inputPos, "leftToRight");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,285 +7,276 @@ Emulation of the SIGABA machine.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import Operation from "../Operation.mjs";
|
import Operation from "../Operation.mjs";
|
||||||
import OperationError from "../errors/OperationError.mjs";
|
|
||||||
import {LETTERS} from "../lib/Enigma.mjs";
|
import {LETTERS} from "../lib/Enigma.mjs";
|
||||||
import {NUMBERS, CR_ROTORS, I_ROTORS, SigabaMachine, CRRotor, IRotor} from "../lib/SIGABA.mjs";
|
import {NUMBERS, CR_ROTORS, I_ROTORS, SigabaMachine, CRRotor, IRotor} from "../lib/SIGABA.mjs";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Sigaba operation
|
Sigaba operation
|
||||||
*/
|
*/
|
||||||
class Sigaba extends Operation{
|
class Sigaba extends Operation {
|
||||||
/**
|
/**
|
||||||
Sigaba constructor
|
Sigaba constructor
|
||||||
*/
|
*/
|
||||||
constructor(){
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.name = "SIGABA";
|
this.name = "SIGABA";
|
||||||
this.module = "SIGABA";
|
this.module = "SIGABA";
|
||||||
this.description = "Encipher/decipher with the WW2 SIGABA machine. <br><br>SIGABA, otherwise known as ECM Mark II, was used by the United States for message encryption during WW2 up to the 1950s. It was developed in the 1930s by the US Army and Navy, and has up to this day never been broken. Consisting of 15 rotors: 5 cipher rotors and 10 rotors (5 control rotors and 5 index rotors) controlling the stepping of the cipher rotors, the rotor stepping for SIGABA is much more complex than other rotor machines of its time, such as Enigma. All example rotor wirings are random example sets.<br><br>To configure rotor wirings, for the cipher and control rotors enter a string of letters which map from A to Z, and for the index rotors enter a sequence of numbers which map from 0 to 9. Note that encryption is not the same as decryption, so first choose the desired mode.";
|
this.description = "Encipher/decipher with the WW2 SIGABA machine. <br><br>SIGABA, otherwise known as ECM Mark II, was used by the United States for message encryption during WW2 up to the 1950s. It was developed in the 1930s by the US Army and Navy, and has up to this day never been broken. Consisting of 15 rotors: 5 cipher rotors and 10 rotors (5 control rotors and 5 index rotors) controlling the stepping of the cipher rotors, the rotor stepping for SIGABA is much more complex than other rotor machines of its time, such as Enigma. All example rotor wirings are random example sets.<br><br>To configure rotor wirings, for the cipher and control rotors enter a string of letters which map from A to Z, and for the index rotors enter a sequence of numbers which map from 0 to 9. Note that encryption is not the same as decryption, so first choose the desired mode.";
|
||||||
this.infoURL = "https://en.wikipedia.org/wiki/SIGABA";
|
this.infoURL = "https://en.wikipedia.org/wiki/SIGABA";
|
||||||
this.inputType = "string";
|
this.inputType = "string";
|
||||||
this.outputType = "string";
|
this.outputType = "string";
|
||||||
this.args = [
|
this.args = [
|
||||||
{
|
{
|
||||||
name: "1st (left-hand) cipher rotor",
|
name: "1st (left-hand) cipher rotor",
|
||||||
type: "editableOption",
|
type: "editableOption",
|
||||||
value: CR_ROTORS,
|
value: CR_ROTORS,
|
||||||
defaultIndex: 0
|
defaultIndex: 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "1st cipher rotor reversed",
|
name: "1st cipher rotor reversed",
|
||||||
type: "boolean",
|
type: "boolean",
|
||||||
value: false
|
value: false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "1st cipher rotor intial value",
|
name: "1st cipher rotor intial value",
|
||||||
type: "option",
|
type: "option",
|
||||||
value: LETTERS
|
value: LETTERS
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "2nd cipher rotor",
|
name: "2nd cipher rotor",
|
||||||
type: "editableOption",
|
type: "editableOption",
|
||||||
value: CR_ROTORS,
|
value: CR_ROTORS,
|
||||||
defaultIndex: 0
|
defaultIndex: 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "2nd cipher rotor reversed",
|
name: "2nd cipher rotor reversed",
|
||||||
type: "boolean",
|
type: "boolean",
|
||||||
value: false
|
value: false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "2nd cipher rotor intial value",
|
name: "2nd cipher rotor intial value",
|
||||||
type: "option",
|
type: "option",
|
||||||
value: LETTERS
|
value: LETTERS
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "3rd (middle) cipher rotor",
|
name: "3rd (middle) cipher rotor",
|
||||||
type: "editableOption",
|
type: "editableOption",
|
||||||
value: CR_ROTORS,
|
value: CR_ROTORS,
|
||||||
defaultIndex: 0
|
defaultIndex: 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "3rd cipher rotor reversed",
|
name: "3rd cipher rotor reversed",
|
||||||
type: "boolean",
|
type: "boolean",
|
||||||
value: false
|
value: false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "3rd cipher rotor intial value",
|
name: "3rd cipher rotor intial value",
|
||||||
type: "option",
|
type: "option",
|
||||||
value: LETTERS
|
value: LETTERS
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "4th cipher rotor",
|
name: "4th cipher rotor",
|
||||||
type: "editableOption",
|
type: "editableOption",
|
||||||
value: CR_ROTORS,
|
value: CR_ROTORS,
|
||||||
defaultIndex: 0
|
defaultIndex: 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "4th cipher rotor reversed",
|
name: "4th cipher rotor reversed",
|
||||||
type: "boolean",
|
type: "boolean",
|
||||||
value: false
|
value: false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "4th cipher rotor intial value",
|
name: "4th cipher rotor intial value",
|
||||||
type: "option",
|
type: "option",
|
||||||
value: LETTERS
|
value: LETTERS
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "5th (right-hand) cipher rotor",
|
name: "5th (right-hand) cipher rotor",
|
||||||
type: "editableOption",
|
type: "editableOption",
|
||||||
value: CR_ROTORS,
|
value: CR_ROTORS,
|
||||||
defaultIndex: 0
|
defaultIndex: 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "5th cipher rotor reversed",
|
name: "5th cipher rotor reversed",
|
||||||
type: "boolean",
|
type: "boolean",
|
||||||
value: false
|
value: false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "5th cipher rotor intial value",
|
name: "5th cipher rotor intial value",
|
||||||
type: "option",
|
type: "option",
|
||||||
value: LETTERS
|
value: LETTERS
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "1st (left-hand) control rotor",
|
name: "1st (left-hand) control rotor",
|
||||||
type: "editableOption",
|
type: "editableOption",
|
||||||
value: CR_ROTORS,
|
value: CR_ROTORS,
|
||||||
defaultIndex: 0
|
defaultIndex: 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "1st control rotor reversed",
|
name: "1st control rotor reversed",
|
||||||
type: "boolean",
|
type: "boolean",
|
||||||
value: false
|
value: false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "1st control rotor intial value",
|
name: "1st control rotor intial value",
|
||||||
type: "option",
|
type: "option",
|
||||||
value: LETTERS
|
value: LETTERS
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "2nd control rotor",
|
name: "2nd control rotor",
|
||||||
type: "editableOption",
|
type: "editableOption",
|
||||||
value: CR_ROTORS,
|
value: CR_ROTORS,
|
||||||
defaultIndex: 0
|
defaultIndex: 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "2nd control rotor reversed",
|
name: "2nd control rotor reversed",
|
||||||
type: "boolean",
|
type: "boolean",
|
||||||
value: false
|
value: false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "2nd control rotor intial value",
|
name: "2nd control rotor intial value",
|
||||||
type: "option",
|
type: "option",
|
||||||
value: LETTERS
|
value: LETTERS
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "3rd (middle) control rotor",
|
name: "3rd (middle) control rotor",
|
||||||
type: "editableOption",
|
type: "editableOption",
|
||||||
value: CR_ROTORS,
|
value: CR_ROTORS,
|
||||||
defaultIndex: 0
|
defaultIndex: 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "3rd control rotor reversed",
|
name: "3rd control rotor reversed",
|
||||||
type: "boolean",
|
type: "boolean",
|
||||||
value: false
|
value: false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "3rd control rotor intial value",
|
name: "3rd control rotor intial value",
|
||||||
type: "option",
|
type: "option",
|
||||||
value: LETTERS
|
value: LETTERS
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "4th control rotor",
|
name: "4th control rotor",
|
||||||
type: "editableOption",
|
type: "editableOption",
|
||||||
value: CR_ROTORS,
|
value: CR_ROTORS,
|
||||||
defaultIndex: 0
|
defaultIndex: 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "4th control rotor reversed",
|
name: "4th control rotor reversed",
|
||||||
type: "boolean",
|
type: "boolean",
|
||||||
value: false
|
value: false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "4th control rotor intial value",
|
name: "4th control rotor intial value",
|
||||||
type: "option",
|
type: "option",
|
||||||
value: LETTERS
|
value: LETTERS
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "5th (right-hand) control rotor",
|
name: "5th (right-hand) control rotor",
|
||||||
type: "editableOption",
|
type: "editableOption",
|
||||||
value: CR_ROTORS,
|
value: CR_ROTORS,
|
||||||
defaultIndex: 0
|
defaultIndex: 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "5th control rotor reversed",
|
name: "5th control rotor reversed",
|
||||||
type: "boolean",
|
type: "boolean",
|
||||||
value: false
|
value: false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "5th control rotor intial value",
|
name: "5th control rotor intial value",
|
||||||
type: "option",
|
type: "option",
|
||||||
value: LETTERS
|
value: LETTERS
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "1st (left-hand) index rotor",
|
name: "1st (left-hand) index rotor",
|
||||||
type: "editableOption",
|
type: "editableOption",
|
||||||
value: I_ROTORS,
|
value: I_ROTORS,
|
||||||
defaultIndex: 0
|
defaultIndex: 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "1st index rotor intial value",
|
name: "1st index rotor intial value",
|
||||||
type: "option",
|
type: "option",
|
||||||
value: NUMBERS
|
value: NUMBERS
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "2nd index rotor",
|
name: "2nd index rotor",
|
||||||
type: "editableOption",
|
type: "editableOption",
|
||||||
value: I_ROTORS,
|
value: I_ROTORS,
|
||||||
defaultIndex: 0
|
defaultIndex: 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "2nd index rotor intial value",
|
name: "2nd index rotor intial value",
|
||||||
type: "option",
|
type: "option",
|
||||||
value: NUMBERS
|
value: NUMBERS
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "3rd (middle) index rotor",
|
name: "3rd (middle) index rotor",
|
||||||
type: "editableOption",
|
type: "editableOption",
|
||||||
value: I_ROTORS,
|
value: I_ROTORS,
|
||||||
defaultIndex: 0
|
defaultIndex: 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "3rd index rotor intial value",
|
name: "3rd index rotor intial value",
|
||||||
type: "option",
|
type: "option",
|
||||||
value: NUMBERS
|
value: NUMBERS
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "4th index rotor",
|
name: "4th index rotor",
|
||||||
type: "editableOption",
|
type: "editableOption",
|
||||||
value: I_ROTORS,
|
value: I_ROTORS,
|
||||||
defaultIndex: 0
|
defaultIndex: 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "4th index rotor intial value",
|
name: "4th index rotor intial value",
|
||||||
type: "option",
|
type: "option",
|
||||||
value: NUMBERS
|
value: NUMBERS
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "5th (right-hand) index rotor",
|
name: "5th (right-hand) index rotor",
|
||||||
type: "editableOption",
|
type: "editableOption",
|
||||||
value: I_ROTORS,
|
value: I_ROTORS,
|
||||||
defaultIndex: 0
|
defaultIndex: 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "5th index rotor intial value",
|
name: "5th index rotor intial value",
|
||||||
type: "option",
|
type: "option",
|
||||||
value: NUMBERS
|
value: NUMBERS
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "SIGABA mode",
|
name: "SIGABA mode",
|
||||||
type: "option",
|
type: "option",
|
||||||
value: ["Encrypt", "Decrypt"]
|
value: ["Encrypt", "Decrypt"]
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@param {string} rotor - rotor wirings
|
@param {string} input
|
||||||
|
@param {Object[]} args
|
||||||
@returns {string}
|
@returns {string}
|
||||||
*/
|
*/
|
||||||
|
run(input, args) {
|
||||||
parseRotorStr(rotor){
|
|
||||||
if (rotor === ""){
|
|
||||||
throw new OperationError(`All rotor wirings must be provided.`);
|
|
||||||
}
|
|
||||||
return rotor;
|
|
||||||
}
|
|
||||||
|
|
||||||
run(input, args){
|
|
||||||
const sigabaSwitch = args[40];
|
const sigabaSwitch = args[40];
|
||||||
const cipherRotors = [];
|
const cipherRotors = [];
|
||||||
const controlRotors = [];
|
const controlRotors = [];
|
||||||
const indexRotors = [];
|
const indexRotors = [];
|
||||||
for (let i=0; i<5; i++){
|
for (let i=0; i<5; i++) {
|
||||||
const rotorWiring = this.parseRotorStr(args[i*3]);
|
const rotorWiring = args[i*3];
|
||||||
cipherRotors.push(new CRRotor(rotorWiring, args[i*3+2], args[i*3+1]));
|
cipherRotors.push(new CRRotor(rotorWiring, args[i*3+2], args[i*3+1]));
|
||||||
}
|
}
|
||||||
for (let i=5; i<10; i++){
|
for (let i=5; i<10; i++) {
|
||||||
const rotorWiring = this.parseRotorStr(args[i*3]);
|
const rotorWiring = args[i*3];
|
||||||
controlRotors.push(new CRRotor(rotorWiring, args[i*3+2], args[i*3+1]));
|
controlRotors.push(new CRRotor(rotorWiring, args[i*3+2], args[i*3+1]));
|
||||||
}
|
}
|
||||||
for (let i=15; i<20; i++){
|
for (let i=15; i<20; i++) {
|
||||||
const rotorWiring = this.parseRotorStr(args[i*2]);
|
const rotorWiring = args[i*2];
|
||||||
indexRotors.push(new IRotor(rotorWiring, args[i*2+1]));
|
indexRotors.push(new IRotor(rotorWiring, args[i*2+1]));
|
||||||
}
|
}
|
||||||
const sigaba = new SigabaMachine(cipherRotors, controlRotors, indexRotors);
|
const sigaba = new SigabaMachine(cipherRotors, controlRotors, indexRotors);
|
||||||
var result;
|
let result;
|
||||||
if (sigabaSwitch === "Encrypt"){
|
if (sigabaSwitch === "Encrypt") {
|
||||||
result = sigaba.encrypt(input);
|
result = sigaba.encrypt(input);
|
||||||
}
|
} else if (sigabaSwitch === "Decrypt") {
|
||||||
else if (sigabaSwitch === "Decrypt") {
|
|
||||||
result = sigaba.decrypt(input);
|
result = sigaba.decrypt(input);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user