-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.ts
63 lines (49 loc) · 1.48 KB
/
browser.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
import "./style.css";
import Alpine from "alpinejs";
import enigma from "./machine";
// Number of letters
const size = 26;
// Unicode value of letter A
const startPos = 65;
// Operation mode
const MODE = {
ENCRYPT: "0", // encryption
DECRYPT: "1", // decryption
};
// define app state
Alpine.data("app", () => ({
positions: [0, 0, 0],
mode: MODE.ENCRYPT,
// qwerty keyboard layout
keys: ["QWERTYUIOP".split(""), "ASDFGHJKL".split(""), "ZXCVBNM".split("")],
key: null,
input: "",
output: "",
init() {
/**
* Transform the input whenever it changes
*/
this.$watch("input", (n: string) => {
enigma.setRotorPositions(this.positions);
let result: string;
if (this.mode == MODE.ENCRYPT) {
result = enigma.decrypt(n);
} else {
result = enigma.encrypt(n);
}
this.key = result.substring(this.output.length);
setTimeout(() => (this.key = null), 500);
this.output = result;
});
},
// function to modify a specific rotor position
increment(index: number, value: number, step: number) {
this.positions[index] = (size + value + step) % size;
this.input = "";
},
// convert a unicode number into a character
toChar(position: number) {
return String.fromCharCode(startPos + ((size + position) % size));
},
}));
window.queueMicrotask(() => Alpine.start());