Skip to content

Commit

Permalink
Merge pull request #3 from gijolopez/restructure
Browse files Browse the repository at this point in the history
Restructure
  • Loading branch information
gijolopez authored Feb 8, 2018
2 parents eeba034 + dbac559 commit 84b3d20
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 24 deletions.
69 changes: 69 additions & 0 deletions lib/crack.rb
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
4 changes: 2 additions & 2 deletions lib/decryptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# Implementation code for decryptor function.
class Decryptor
attr_reader :message
def initialize(message)
def initialize(message,key,date)
@message = message
@offset = Offset.new('00000')
@offset = Offset.new(key,date)
end

def split_chars
Expand Down
43 changes: 23 additions & 20 deletions lib/encryptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@
# Encryptor class implementation code.
class Encryptor
attr_reader :message
def initialize(message)
def initialize(message,key,date)
@message = message
@offset = Offset.new('00000')
@offset = Offset.new(key,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 split_chars
@message.downcase.chars
end
Expand All @@ -19,16 +29,6 @@ def groups_of_four
sub_strings
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 rotate_alphabet_by_b
alphabet_b = alphabet
@offset.b_rotation.times do
Expand Down Expand Up @@ -78,13 +78,16 @@ def hash_d
end

def encrypt
encrypted_message = groups_of_four.map do |substring|
a = hash_a[substring[0]]
b = hash_b[substring[1]]
c = hash_c[substring[2]]
d = hash_d[substring[3]]
"#{a}#{b}#{c}#{d}"
end
encrypted_message.join
encrypted_message = groups_of_four.map do |substring|
a = hash_a[substring[0]]
b = hash_b[substring[1]]
c = hash_c[substring[2]]
d = hash_d[substring[3]]
"#{a}#{b}#{c}#{d}"
end
encrypted_message.join
end
end

encryptor = Encryptor.new('the ..end..', '12345', '070218')
puts encryptor.encrypt
28 changes: 28 additions & 0 deletions lib/enigma.rb
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
3 changes: 1 addition & 2 deletions lib/offset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
class Offset
attr_reader :date, :key

def initialize(key = KeyGenerator.new.new_key,
date = Date.today.strftime('%d%m%y'))
def initialize(key,date)
@key = key
@date = date
end
Expand Down
47 changes: 47 additions & 0 deletions test/crack_test.rb
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
28 changes: 28 additions & 0 deletions test/enigma_test.rb
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

0 comments on commit 84b3d20

Please sign in to comment.