ibraimgm / enigma

A simple enigma machine in Go
MIT License
8 stars 2 forks source link
go golang learning learning-by-doing learning-go learning-golang

Enigma

Build Status codecov Go Report Card Enigma Docs Parts Docs

Basic information

Description

A simple enigma machine in Go. This is just a toy project to play a bit more with Go and it's tools, dependency management, and all that.

The main objective is to emulate a "generic" M3/M4 Enigma (the most known/used model in the War), in the command line.

What is a "Enigma Machine"

According to Wikipedia,

The Enigma machines were a series of electro-mechanical rotor cipher machines developed and used in the early- to mid-20th century to protect commercial, diplomatic and military communication. Enigma was invented by the German engineer Arthur Scherbius at the end of World War I. Early models were used commercially from the early 1920s, and adopted by military and government services of several countries, most notably Nazi Germany before and during World War II. Several different Enigma models were produced, but the German military models, having a plugboard, were the most complex. Japanese and Italian models were also in use.

It was, basically, a machine used to encode military messages on WWII. It has an interesting history and emultating it serves as a fun programming exercise.

How does it work

Some people belive that an image is worth a thousand words. If that is the case, the followin diagram (stolen from here) sums up pretty well how such a machine works:

Enigma wiring diagram

Basically, the input travels from the keyboard to the plugboard and then, to each rotor, ultil it reaches the reflector (red arrow). After that, the signal bounces on the opposite path, until it reaches the lightboard (blue arrow). Not that complicated, right?

Usage

Command-line application

Simply running the executable is enough to start the application with a default set of options (use --help) to see all the flags available. By default, the coded text is written to STDOUT after every newline. You can change this behavior using the -o flag, e. g. enigma -o coded.txt.

You can also encode an entire file in one go by piping it to the application: cat plain.txt | enigma -q > coded.txt.

API

There are basically two ways to use the API. The first one, in the package enigma exports an easy-to-use built-int enigma machine, with configurable rotors, ring settings and window settings. It is also possible to use the Assemble funcion to specify every individual part of the machine.

The second package, parts, contains the interfaces for every machine part used by the enigma, with default implementations as well.

Caveats

This implementation is a bit more 'flexible' than the actual enigma hardware. For example, you can use the same rotor more than once all rotors are valid in all positions, etc. This is intentional to make the API and machine construction as flexible as possible.

References

These are the main references/documentation used in this project:

License

MIT. Take a look at the LICENSE file.