Class: RotorMachine::Reflector
- Inherits:
-
Object
- Object
- RotorMachine::Reflector
- Defined in:
- lib/rotor_machine/reflector.rb
Overview
Implementation of the Reflector rotor.
A Reflector behaves similarly to a Rotor, except that the Reflector did not rotate. Its purpose is to reflect the signal path back through the rotor stack in the opposite direction, thereby ensuring that the encryption algorithm is symmetric.
The module defines constants for the standard German Enigma reflectors, but you can create a reflector with any string of 26 alphabetic characters. However, you may not repeat a given letter more than once in your string, or else the symmetry of the encipherment algorithm will be broken.
Constant Summary collapse
- REFLECTOR_A =
The letter mapping for the German “A” reflector.
"EJMZALYXVBWFCRQUONTSPIKHGD".freeze
- REFLECTOR_B =
The letter mapping for the German “B” reflector.
"YRUHQSLDPXNGOKMIEBFZCWVJAT".freeze
- REFLECTOR_C =
The letter mapping for the German “C” reflector.
"FVPJIAOYEDRZXWGCTKUQSBNMHL".freeze
- REFLECTOR_B_THIN =
The letter mapping for the German “B Thin” reflector.
"ENKQAUYWJICOPBLMDXZVFTHRGS".freeze
- REFLECTOR_C_THIN =
The letter mapping for the German “C Thin” reflector.
"RDOBJNTKVEHMLFCWZAXGYIPSUQ".freeze
- REFLECTOR_ETW =
The letter mapping for the German “ETW” reflector.
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".freeze
Instance Method Summary collapse
-
#initialize(selected_reflector, start_position = 0) ⇒ Reflector
constructor
Initialize a new Reflector.
-
#reflect(input) ⇒ String
Feed a sequence of characters through the reflector, and return the results.
-
#reflector_kind_name ⇒ Symbol
Return the reflector kind.
-
#to_s ⇒ String
Return a human-readable representation of the Reflector.
Constructor Details
#initialize(selected_reflector, start_position = 0) ⇒ Reflector
Initialize a new RotorMachine::Reflector.
51 52 53 54 55 |
# File 'lib/rotor_machine/reflector.rb', line 51 def initialize(selected_reflector, start_position = 0) @letters = selected_reflector.chars.freeze @alphabet = REFLECTOR_ETW.chars.freeze @position = start_position end |
Instance Method Details
#reflect(input) ⇒ String
Feed a sequence of characters through the reflector, and return the results.
Any characters which are not present on the reflector will be passed through unchanged.
67 68 69 70 71 72 73 74 |
# File 'lib/rotor_machine/reflector.rb', line 67 def reflect(input) input.upcase.chars.each.collect { |c| if @alphabet.include?(c) then @letters[(@alphabet.index(c) + @position) % @alphabet.length] else c end }.join("") end |
#reflector_kind_name ⇒ Symbol
Return the reflector kind.
If the RotorMachine::Reflector is initialized with one of the provided rotor type constants (such as REFLECTOR_A), the name of the reflector will be returned as a symbol. If not, the symbol ‘:CUSTOM` will be returned..
84 85 86 87 |
# File 'lib/rotor_machine/reflector.rb', line 84 def reflector_kind_name self.class.constants.each { |r| return r if (@letters.join("") == self.class.const_get(r)) } return :CUSTOM end |
#to_s ⇒ String
Return a human-readable representation of the RotorMachine::Reflector
93 94 95 |
# File 'lib/rotor_machine/reflector.rb', line 93 def to_s "a RotorMachine::Reflector of type '#{self.reflector_kind_name.to_s}'" end |