Created
September 14, 2025 08:37
-
-
Save CornerSyrup/8eac3884bc29a199cde2700fd55b5256 to your computer and use it in GitHub Desktop.
An implementation of Enigma in TypeScript with my weakest programming skill, AI co-autor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const base = 97; | |
| const ALPHABET_LENGTH = 26; | |
| const Rotor = | |
| (config: number) => | |
| (plain: string): string => { | |
| if (plain < "a" || plain > "z") { | |
| return plain; | |
| } | |
| return String.fromCharCode(((plain.charCodeAt(0) - base + config) % ALPHABET_LENGTH) + base); | |
| }; | |
| const Reflector = (plain: string): string => { | |
| if (plain < "a" || plain > "z") { | |
| return plain; | |
| } | |
| return String.fromCharCode(ALPHABET_LENGTH - 1 - (plain.charCodeAt(0) - base) + base); | |
| }; | |
| export const Enigma = (configs: number[]) => { | |
| const rotors = configs.map(Rotor); | |
| const reverseRotors = [...rotors].reverse(); | |
| return (plain: string): string => { | |
| return plain | |
| .split("") | |
| .map((c) => rotors.reduce((t, rotor) => rotor(t), c)) | |
| .map(Reflector) | |
| .map((c) => reverseRotors.reduce((t, rotor) => rotor(t), c)) | |
| .join(""); | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment