-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from gijolopez/restructure
Restructure
- Loading branch information
Showing
7 changed files
with
198 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require 'pry' | ||
class Crack | ||
attr_reader :encrypted_message, | ||
:date | ||
def initialize(encrypted_message, | ||
date = Date.today.strftime('%d%m%y')) | ||
|
||
@encrypted_message = encrypted_message | ||
@date = date | ||
end | ||
|
||
def alphabet | ||
['a', 'b', 'c', 'd', 'e', 'f', | ||
'g', 'h', 'i', 'j', 'k', 'l', | ||
'm', 'n', 'o', 'p', 'q', 'r', | ||
's', 't', 'u', 'v', 'w', 'x', | ||
'y', 'z', '0', '1', '2', '3', | ||
'4', '5', '6', '7', '8', '9', | ||
' ', '.', ','] | ||
end | ||
|
||
def calculate_0_mod_rotation | ||
last_four_array = @encrypted_message.chars[-4..-1] | ||
actual_last_four = ["n", "d", ".", "."] | ||
zipped = last_four_array.zip(actual_last_four) | ||
zipped.map do |subarray| | ||
alphabet.index(subarray[1]) - alphabet.index(subarray[0]) | ||
end | ||
end | ||
|
||
def calculate_1_mod_rotation | ||
last_four_array = @encrypted_message.chars[-5..-2] | ||
actual_last_four = ["e", "n", "d", "."] | ||
zipped = last_four_array.zip(actual_last_four) | ||
zipped.map do |subarray| | ||
alphabet.index(subarray[1]) - alphabet.index(subarray[0]) | ||
end | ||
end | ||
|
||
def calculate_2_mod_rotation | ||
last_four_array = @encrypted_message.chars[-6..-3] | ||
actual_last_four = [".", "e", "n", "d"] | ||
zipped = last_four_array.zip(actual_last_four) | ||
zipped.map do |subarray| | ||
alphabet.index(subarray[1]) - alphabet.index(subarray[0]) | ||
end | ||
end | ||
|
||
def calculate_3_mod_rotation | ||
last_four_array = @encrypted_message.chars[-7..-4] | ||
actual_last_four = [".", ".", "e", "n"] | ||
zipped = last_four_array.zip(actual_last_four) | ||
zipped.map do |subarray| | ||
alphabet.index(subarray[1]) - alphabet.index(subarray[0]) | ||
end | ||
end | ||
|
||
def remainder | ||
if @encrypted_message.length % 4 == 0 | ||
calculate_0_mod_rotation | ||
elsif @encrypted_message.length % 4 == 1 | ||
calculate_1_mod_rotation | ||
elsif @encrypted_message.length % 4 == 2 | ||
calculate_2_mod_rotation | ||
else | ||
calculate_3_mod_rotation | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require_relative 'key_generator' | ||
require_relative 'offset' | ||
require_relative 'encryptor' | ||
require_relative 'decryptor' | ||
|
||
class Enigma | ||
|
||
def decrypt(message, | ||
key = KeyGenerator.new.new_key, | ||
date = Date.today.strftime('%d%m%y')) | ||
|
||
decryptor = Decryptor.new(message,key,date) | ||
decryptor.decrypt | ||
end | ||
|
||
def encrypt(message, | ||
key = KeyGenerator.new.new_key, | ||
date = Date.today.strftime('%d%m%y')) | ||
|
||
encryptor = Encryptor.new(message,key,date) | ||
encryptor.encrypt | ||
end | ||
|
||
|
||
|
||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'minitest/autorun' | ||
require 'minitest/pride' | ||
require './lib/key_generator' | ||
require './lib/offset' | ||
require './lib/encryptor' | ||
require './lib/decryptor' | ||
require './lib/enigma' | ||
require './lib/crack' | ||
|
||
class CrackTest < Minitest::Test | ||
|
||
def test_does_it_exisit | ||
crack = Crack.new('ksadfkcdfkdls', '070218') | ||
|
||
assert_instance_of Crack, crack | ||
end | ||
|
||
def test_it_can_pass_encrypted_message | ||
crack = Crack.new('ksadfkcdfkdls', '070218') | ||
|
||
assert_equal 'ksadfkcdfkdls', crack.encrypted_message | ||
assert_equal '070218', crack.date | ||
end | ||
|
||
def test_can_calculate_total_rotation_array | ||
crack = Crack.new('hr0bxw08', '070218') | ||
|
||
assert_equal [-10, -19, 11, 3], crack.calculate_0_mod_rotation | ||
assert_equal [3, -10, -19, 11], crack.calculate_1_mod_rotation | ||
assert_equal [11, 3, -10, -19], crack.calculate_2_mod_rotation | ||
assert_equal [20, 11, 3, -10], crack.calculate_3_mod_rotation | ||
end | ||
|
||
def test_which_rotation_to_calculate | ||
crack = Crack.new('hr0bxw08', '070218') | ||
assert_equal [-10, -19, 11, 3], crack.remainder | ||
|
||
crack = Crack.new('chr0bxw08', '070218') | ||
assert_equal [3, -10, -19, 11], crack.remainder | ||
|
||
crack = Crack.new('cyhr0bxw08', '070218') | ||
assert_equal [11, 3, -10, -19], crack.remainder | ||
|
||
crack = Crack.new('iuyhr0bxw08', '070218') | ||
assert_equal [20, 11, 3, -10], crack.remainder | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'minitest/autorun' | ||
require 'minitest/pride' | ||
require './lib/key_generator' | ||
require './lib/offset' | ||
require './lib/encryptor' | ||
require './lib/decryptor' | ||
require './lib/enigma' | ||
|
||
class EnigmaTest < Minitest::Test | ||
def test_does_it_exisit | ||
enigma = Enigma.new | ||
|
||
assert_instance_of Enigma, enigma | ||
end | ||
|
||
def test_can_enigma_decrypt | ||
enigma = Enigma.new | ||
|
||
assert_equal 'hell' , enigma.decrypt('ojnp','00000') | ||
end | ||
|
||
def test_can_enigma_encrypt | ||
enigma = Enigma.new | ||
|
||
assert_equal 'ojnp' , enigma.encrypt('hell','00000') | ||
end | ||
|
||
end |