rotor_machine 1.0.11 → 1.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +26 -4
- data/lib/rotor_machine/factory.rb +27 -5
- data/lib/rotor_machine/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 54bee549dee381db8f08c906984d17aa7a543733f38eb468102b56c7662443f2
|
4
|
+
data.tar.gz: e72ba384c15532351786ee8d3521e9c3316a230889f8799d5197e40f0749e561
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43f67bbc06116c4c1a3d0ccef333f01ebb4c2eb2ff7b76528b2a03a8be4a4ece9f0f6e226ad34bc227aa6c9ca6323d6ec686ede3fd64026c84a10f08f4cc6c4d
|
7
|
+
data.tar.gz: 65a8df5ae035d2ff47fb1164ce30470566da792dd7c5a72779f525000f1e99c62b5bd19f0c3378634cccca8029e9546967426e5aea20fc12116abc5d86cd0215
|
data/README.md
CHANGED
@@ -118,6 +118,9 @@ factory methods whcih set up, respectively, a fully configured machine
|
|
118
118
|
with a default set of rotors and reflector, and an empty machine with
|
119
119
|
no rotors or reflector.
|
120
120
|
|
121
|
+
You can also create a new `RotorMachine::Machine` (or its various parts)
|
122
|
+
using the `RotorMachine::Factory` methods, as shown in the second example.
|
123
|
+
|
121
124
|
## Example
|
122
125
|
|
123
126
|
```ruby
|
@@ -130,16 +133,35 @@ machine.rotors << RotorMachine::Rotor.new(RotorMachine::Rotor::ROTOR_II, "A", 1)
|
|
130
133
|
machine.rotors << RotorMachine::Rotor.new(RotorMachine::Rotor::ROTOR_III, "A", 1)
|
131
134
|
machine.reflector = RotorMachine::Reflector.new(RotorMachine::Reflector::REFLECTOR_A)
|
132
135
|
|
133
|
-
machine.plugboard.connect("A", "M")
|
136
|
+
machine.plugboard.connect("A", "M")
|
137
|
+
machine.plugboard.connect("Q", "K")
|
134
138
|
|
135
139
|
machine.set_rotors("CFL")
|
136
140
|
plaintext = "This is a super secret message".upcase
|
137
|
-
ciphertext = machine.encipher(plaintext) # => "
|
141
|
+
ciphertext = machine.encipher(plaintext) # => "MYGMS ZLTWS AAIDD VTGOC RFKFO"
|
142
|
+
|
143
|
+
machine.set_rotors("CFL")
|
144
|
+
new_plaintext = machine.encipher(ciphertext) # => "THISI SASUP ERSEC RETME SSAGE"
|
145
|
+
```
|
146
|
+
|
147
|
+
## Example - Simplified Setup Using the Factory Methods
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
require 'rotor_machine'
|
138
151
|
|
152
|
+
machine = RotorMachine::Factory.build_machine(
|
153
|
+
rotors: [:ROTOR_I, :ROTOR_II, :ROTOR_III],
|
154
|
+
reflector: :REFLECTOR_A,
|
155
|
+
connections: {"A" => "M", "Q" => "K" }
|
156
|
+
)
|
139
157
|
machine.set_rotors("CFL")
|
140
|
-
new_plaintext = machine.encipher(ciphertext) # => "THIS IS A SUPER SECRET MESSAGE"
|
141
158
|
|
142
|
-
|
159
|
+
plaintext = "This is a super secret message".upcase
|
160
|
+
|
161
|
+
ciphertext = machine.encipher(plaintext) # => "MYGMS ZLTWS AAIDD VTGOC RFKFO"
|
162
|
+
|
163
|
+
machine.set_rotors("CFL")
|
164
|
+
new_plaintext = machine.encipher(ciphertext) # => "THISI SASUP ERSEC RETME SSAGE"
|
143
165
|
```
|
144
166
|
|
145
167
|
## Documentation
|
@@ -148,9 +148,16 @@ module RotorMachine
|
|
148
148
|
# The options hash can provide the following options:
|
149
149
|
#
|
150
150
|
# - `:rotors` - An array of {Rotor} objects. This can be constructed
|
151
|
-
#
|
152
|
-
#
|
153
|
-
#
|
151
|
+
# manually, through multiple calls to {#build_rotor}, or through a single
|
152
|
+
# call to {#build_rotor_set}. Alternatively, you can pass an array of
|
153
|
+
# symbols which match the constants in the {Rotor} class, and {Rotor}
|
154
|
+
# objects will be built from those (using default position and step
|
155
|
+
# sizes).
|
156
|
+
#
|
157
|
+
# - `:reflector` - A {Reflector} object. Alternatively, a symbol
|
158
|
+
# matching one of the reflector type constants can be passed in, and
|
159
|
+
# a {Reflector} of the specified type will be created.j
|
160
|
+
#
|
154
161
|
# - `:connections` - A {Hash} of connections to make on the new {Machine}'s
|
155
162
|
# {Plugboard}.
|
156
163
|
#
|
@@ -162,8 +169,23 @@ module RotorMachine
|
|
162
169
|
connections = options.fetch(:connections, {})
|
163
170
|
|
164
171
|
m = RotorMachine::Machine.new()
|
165
|
-
rotors.each
|
166
|
-
|
172
|
+
rotors.each do |r|
|
173
|
+
if r.class.name == "RotorMachine::Rotor"
|
174
|
+
m.rotors << r
|
175
|
+
elsif r.class.name == "Symbol"
|
176
|
+
m.rotors << RotorMachine::Factory.build_rotor(rotor_kind: r)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
unless reflector.nil?
|
181
|
+
if reflector.class.name == "Symbol"
|
182
|
+
m.reflector = RotorMachine::Factory.build_reflector(reflector_kind: reflector)
|
183
|
+
elsif reflector.class.name == "RotorMachine::Reflector"
|
184
|
+
m.reflector = reflector
|
185
|
+
else
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
167
189
|
m.plugboard = RotorMachine::Plugboard.new()
|
168
190
|
unless connections.empty?
|
169
191
|
connections.each { |from, to| m.plugboard.connect(from, to) }
|