-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
211 lines (187 loc) · 5.34 KB
/
sketch.js
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
class Key {
constructor(idx,layout,r=15) {
let Layoutletters; let keysPerRow;
if (layout == 'original') {
Layoutletters = 'QWERTZUIOASDFGHJKPYXCVBNML'
keysPerRow = [9, 8, 9];
}
else {
Layoutletters = 'QWERTYUIOPASDFGHJKLZXCVBNM'
keysPerRow = [10, 9, 7];
}
this.label = Layoutletters[idx]
this.r = r;
let row; let col;
if (idx < keysPerRow[0]) {
row = 0;
col = idx;
} else if ( idx < keysPerRow[0] + keysPerRow[1]) {
row = 1;
col = idx - keysPerRow[0];
} else {
row = 2;
col = idx - ( keysPerRow[0] + keysPerRow[1] );
}
this.yRelative = (2*row + 1)*this.r;
if ( (layout == 'original' ) && ( row == 2) ) {
this.xRelative = (2*col + 1)*this.r;
}
else {
this.xRelative = (2*col + row + 1)*this.r;
}
}
draw(x0,y0) {
fill('white')
ellipse(this.xRelative + x0, this.yRelative +y0, 2*this.r, 2*this.r)
textSize(this.r)
textAlign(CENTER, CENTER)
fill('black')
text(this.label, this.xRelative + x0,this.yRelative + y0)
}
}
class Keyboard {
constructor(x0, y0, layout = 'modern') {
this.xpos = x0;
this.ypos = y0;
this.key = new Array(26);
this.key[25] = 0;
var key;
for (let k=0; k<26; k++) {
key = new Key(k, layout);
this.key[letter2num(key.label)] = key;
}
}
draw() {
for (let k=0; k<26; k++) {
this.key[k].draw(this.xpos,this.ypos);
}
}
highlightKey(n) {
this.draw();
let x, y, r;
x = this.key[n].xRelative + this.xpos;
y = this.key[n].yRelative + this.ypos;
r = this.key[n].r;
fill(10,150,10,150)
ellipse(x,y,2*r,2*r);
noFill();
}
}
class Rotor {
constructor(cipher,notchPos) {
this.cipher = cipher;
this.notch = notchPos;
}
fwd(n,state= 0 ) { // forward permutation
let cipher = rotateCipher(this.cipher,state);
return letter2num(cipher[n]);
}
bwd(n, state = 0) { // backward permutation
let cipher = rotateCipher(this.cipher,state)
cipher = reverseCipher(cipher);
return letter2num(cipher[n]);
}
}
class RotorArray {
// The reflector works like a symmetric rotor, and should be the last (usually 4th) Rotor in the list
constructor(listOfRotors, initialState) {
this.rotors = listOfRotors;
this.state = initialState;
}
click() {
// advance rotor state
let k = 0;
this.state[0] = (this.state[0] + 1) % 26;
while ( (this.state[k] == this.rotors[k].notch) && (k < this.state.length - 1) ) {
this.state[k+1] = (this.state[k+1] + 1) % 26
k++;
}
}
}
class Plugboard {
constructor(cipher) {
this.cipher = cipher;
if ( cipher != reverseCipher(cipher) ) {
console.warn('A non-symmetric cipher was provided for the plugboard')
}
}
swap(n) { // swap letters if they are connected
return letter2num(this.cipher[n]);
}
}
var rotorCiphers = ['EKMFLGDQVZNTOWYHXUSPAIBRCJ', 'AJDKSIRUXBLHWTMCQGZNPYFVOE',
'BDFHJLCPRTXVZNYEIWGAKMUSQO', 'ESOVPZJAYQUIRHXLNFTGKDCMWB', 'VZBRGITYUPSDNHLXAWMJQOFECK'];
var rotorNotches = [17, 5, 22, 10, 0];
var reflectorCiphers = ['EJMZALYXVBWFCRQUONTSPIKHGD','YRUHQSLDPXNGOKMIEBFZCWVJAT',
'FVPJIAOYEDRZXWGCTKUQSBNMHL'];
var plugboard = new Plugboard(reflectorCiphers[0]);
var rotor0 = new Rotor(rotorCiphers[0],rotorNotches[0]);
var rotor1 = new Rotor(rotorCiphers[1],rotorNotches[1]);
var rotor2 = new Rotor(rotorCiphers[2],rotorNotches[2]);
var reflector = new Rotor(reflectorCiphers[1],'');
var rotorList = [rotor0, rotor1, rotor2, reflector];
var rotorArray = new RotorArray(rotorList,[0, 0, 0]);
var keyboard = new Keyboard(100,150,'original');
function setup() {
createCanvas(470,470)
background(51)
keyboard.draw();
}
function draw() {
// noLoop();
}
function decode(n,plugboard,rotorArray) {
// keystroke goes through plugboard
let out = plugboard.swap(n);
// then traverses the rotor array wiring
for (let k=0; k<rotorArray.state.length; k++) {
out = rotorArray.rotors[k].fwd(out,rotorArray.state[k]);
}
// bounces back through the reflector (bwd and fwd methods should give the same result)
out = rotorArray.rotors[rotorArray.state.length].fwd(out);
// traversers rotor array in reverse
for (let k = rotorArray.state.length - 1; k>=0; k--) {
out = rotorArray.rotors[k].bwd(out,rotorArray.state[k]);
}
// finally thorugh the plugboard again
out = plugboard.swap(out);
// advances rotor array state
rotorArray.click();
return out;
}
function keyPressed() {
let n = parseInt(key,36) - 10;
if ( (n>=0) && (n<=25) ) {
n = decode(n,plugboard,rotorArray);
console.log(num2letter(n));
keyboard.highlightKey(n);
}
}
function letter2num(letter) {
return parseInt(letter,36) - 10;
}
function num2letter(n) {
letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
return letters[n]
}
function reverseCipher(cipher) {
let letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
let cipherR = new Array(26);
for (let k = 0; k < 26; k++) {
cipherR[letter2num(cipher[k])] = letters[k];
}
return cipherR.reduce( (x,y) => x + y );
}
function rotateCipher(cipher,state) {
let wiring = new Array(52);
let out = [];
for (let k=0; k<26; k++) {
wiring[k] = (parseInt(cipher[k],36) - k + 26 - 10) % 26;
wiring[k+26] = wiring[k];
out.push(k);
}
for (let k=0; k<26; k++) {
out[k] = (out[k] + wiring[state+k]) % 26;
}
return out.map(num2letter).reduce( (x,y) => x+y );
}