Class: Encryption

Inherits:
Object
  • Object
show all
Includes:
EnigmaHelpers
Defined in:
lib/enigma/encryption.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EnigmaHelpers

#character_map, #date_of_encryption, #format_month, #generate_key, #key_rotation, #offset_key, #offset_rotation, #total_rotation

Constructor Details

#initialize(plain_file, encrypted_file) ⇒ Encryption

Returns a new instance of Encryption.



5
6
7
8
9
10
11
12
# File 'lib/enigma/encryption.rb', line 5

def initialize(plain_file, encrypted_file)
  @plain_file = plain_file
  @encrypted_file = encrypted_file
  @count = 0
  @key = generate_key
  @character_map = character_map
  @read_write = Files.new
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



4
5
6
# File 'lib/enigma/encryption.rb', line 4

def key
  @key
end

Instance Method Details

#encrypt(plain_text) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/enigma/encryption.rb', line 14

def encrypt(plain_text)
  text = ""
  plain_text.each_byte do |each_char|
    text << @character_map[encrypt_helper(each_char, @count)]
    @count += 1
  end
  text
end

#encrypt_helper(each_char, _count) ⇒ Object



23
24
25
# File 'lib/enigma/encryption.rb', line 23

def encrypt_helper(each_char, _count)
  (@character_map.index(each_char.chr) + get_total_rotation) % @character_map.size
end

#encrypt_writeObject



31
32
33
# File 'lib/enigma/encryption.rb', line 31

def encrypt_write
  @read_write.write_file(@encrypted_file, encrypt(file_to_encrypt))
end

#file_to_encryptObject



35
36
37
# File 'lib/enigma/encryption.rb', line 35

def file_to_encrypt
  @read_write.read_file(@plain_file).chomp
end

#get_total_rotationObject



27
28
29
# File 'lib/enigma/encryption.rb', line 27

def get_total_rotation
  total_rotation(@count, @key, offset_key(date_of_encryption))
end