enigma_ruby 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2e53ea3ed0ad1827e09e9f3f5a05842af0e4cb87aeb855922736c20526f9867c
4
- data.tar.gz: 986f7539cb3633e3aa55c8dcd46c52ae7c54e90e63a3a8cdd17e28e89617e1d6
3
+ metadata.gz: d2aa5a5b1b3a00b9462ddf323f15adcd89c56b1d788f6366223d398b04cfdefd
4
+ data.tar.gz: 4110e1e5ff0f19f873c081cebe52da2ee574286be70eea4177f5997ee3de205a
5
5
  SHA512:
6
- metadata.gz: df1107dbd9fa1154605f3ffc17d282d517111f0cfe3ca2e45e72551fbf0150bd1cad466ad11dff13e20e502682f285c26bcfa97aa3f6a4008215dd9e402ef79e
7
- data.tar.gz: 8e11afbdbea5fe6e8cc8d0a5409e9d7f762946b47039b090a9cb29e6b4fddb04481fa80fbe012bf6d37d16ce76ffc3ff0ae3b05adea2ffca022c537d2d9b626a
6
+ metadata.gz: 65cb27db3efe47b644560594c9d03e7ebefc557103fd5feb1be8235b6c17e3fdbc213d2baab1dd3ca4bb93e6cc10e909342dd4e1d67268859d5f399b420ae5a0
7
+ data.tar.gz: e40bb17690c5bd0c748a6bca21a0337d3247dfa41364fb3d246d1ce679ed441aaf77921e9ebf17d43f6d130d79fbb020b868fd326af147f25d5de18e5f56d0ca
@@ -0,0 +1,19 @@
1
+ module EnigmaRuby
2
+ module Commands
3
+ class AdvanceRotors < Base
4
+ def initialize(rotors)
5
+ @rotors = rotors
6
+ end
7
+
8
+ def execute(_)
9
+ @rotors[0].advance
10
+ @rotors.each_with_index do |rotor, i|
11
+ next if i == 0
12
+ break unless @rotors[i - 1].at_notch?
13
+
14
+ rotor.advance
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ module EnigmaRuby
2
+ module Commands
3
+ class Base
4
+ def execute(context)
5
+ raise NotImplementedError, 'Subclasses must implement execute(context)'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ module EnigmaRuby
2
+ module Commands
3
+ class EncodeRotorBackward < Base
4
+ def initialize(rotors)
5
+ @rotors = rotors
6
+ end
7
+
8
+ def execute(context)
9
+ @rotors.reverse_each do |rotor|
10
+ context[:encoded_char] = rotor.encode_backward(context[:encoded_char])
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module EnigmaRuby
2
+ module Commands
3
+ class EncodeRotorForward < Base
4
+ def initialize(rotors)
5
+ @rotors = rotors
6
+ end
7
+
8
+ def execute(context)
9
+ @rotors.each do |rotor|
10
+ context[:encoded_char] = rotor.encode_forward(context[:encoded_char])
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module EnigmaRuby
2
+ module Commands
3
+ class Reflect < Base
4
+ def execute(context)
5
+ context[:encoded_char] = Reflector.reflect(context[:encoded_char])
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module EnigmaRuby
2
+ module Commands
3
+ class SwapPlugboard < Base
4
+ def execute(context)
5
+ context[:encoded_char] = Plugboard.swap(context[:encoded_char])
6
+ end
7
+ end
8
+ end
9
+ end
@@ -6,12 +6,12 @@ module EnigmaRuby
6
6
  def initialize(rotor_settings = [])
7
7
  initialize_rotors(rotor_settings)
8
8
  @commands = [
9
- SwapPlugboardCommand.new,
10
- EncodeRotorForwardCommand.new(@rotors),
11
- ReflectCommand.new,
12
- EncodeRotorBackwardCommand.new(@rotors),
13
- SwapPlugboardCommand.new,
14
- AdvanceRotorsCommand.new(@rotors)
9
+ Commands::SwapPlugboard.new,
10
+ Commands::EncodeRotorForward.new(@rotors),
11
+ Commands::Reflect.new,
12
+ Commands::EncodeRotorBackward.new(@rotors),
13
+ Commands::SwapPlugboard.new,
14
+ Commands::AdvanceRotors.new(@rotors)
15
15
  ]
