pub struct Enigma<R, P, O, const ROTOR_NUM: usize = DEFAULT_ROTOR_NUM> { /* private fields */ }
Expand description
Enigma Finite State Machine
An incomplete instance (e.g. from Enigma::default
) can be initialised later
semi-dynamically with the compiler checking correct initialisation before use.
§Examples
let enigma = Enigma::default()
.set_rotors(Wheel::new([ROTOR_I, ROTOR_II, ROTOR_III]))
.set_plugboard(Plugboard::new(&[]));
let mut enigma = enigma.set_reflector(REFLECTOR_A);
enigma.encrypt_letter(Letter::A);
The compiler can detect if one of the required initialisation steps is missing:
let mut enigma = Enigma::default()
.set_rotors(Wheel::new([ROTOR_I, ROTOR_II, ROTOR_III]))
.set_plugboard(Plugboard::new(&[]));
enigma.encrypt_letter(Letter::A); // encrypt can't be called until a reflector is set
Implementations§
Source§impl Enigma<Unset, Unset, Unset>
impl Enigma<Unset, Unset, Unset>
Sourcepub fn new<const ROTOR_NUM: usize>(
config: &EnigmaSettings<ROTOR_NUM>,
) -> Enigma<Wheel<ROTOR_NUM>, Plugboard, Reflector, ROTOR_NUM>
pub fn new<const ROTOR_NUM: usize>( config: &EnigmaSettings<ROTOR_NUM>, ) -> Enigma<Wheel<ROTOR_NUM>, Plugboard, Reflector, ROTOR_NUM>
Create a fully setup Enigma FSM from the provided settings
§Examples
let mut enigma = Enigma::new(&EnigmaSettings {
rotors: [ROTOR_II, ROTOR_IV, ROTOR_V],
plugboard: Plugboard::from_str(
"AV,BS,CG,DL,FU,HZ,IN,KM,OW,RX",
).unwrap(),
reflector: REFLECTOR_B,
ringstellung: [2, 21, 12],
grundstellung: [Letter::B, Letter::L, Letter::A],
});
enigma.encrypt_letter(Letter::A);
Rust will attempt to determine whether to create an M3 or M4 (or another variant) based on the size of the rotor array. However, in some cases, it may be necessary to specify this explicitly:
let settings = EnigmaSettings {
rotors: [ROTOR_II, ROTOR_IV, ROTOR_V, ROTOR_III],
plugboard: Plugboard::from_str(
"AV,BS,CG,DL,FU,HZ,IN,KM,OW,RX",
).unwrap(),
reflector: REFLECTOR_B_THIN,
ringstellung: [2, 21, 12, 3],
grundstellung: [Letter::B, Letter::L, Letter::A, Letter::B],
};
let mut enigma = Enigma::new::<M4>(&settings);
enigma.encrypt_letter(Letter::A);
As alluded to above this also lets you create instances of machines that never existed.
For example, a 10-rotor enigma machine can be created with Enigma::new<10>(&settings)
Source§impl<AnyRotor, AnyPlugboard, AnyReflector, const ROTOR_NUM: usize> Enigma<AnyRotor, AnyPlugboard, AnyReflector, ROTOR_NUM>
impl<AnyRotor, AnyPlugboard, AnyReflector, const ROTOR_NUM: usize> Enigma<AnyRotor, AnyPlugboard, AnyReflector, ROTOR_NUM>
Sourcepub fn set_rotors(
self,
rotors: Wheel,
) -> Enigma<Wheel, AnyPlugboard, AnyReflector, ROTOR_NUM>
pub fn set_rotors( self, rotors: Wheel, ) -> Enigma<Wheel, AnyPlugboard, AnyReflector, ROTOR_NUM>
Set the Enigma rotors
Sourcepub fn set_plugboard(
self,
plugboard: Plugboard,
) -> Enigma<AnyRotor, Plugboard, AnyReflector, ROTOR_NUM>
pub fn set_plugboard( self, plugboard: Plugboard, ) -> Enigma<AnyRotor, Plugboard, AnyReflector, ROTOR_NUM>
Set the Enigma plguboard
Sourcepub fn set_reflector(
self,
reflector: Reflector,
) -> Enigma<AnyRotor, AnyPlugboard, Reflector, ROTOR_NUM>
pub fn set_reflector( self, reflector: Reflector, ) -> Enigma<AnyRotor, AnyPlugboard, Reflector, ROTOR_NUM>
Set the Enigma reflector
Source§impl<AnyPlugboard, AnyReflector, const ROTOR_NUM: usize> Enigma<Wheel<ROTOR_NUM>, AnyPlugboard, AnyReflector, ROTOR_NUM>
impl<AnyPlugboard, AnyReflector, const ROTOR_NUM: usize> Enigma<Wheel<ROTOR_NUM>, AnyPlugboard, AnyReflector, ROTOR_NUM>
Sourcepub fn set_grundstellung(
&mut self,
positions: [Letter; ROTOR_NUM],
) -> &mut Enigma<Wheel<ROTOR_NUM>, AnyPlugboard, AnyReflector, ROTOR_NUM>
pub fn set_grundstellung( &mut self, positions: [Letter; ROTOR_NUM], ) -> &mut Enigma<Wheel<ROTOR_NUM>, AnyPlugboard, AnyReflector, ROTOR_NUM>
Set the starting position for the rotors
Sourcepub fn set_ringstellung(
&mut self,
positions: [u8; ROTOR_NUM],
) -> &mut Enigma<Wheel<ROTOR_NUM>, AnyPlugboard, AnyReflector, ROTOR_NUM>
pub fn set_ringstellung( &mut self, positions: [u8; ROTOR_NUM], ) -> &mut Enigma<Wheel<ROTOR_NUM>, AnyPlugboard, AnyReflector, ROTOR_NUM>
Set the ring settings for the rotors
Sourcepub fn get_grundstellung(&self) -> [Letter; ROTOR_NUM]
pub fn get_grundstellung(&self) -> [Letter; ROTOR_NUM]
Get Rotor
positions
Source§impl<const ROTOR_NUM: usize> Enigma<Wheel<ROTOR_NUM>, Plugboard, Reflector, ROTOR_NUM>
impl<const ROTOR_NUM: usize> Enigma<Wheel<ROTOR_NUM>, Plugboard, Reflector, ROTOR_NUM>
Sourcepub fn encrypt_letter(&mut self, letter: Letter) -> Letter
pub fn encrypt_letter(&mut self, letter: Letter) -> Letter
Encrypt/decrypt a Letter
and advance the state
Sourcepub fn encrypt_char(&mut self, letter: char) -> char
pub fn encrypt_char(&mut self, letter: char) -> char
Encrypt/decrypt a char
and advance the state