-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
174 lines (163 loc) · 4.45 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import clear from 'clear';
import figlet from 'figlet';
import inquirer from 'inquirer';
import chalk from 'chalk';
import { Machine } from './components/Machine';
import { RotorSetting } from './components/Rotor';
clear();
console.log(chalk.bgGreen(figlet.textSync('ENIGMA', { font: 'Doom' })));
console.log(`
Welcome to Enigma Machine v0.3.1.
`);
inquirer
.prompt([
{
type: 'list',
name: 'rotor3type',
message: 'Select first Rotor (from left to right): ',
choices: ['I', 'II', 'III', 'IV', 'V'],
filter: function (val) {
return val.toLowerCase();
},
},
{
type: 'input',
name: 'rotor3ringSetting',
message: 'Select ring setting for first Rotor (1-26): ',
default: () => '1',
validate: (value) => {
if (/\d/.test(value) && value.length === 1) {
return true;
}
return 'Please enter only digits';
},
},
{
type: 'input',
name: 'rotor3position',
message: 'Select position for first Rotor (A-Z): ',
default: () => 'A',
validate: (value) => {
if (/[ABCDEFGHIJKLMNOPQRSTUVWXYZ]/.test(value) && value.length === 1) {
return true;
}
return 'Please enter only latin alphabet symbol';
},
},
{
type: 'list',
name: 'rotor2type',
message: 'Select second Rotor (from left to right)',
choices: ['I', 'II', 'III', 'IV', 'V'],
filter: function (val) {
return val.toLowerCase();
},
},
{
type: 'input',
name: 'rotor2ringSetting',
message: 'Select ring setting for second Rotor (1-26): ',
default: () => '1',
validate: (value) => {
if (/\d/.test(value) && value.length === 1) {
return true;
}
return 'Please enter only digits';
},
},
{
type: 'input',
name: 'rotor2position',
message: 'Select position for second Rotor (A-Z): ',
default: () => 'A',
validate: (value) => {
if (/[ABCDEFGHIJKLMNOPQRSTUVWXYZ]/.test(value) && value.length === 1) {
return true;
}
return 'Please enter only latin alphabet symbol';
},
},
{
type: 'list',
name: 'rotor1type',
message: 'Select third Rotor (from left to right)',
choices: ['I', 'II', 'III', 'IV', 'V'],
filter: function (val) {
return val.toLowerCase();
},
},
{
type: 'input',
name: 'rotor1ringSetting',
message: 'Select ring setting for third Rotor (1-26): ',
default: () => '1',
validate: (value) => {
if (/\d/.test(value) && value.length === 1) {
return true;
}
return 'Please enter only digits';
},
},
{
type: 'input',
name: 'rotor1position',
message: 'Select position for third Rotor (A-Z): ',
default: () => 'A',
validate: (value) => {
if (/[ABCDEFGHIJKLMNOPQRSTUVWXYZ]/.test(value) && value.length === 1) {
return true;
}
return 'Please enter only latin alphabet symbol';
},
},
{
type: 'list',
name: 'reflector',
message: 'Select reflector: ',
choices: ['B', 'C'],
},
{
type: 'input',
name: 'plugboard',
message: "Enter plugboard setting (e.g. 'AB CD EF'): ",
default: () => '',
},
{
type: 'input',
name: 'text',
message: 'Enter you message for encrypt: ',
},
])
.then((answers) => {
const machineSetting = {
reflectorType: answers.reflector,
rotorSettings: [
{
type: answers.rotor1type.toUpperCase(),
position: answers.rotor1position,
ringSetting: answers.rotor1ringSetting,
} as RotorSetting,
{
type: answers.rotor2type.toUpperCase(),
position: answers.rotor2position,
ringSetting: answers.rotor2ringSetting,
} as RotorSetting,
{
type: answers.rotor3type.toUpperCase(),
position: answers.rotor3position,
ringSetting: answers.rotor3ringSetting,
} as RotorSetting,
],
debug: false,
};
const enigma = new Machine(machineSetting);
console.log(chalk.red('Encrypted message: '));
console.log(chalk.green(enigma.encryptMessage(answers.text)));
})
.catch((error) => {
if (error.isTtyError) {
// Prompt couldn't be rendered in the current environment
} else {
// Something else went wrong
}
});