16
16
  end
17
17
 
@@ -1,14 +1,22 @@
1
1
 
2
2
  module EnigmaRuby
3
3
  class Plugboard
4
- WIRING = {
5
- 'A' => 'F', 'F' => 'A',
6
- 'B' => 'E', 'E' => 'B',
7
- 'C' => 'D', 'D' => 'C'
8
- }.freeze
4
+ PAIRS = %w[AF BE CD].freeze
9
5
 
10
6
  def self.swap(char)
11
- WIRING[char] || char
7
+ check_duplicates
8
+
9
+ PAIRS.each do |pair|
10
+ return pair[1] if pair[0] == char
11
+ return pair[0] if pair[1] == char
12
+ end
13
+ char
14
+ end
15
+
16
+ def self.check_duplicates
17
+ characters = PAIRS.join.chars
18
+ duplicates = characters.select { |char| characters.count(char) > 1 }.uniq
19
+ raise "Duplicate characters found: #{duplicates.join(', ')}" if duplicates.any?
12
20
  end
13
21
  end
14
22
  end
@@ -1,10 +1,15 @@
1
1
  module EnigmaRuby
2
2
  class Reflector
3
- WIRING = 'YRUHQSLDPXNGOKMIEBFZCWVJAT'.freeze
3
+ WIRING = {
4
+ 'ORIGIN' => 'YRUHQSLDPXNGOKMIEBFZCWVJAT',
5
+ 'UKW-A' => 'EJMZALYXVBWFCRQUONTSPIKHGD',
6
+ 'UKW-B' => 'YRUHQSLDPXNGOKMIEBFZCWVJAT',
7
+ 'UKW-C' => 'FVPJIAOYEDRZXWGCTKUQSBNMHL'
8
+ }.freeze
4
9
 
5
- def self.reflect(char)
10
+ def self.reflect(char, wiring_key = 'ORIGIN')
6
11
  index = char.ord - 'A'.ord
7
- WIRING[index]
12
+ WIRING[wiring_key][index]
8
13
  end
9
14
  end
10
15
  end
@@ -6,21 +6,27 @@ module EnigmaRuby
6
6
  end
7
7
 
8
8
  def encode_forward(char)
9
- index = (char.ord - 'A'.ord + @position) % 26
9
+ index = (char.ord - 'A'.ord + @position) % wiring_size
10
10
  @wiring[index]
11
11
  end
12
12
 
13
13
  def encode_backward(char)
14
14
  index = @wiring.index(char)
15
- ((index - @position) % 26 + 'A'.ord).chr
15
+ ((index - @position) % wiring_size + 'A'.ord).chr
16
16
  end
17
17
 
18
18
  def advance
19
- @position = (@position + 1) % 26
19
+ @position = (@position + 1) % wiring_size
20
20
  end
21
21
 
22
22
  def at_notch?
23
23
  @position == @notch
24
24
  end
25
+
26
+ private
27
+
28
+ def wiring_size
29
+ @wiring.size
30
+ end
25
31
  end
26
32
  end
@@ -1,3 +1,3 @@
1
1
  module EnigmaRuby
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.5"
3
3
  end
data/lib/enigma_ruby.rb CHANGED
@@ -3,12 +3,12 @@ require "enigma_ruby/rotor"
3
3
  require "enigma_ruby/reflector"
4
4
  require "enigma_ruby/plugboard"
5
5
  require "enigma_ruby/enigma"
