enigma_ruby 0.1.4 → 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 +4 -4
- data/lib/enigma_ruby/plugboard.rb +14 -6
- data/lib/enigma_ruby/reflector.rb +8 -3
- data/lib/enigma_ruby/rotor.rb +9 -3
- data/lib/enigma_ruby/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d2aa5a5b1b3a00b9462ddf323f15adcd89c56b1d788f6366223d398b04cfdefd
|
4
|
+
data.tar.gz: 4110e1e5ff0f19f873c081cebe52da2ee574286be70eea4177f5997ee3de205a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 65cb27db3efe47b644560594c9d03e7ebefc557103fd5feb1be8235b6c17e3fdbc213d2baab1dd3ca4bb93e6cc10e909342dd4e1d67268859d5f399b420ae5a0
|
7
|
+
data.tar.gz: e40bb17690c5bd0c748a6bca21a0337d3247dfa41364fb3d246d1ce679ed441aaf77921e9ebf17d43f6d130d79fbb020b868fd326af147f25d5de18e5f56d0ca
|
@@ -1,14 +1,22 @@
|
|
1
1
|
|
2
2
|
module EnigmaRuby
|
3
3
|
class Plugboard
|
4
|
-
|
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
|
-
|
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 =
|
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
|
data/lib/enigma_ruby/rotor.rb
CHANGED
@@ -6,21 +6,27 @@ module EnigmaRuby
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def encode_forward(char)
|
9
|
-
index = (char.ord - 'A'.ord + @position) %
|
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) %
|
15
|
+
((index - @position) % wiring_size + 'A'.ord).chr
|
16
16
|
end
|
17
17
|
|
18
18
|
def advance
|
19
|
-
@position = (@position + 1) %
|
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
|
data/lib/enigma_ruby/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2024-07-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|