@ondoher/enigma 1.0.2 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +75 -48
- package/dist/index.d.ts +899 -0
- package/jsconfig.json +7 -1
- package/lib/enigma/Encoder.js +154 -12
- package/lib/enigma/Enigma.js +45 -59
- package/lib/enigma/EnigmaTypes.d.ts +115 -18
- package/lib/enigma/EntryDisc.js +2 -8
- package/lib/enigma/PlugBoard.js +4 -9
- package/lib/enigma/Reflector.js +5 -9
- package/lib/enigma/Rotor.js +43 -36
- package/lib/enigma/index.js +1 -1
- package/lib/enigma/tests/EnigmaSpec.js +21 -15
- package/lib/enigma/tests/RotorData.js +1 -1
- package/lib/enigma/tests/RotorSpec.js +3 -1
- package/lib/generator/CodeBook.js +3 -3
- package/lib/generator/Generator.js +6 -5
- package/lib/generator/GeneratorTypes.d.ts +2 -1
- package/lib/utils/Random.js +1 -1
- package/package.json +8 -2
- package/scripts/EnigmaData.js +236 -0
- package/scripts/hamlet.html +8880 -0
- package/scripts/make-validated-data.js +4 -0
- package/scripts/parseHamlet.js +32 -0
- package/scripts/test-messages.js +60 -0
- package/scripts/test.js +118 -0
- package/scripts/x +6446 -0
- package/tsconfig.json +19 -0
- package/types/enigma/Encoder.d.ts +128 -0
- package/types/enigma/Enigma.d.ts +88 -0
- package/types/enigma/EntryDisc.d.ts +17 -0
- package/types/enigma/Inventory.d.ts +91 -0
- package/types/enigma/PlugBoard.d.ts +26 -0
- package/types/enigma/Reflector.d.ts +14 -0
- package/types/enigma/Rotor.d.ts +59 -0
- package/types/enigma/consts.d.ts +1 -0
- package/types/enigma/index.d.ts +5 -0
- package/types/enigma/standardInventory.d.ts +71 -0
- package/types/enigma/tests/EnigmaData.d.ts +46 -0
- package/types/enigma/tests/EnigmaSpec.d.ts +1 -0
- package/types/enigma/tests/PlugBoardData.d.ts +4 -0
- package/types/enigma/tests/PlugBoardSpec.d.ts +1 -0
- package/types/enigma/tests/RotorData.d.ts +15 -0
- package/types/enigma/tests/RotorSpec.d.ts +1 -0
- package/types/generator/CodeBook.d.ts +82 -0
- package/types/generator/Generator.d.ts +67 -0
- package/types/generator/hamlet.d.ts +2 -0
- package/types/generator/index.d.ts +3 -0
- package/types/index.d.ts +899 -0
- package/types/utils/Random.d.ts +131 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {readFile, writeFile} from 'node:fs/promises';
|
|
2
|
+
|
|
3
|
+
var hamletHtml = await readFile('./hamlet.html', 'utf-8');
|
|
4
|
+
var blockRE = /<blockquote>([^]*?)<\/blockquote\>/gm
|
|
5
|
+
//var blockRE = /blockquote/g
|
|
6
|
+
var blockQuotes = hamletHtml.matchAll(blockRE);
|
|
7
|
+
var bqArray = [...blockQuotes];
|
|
8
|
+
|
|
9
|
+
var text = '';
|
|
10
|
+
|
|
11
|
+
bqArray.forEach(function(match) {
|
|
12
|
+
var searchText = match[1];
|
|
13
|
+
var textRE = /<A NAME.*?>([^]*?)<\/A/g
|
|
14
|
+
var results = searchText.matchAll(textRE);
|
|
15
|
+
var resultsArray = [...results];
|
|
16
|
+
|
|
17
|
+
text = resultsArray.reduce(function(text, match) {
|
|
18
|
+
text += ' ' + match[1];
|
|
19
|
+
return text;
|
|
20
|
+
}, text);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
var sentenceRE = /([^]*?[?.]) *?/g
|
|
24
|
+
var sentencesMatch = text.matchAll(sentenceRE);
|
|
25
|
+
var sentenceArray = [...sentencesMatch];
|
|
26
|
+
var sentences = sentenceArray.map(function(match) {
|
|
27
|
+
return match[1].trim();
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
await writeFile('./hamlet.json', JSON.stringify(sentences, null, ' '), 'utf-8')
|
|
31
|
+
|
|
32
|
+
//console.log(bqArray);
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { readFile } from "fs/promises";
|
|
2
|
+
import Enigma from "./test.js";
|
|
3
|
+
import { alphabet } from "./Inventory.js";
|
|
4
|
+
|
|
5
|
+
var enigma = new Enigma('I', 'B', alphabet);
|
|
6
|
+
var sheet = JSON.parse(await readFile('./keysheet.json', 'utf-8'));
|
|
7
|
+
var messages = JSON.parse(await readFile('./messages.json', 'utf-8'));
|
|
8
|
+
|
|
9
|
+
function findDay(sheet, indicator) {
|
|
10
|
+
return sheet.find(function (day) {
|
|
11
|
+
return day.indicators.includes(indicator);
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function clean(text) {
|
|
16
|
+
text = text.replace(/ZIZ/g, ',');
|
|
17
|
+
text = text.replace(/YIY/g, '.');
|
|
18
|
+
text = text.replace(/XIX/g, '?');
|
|
19
|
+
text = text.replace(/WIW/g, ' ');
|
|
20
|
+
text = text.replace(/VIV/g, '\'');
|
|
21
|
+
text = text.replace(/UIU/g, '"');
|
|
22
|
+
text = text.replace(/NULL/g, '0');
|
|
23
|
+
text = text.replace(/ONE/g, '1');
|
|
24
|
+
text = text.replace(/TWO/g, '2');
|
|
25
|
+
text = text.replace(/THREE/g, '3');
|
|
26
|
+
text = text.replace(/FOUR/g, '4');
|
|
27
|
+
text = text.replace(/FIVE/g, '5');
|
|
28
|
+
text = text.replace(/SIX/g, '6');
|
|
29
|
+
text = text.replace(/SEVEN/g, '7');
|
|
30
|
+
text = text.replace(/EIGHT/g, '8');
|
|
31
|
+
text = text.replace(/NINE/g, '9');
|
|
32
|
+
|
|
33
|
+
return text;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function parsePart(sheet, part) {
|
|
37
|
+
var indicator = part.text.slice(2, 5);
|
|
38
|
+
indicator = indicator.toLowerCase();
|
|
39
|
+
var day = findDay(sheet, indicator);
|
|
40
|
+
var text = part.text.slice(6);
|
|
41
|
+
text = text.replace(/ /g, '');
|
|
42
|
+
|
|
43
|
+
enigma.configure(day.plugs, day.rotors, day.ringSettings);
|
|
44
|
+
var start = enigma.encode(part.key, part.enc);
|
|
45
|
+
text = enigma.encode(start, text);
|
|
46
|
+
|
|
47
|
+
return text.trim();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function parseMessage(sheet, message) {
|
|
51
|
+
return message.map(function(part) {
|
|
52
|
+
return parsePart(sheet, part)
|
|
53
|
+
}).join('');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
messages.forEach(function(message, idx) {
|
|
57
|
+
console.log('\nMESSAGE', idx, '-------------------------')
|
|
58
|
+
console.log(clean(parseMessage(sheet, message)))
|
|
59
|
+
})
|
|
60
|
+
parseMessage(sheet, messages[0])
|
package/scripts/test.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import '../lib/enigma/standardInventory.js'
|
|
2
|
+
import Enigma from "../lib/enigma/Enigma.js";
|
|
3
|
+
import Generator from '../lib/generator/Generator.js';
|
|
4
|
+
import CodeBook from '../lib/generator/CodeBook.js';
|
|
5
|
+
import { writeFile } from "fs/promises";
|
|
6
|
+
import { enigmaData } from './EnigmaData.js';
|
|
7
|
+
|
|
8
|
+
const WRITE = false;
|
|
9
|
+
const GENERATE_MESSAGES = false;
|
|
10
|
+
const GENERATE_CODEBOOK = false;
|
|
11
|
+
const SHOW_EVENTS = true;
|
|
12
|
+
const EVENT_TYPES = ["translate", "input", "output", "step", "double-step"];
|
|
13
|
+
const EVENT_MESSAGE = enigmaData.sampleFieldMessages[0];
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
const TEST_MESSAGE_FILE = './test-messages.json';
|
|
17
|
+
const TEST_CODEBOOK_FILE = './test-codebook.json';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
*
|
|
21
|
+
* @param {string} model
|
|
22
|
+
* @param {number} count
|
|
23
|
+
* @param {GeneratedMessage[]} list
|
|
24
|
+
*/
|
|
25
|
+
function generateForModel(model, count, list) {
|
|
26
|
+
var generator = new Generator();
|
|
27
|
+
|
|
28
|
+
let {reflectors, rotors, fixed} = generator.getModelOptions(model);
|
|
29
|
+
let enigma = generator.createRandomEnigma(model, reflectors)
|
|
30
|
+
|
|
31
|
+
for (let idx = 0; idx < count; idx++) {
|
|
32
|
+
let configuration = generator.generateEnigmaConfiguration({rotors, fixed});
|
|
33
|
+
enigma.configure(configuration);
|
|
34
|
+
|
|
35
|
+
let message = generator.generateMessage(enigma);
|
|
36
|
+
|
|
37
|
+
list.push({model, ...message});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
*
|
|
43
|
+
* @param {number} count
|
|
44
|
+
*/
|
|
45
|
+
async function generateMessages(count) {
|
|
46
|
+
/** @type {object[]} */
|
|
47
|
+
let messages = [];
|
|
48
|
+
|
|
49
|
+
generateForModel('I', count, messages);
|
|
50
|
+
generateForModel('M3', count, messages);
|
|
51
|
+
generateForModel('M4', count, messages);
|
|
52
|
+
|
|
53
|
+
let output = JSON.stringify(messages, null, ' ');
|
|
54
|
+
if (WRITE) {
|
|
55
|
+
await writeFile(TEST_MESSAGE_FILE, output, 'utf-8');
|
|
56
|
+
} else {
|
|
57
|
+
console.log(output);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
*
|
|
63
|
+
* @param {number} count
|
|
64
|
+
*/
|
|
65
|
+
async function codebook(count) {
|
|
66
|
+
var generator = new Generator();
|
|
67
|
+
|
|
68
|
+
let {reflectors} = generator.getModelOptions("I");
|
|
69
|
+
let enigma = generator.createRandomEnigma("I", reflectors)
|
|
70
|
+
let codeBook = new CodeBook(enigma);
|
|
71
|
+
|
|
72
|
+
let keySheet = codeBook.generateKeySheet(30);
|
|
73
|
+
|
|
74
|
+
let messages = [];
|
|
75
|
+
|
|
76
|
+
for (let idx = 0; idx < count; idx++) {
|
|
77
|
+
let message = codeBook.generateMessage(keySheet);
|
|
78
|
+
|
|
79
|
+
messages.push(message);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let output = JSON.stringify({keySheet, messages}, null, ' ');
|
|
83
|
+
|
|
84
|
+
if (WRITE) {
|
|
85
|
+
await writeFile(TEST_CODEBOOK_FILE, output, 'utf-8');
|
|
86
|
+
} else {
|
|
87
|
+
console.log(output);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function showEvents() {
|
|
92
|
+
let enigma = new Enigma(EVENT_MESSAGE.model, {reflector: EVENT_MESSAGE.setup.reflector});
|
|
93
|
+
enigma.configure({plugs: EVENT_MESSAGE.setup.plugs, rotors: EVENT_MESSAGE.setup.rotors, ringSettings: EVENT_MESSAGE.setup.ringSettings});
|
|
94
|
+
|
|
95
|
+
enigma.listen('showEvents', (event, name, data) => {
|
|
96
|
+
if (EVENT_TYPES.includes(event)) {
|
|
97
|
+
console.log(data.description);
|
|
98
|
+
}
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
let encoded = enigma.encode(EVENT_MESSAGE.message.key, EVENT_MESSAGE.message.decoded);
|
|
102
|
+
|
|
103
|
+
console.log(encoded);
|
|
104
|
+
console.log(EVENT_MESSAGE.message.encoded);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
if (GENERATE_MESSAGES) {
|
|
109
|
+
await generateMessages(20);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (GENERATE_CODEBOOK) {
|
|
113
|
+
await codebook(20);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (SHOW_EVENTS) {
|
|
117
|
+
showEvents();
|
|
118
|
+
}
|