6
- require "enigma_ruby/commands/command"
7
- require "enigma_ruby/commands/advance_rotors_command"
8
- require "enigma_ruby/commands/encode_rotor_backward_command"
9
- require "enigma_ruby/commands/encode_rotor_forward_command"
10
- require "enigma_ruby/commands/reflect_command"
11
- require "enigma_ruby/commands/swap_plugboard_command"
6
+ require "enigma_ruby/commands/base"
7
+ require "enigma_ruby/commands/advance_rotors"
8
+ require "enigma_ruby/commands/encode_rotor_backward"
9
+ require "enigma_ruby/commands/encode_rotor_forward"
10
+ require "enigma_ruby/commands/reflect"
11
+ require "enigma_ruby/commands/swap_plugboard"
12
12
 
13
13
  module EnigmaRuby
14
14
  DEFAULT_ROTOR_SETTINGS = [
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enigma_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - chibicco
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-13 00:00:00.000000000 Z
11
+ date: 2024-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -62,12 +62,12 @@ extra_rdoc_files: []
62
62
  files:
63
63
  - README.md
64
64
  - lib/enigma_ruby.rb
65
- - lib/enigma_ruby/commands/advance_rotors_command.rb
66
- - lib/enigma_ruby/commands/command.rb
67
- - lib/enigma_ruby/commands/encode_rotor_backward_command.rb
68
- - lib/enigma_ruby/commands/encode_rotor_forward_command.rb
69
- - lib/enigma_ruby/commands/reflect_command.rb
70
- - lib/enigma_ruby/commands/swap_plugboard_command.rb
65
+ - lib/enigma_ruby/commands/advance_rotors.rb
66
+ - lib/enigma_ruby/commands/base.rb
67
+ - lib/enigma_ruby/commands/encode_rotor_backward.rb
68
+ - lib/enigma_ruby/commands/encode_rotor_forward.rb
69
+ - lib/enigma_ruby/commands/reflect.rb
70
+ - lib/enigma_ruby/commands/swap_plugboard.rb
71
71
  - lib/enigma_ruby/enigma.rb
72
72
  - lib/enigma_ruby/plugboard.rb
73
73
  - lib/enigma_ruby/reflector.rb
@@ -1,17 +0,0 @@
1
- module EnigmaRuby
2
- class AdvanceRotorsCommand < Command
3
- def initialize(rotors)
4
- @rotors = rotors
5
- end
6
-
7
- def execute(_)
8
- @rotors[0].advance
9
- @rotors.each_with_index do |rotor, i|
10
- next if i == 0
11
- break unless @rotors[i - 1].at_notch?
12
-
13
- rotor.advance
14
- end
15
- end
16
- end
17
- end
@@ -1,7 +0,0 @@
1
- module EnigmaRuby
2
- class Command
3
- def execute(context)
4
- raise NotImplementedError, 'Subclasses must implement execute(context)'
5
- end
6
- end
7
- end
@@ -1,13 +0,0 @@
1
- module EnigmaRuby
2
- class EncodeRotorBackwardCommand < Command
3
- def initialize(rotors)
4
- @rotors = rotors
5
- end
6
-
7
- def execute(context)
8
- @rotors.reverse_each do |rotor|
9
- context[:encoded_char] = rotor.encode_backward(context[:encoded_char])
10
- end
11
- end
12
- end
13
- end
@@ -1,13 +0,0 @@
1
- module EnigmaRuby
2
- class EncodeRotorForwardCommand < Command
3
- def initialize(rotors)
4
- @rotors = rotors
5
- end
6
-
7
- def execute(context)
8
- @rotors.each do |rotor|
9
- context[:encoded_char] = rotor.encode_forward(context[:encoded_char])
10
- end
11
- end
12
- end
13
- end
@@ -1,7 +0,0 @@
1
- module EnigmaRuby
2
- class ReflectCommand < Command
3
- def execute(context)
4
- context[:encoded_char] = Reflector.reflect(context[:encoded_char])
5
- end
6
- end
7
- end
@@ -1,7 +0,0 @@
1
- module EnigmaRuby
2
- class SwapPlugboardCommand < Command
3
- def execute(context)
4
- context[:encoded_char] = Plugboard.swap(context[:encoded_char])
5
- end
6
- end
7
- end