UNPKG

enigma

Version:

Implementation of an enigma machine

1 lines 6.03 kB
!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var e;"undefined"!=typeof window?e=window:"undefined"!=typeof global?e=global:"undefined"!=typeof self&&(e=self),e.enigma=t()}}(function(){return function t(e,i,n){function r(s,h){if(!i[s]){if(!e[s]){var a="function"==typeof require&&require;if(!h&&a)return a(s,!0);if(o)return o(s,!0);var p=new Error("Cannot find module '"+s+"'");throw p.code="MODULE_NOT_FOUND",p}var l=i[s]={exports:{}};e[s][0].call(l.exports,function(t){var i=e[s][1][t];return r(i?i:t)},l,l.exports,t,e,i,n)}return i[s].exports}for(var o="function"==typeof require&&require,s=0;s<n.length;s++)r(n[s]);return r}({1:[function(t,e){var i=t("./lib/enigma"),n=t("./lib/rotor"),r=t("./lib/umkehrwalze"),o=t("./lib/eintrittswalze"),s=t("./lib/steckerbrett");e.exports={Enigma:i,Rotor:n,Umkehrwalze:r,Reflector:r,Eintrittswalze:o,EntryWheel:o,Steckerbrett:s,Plugboard:s}},{"./lib/eintrittswalze":2,"./lib/enigma":3,"./lib/rotor":4,"./lib/steckerbrett":5,"./lib/umkehrwalze":6}],2:[function(t,e){function i(t,e){return this.setWiring(t),this.setName(e),this}var n=t("./walze");e.exports=i,i.prototype=new n},{"./walze":7}],3:[function(t,e){function i(t,e,i,n){if(this.alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""),t.length<3||t.length>4||"undefined"==typeof e||"undefined"==typeof i||"undefined"==typeof n)throw new Error("Only three or four rotors are supported, and reflector, plugboard and entry wheel must be set");return this.rotors=t,this.ukw=e,this.steckerbrett=i,this.etw=n,this}e.exports=i,i.prototype.signal=function(t){this.rotate(),t=this.steckerbrett.signal(t),t=this.etw.signal(t);for(var e=this.rotors.length-1;e>=0;e--)t=this.rotors[e].signal(t);t=this.ukw.signal(t);for(var i=0;i<this.rotors.length;i++)t=this.rotors[i].signal(t,!0);return t=this.etw.signal(t,!0),t=this.steckerbrett.signal(t,!0)},i.prototype.rotate=function(){for(var t=[],e=[],i=0;i<this.rotors.length;i++)e[i]=!1,t.push(this.rotors[i].inNotch());for(var n=this.rotors.length-1;n>=0;n--)(n===this.rotors.length-1||e[n+1]&&t[n+1]||e[n+1]&&t[n]&&n>0)&&(this.rotors[n].rotate(),e[n]=!0)},i.prototype.string=function(t){t="string"==typeof t?t:t.toString(),t=t.toUpperCase().replace(new RegExp("[^"+this.etw.wiring+"]","g"),"").split("");for(var e="",i=0;i<t.length;i++)e+=this.signal(t[i]);return e},i.prototype.getPositions=function(){for(var t="",e=0;e<this.rotors.length;e++)t+=this.rotors[e].getPosition();return t},i.prototype.setPositions=function(t){t=t.split("");for(var e=0;e<this.rotors.length;e++)this.rotors[e].setPosition(t[e])},i.prototype.getRingSettings=function(){for(var t="",e=0;e<this.rotors.length;e++)t+=this.rotors[e].getRingSetting();return t},i.prototype.setRingSettings=function(t){t=t.split("");for(var e=0;e<this.rotors.length;e++)this.rotors[e].setRingSetting(t[e])}},{}],4:[function(t,e){function i(t,e,i){return this.position=0,this.ringsetting=0,this.setWiring(t),this.setNotches(e),this.setName(i),this}var n=t("./walze");e.exports=i,i.prototype=new n,i.prototype.signal=function(t,e){var i;return i=this.alphabet.indexOf(t),i=(i+this.position+this.alphabet.length-this.ringsetting)%this.alphabet.length,t=this.alphabet[i],e&&(i=this.wiring.indexOf(t)),t=e?this.alphabet[i]:this.wiring[i],i=this.alphabet.indexOf(t),i=(i+(this.alphabet.length-this.position+this.ringsetting))%this.alphabet.length,t=this.alphabet[i]},i.prototype.rotate=function(){this.position=(this.position+1)%this.alphabet.length},i.prototype.inNotch=function(){for(var t=!1,e=0;e<this.notches.length;e++)if(this.position===this.alphabet.indexOf(this.notches[e])){t=!0;break}return t},i.prototype.setNotches=function(t){if(t="string"==typeof t?t.toUpperCase().split(""):"object"==typeof t&&t instanceof Array?t.map(function(t){return t.toString().toUpperCase()}):[],t.some(function(t,e,i){return i.indexOf(t)!==i.lastIndexOf(t)}))throw new Error("notches has to consist of unique characters");if(t.length>this.alphabet.length)throw new Error("you can’t have more notches than characters on the rotor");this.notches=t},i.prototype.getPosition=function(){return this.alphabet[this.position]},i.prototype.setPosition=function(t){this.position=this.alphabet.indexOf(t)},i.prototype.getRingSetting=function(){return this.alphabet[this.ringsetting]},i.prototype.setRingSetting=function(t){this.ringsetting=this.alphabet.indexOf(t)}},{"./walze":7}],5:[function(t,e){function i(t,e){return this.setName(e),this.setPlugs(t),this}var n=t("./walze");e.exports=i,i.prototype=new n,i.prototype.setPlugs=function(t){t="string"==typeof t?t.toUpperCase().split(" "):"object"==typeof t&&t instanceof Array?t.map(function(t){return t.toString().toUpperCase()}):[];for(var e=this.alphabet.slice(),i=0;i<t.length;i++){var n=t[i].split("");e[this.alphabet.indexOf(n[0])]=n[1],e[this.alphabet.indexOf(n[1])]=n[0]}if(e.length>this.alphabet.length||t.length>Math.floor(this.alphabet.length/2))throw new Error("The Steckerbrett only allows for "+Math.floor(this.alphabet.length/2)+" connections to be set");this.setWiring(e)}},{"./walze":7}],6:[function(t,e){function i(t,e){return this.setWiring(t),this.setName(e),this}var n=t("./walze");e.exports=i,i.prototype=new n},{"./walze":7}],7:[function(t,e){function i(t,e){return this.setWiring(t),this.setName(e),this}e.exports=i,i.prototype.alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""),i.prototype.setWiring=function(t){if(t="string"==typeof t?t.toUpperCase().split(""):"object"==typeof t&&t instanceof Array?t.map(function(t){return t.toString().toUpperCase()}):this.alphabet.slice(),t.some(function(t,e,i){return i.indexOf(t)!==i.lastIndexOf(t)})||t.length!==this.alphabet.length)throw new Error("The Walze must have "+this.alphabet.length+" unique characters.");this.wiring=t},i.prototype.setName=function(t){this.name="string"==typeof t?t:""},i.prototype.signal=function(t,e){var i=e?this.wiring.indexOf(t):this.alphabet.indexOf(t);return t=e?this.alphabet[i]:this.wiring[i]}},{}]},{},[1])(1)});