Comparing version 0.1.0 to 0.2.0
@@ -102,3 +102,4 @@ const _ = require('underscore'); | ||
// For M4, we need the tiny reflector wheels (b/c) | ||
const subType = this.options.type === 3 ? this.options.reflectorType : this.options.reflectorType.toLowerCase(); | ||
this.type = this.options.type; | ||
const subType = this.type === 3 ? this.options.reflectorType : this.options.reflectorType.toLowerCase(); | ||
const reflectorType = `UKW-${subType}`; | ||
@@ -201,9 +202,31 @@ | ||
* Encode an entire message character by character | ||
* @param rotors [optional] | ||
* @param msg | ||
*/ | ||
onMessage(msg) { | ||
onMessage(rotors, msg) { | ||
if (!msg) { | ||
msg = rotors; | ||
rotors = null; | ||
} | ||
if (!msg || msg.length < 1) { | ||
throw new EnigmaError('Invalid use of onMessage()'); | ||
throw new EnigmaError('Empty message passed to onMessage()'); | ||
} | ||
if (rotors) { | ||
if (rotors.length !== this.rotors.length) { | ||
throw new EnigmaError('Wrong number of rotors passed to onMessage()'); | ||
} | ||
if (!/^[A-Z]{3,4}$/.exec(rotors)) { | ||
throw new EnigmaError('Invalid rotors settings passed to onMessage()') | ||
} | ||
// Setup the rotor offsets | ||
rotors.split('').reverse().forEach((r, i) => this.rotors[i].setOffset(r)); | ||
} | ||
// Be forgiving: turn everything into uppercase, replace ' ' with X, | ||
// remove everything that is not a character A-Z. | ||
msg = msg.toUpperCase().replace(/\s/g, 'X').replace(/[^A-Z]/g, ''); | ||
// Simulate key presses for all characters in the message | ||
@@ -210,0 +233,0 @@ return msg.split('').map(x => this.onKey(x)).join(''); |
{ | ||
"name": "enigma-sim", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Simple Enigma M3/M4 simulator.", | ||
@@ -9,2 +9,6 @@ "main": "Enigma.js", | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Sandyman/enigma.git" | ||
}, | ||
"author": "Sander Huijsen <sander.huijsen@gmail.com>", | ||
@@ -11,0 +15,0 @@ "license": "ISC", |
@@ -5,3 +5,3 @@ # Enigma simulator | ||
I have focussed on functionality, so this simulator behaves as a proper Enigma M3/M4. This includes the "double stepping", which was probably unintended in the original device. | ||
I have focused on functionality, so this simulator behaves as a proper Enigma M3/M4. This includes the "double stepping", which was probably unintended in the original device. | ||
@@ -8,0 +8,0 @@ ### Example |
11
Rotor.js
@@ -120,4 +120,15 @@ const autoBind = require('auto-bind'); | ||
} | ||
/** | ||
* Set offset for this rotor | ||
* @param offset | ||
*/ | ||
setOffset(offset) { | ||
this.rotorOffset = _idx(offset); | ||
if (this.rotorOffset < 0) { | ||
this.rotorOffset = 0; | ||
} | ||
} | ||
} | ||
module.exports = Rotor; |
@@ -28,1 +28,4 @@ WETTERVORHERSAGE a | ||
"QB" | ||
SFBWDNJUSEGQOBHKRTAREEZMWKPPRBXOHDROEQGBBGTQVPGVKBVVGBIMHUSZYDAJQIROAXSSSNREHYGGRPISEZBOVMQIEMMZCYSGQDGRERVBILEKXYQIRGIRQNRDNVRXCYYTNJR |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
21571
10
450
0