Class: Zola::Encryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/encrypt.rb

Direct Known Subclasses

Crack, Decryptor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_file, output_file) ⇒ Encryptor

Returns a new instance of Encryptor.



5
6
7
8
9
10
11
12
13
14
# File 'lib/encrypt.rb', line 5

def initialize(input_file, output_file)
  @input_file = input_file
  @output_file = output_file
  @message = ""
  @processed_message = ""
  @key = ""
  @date = 0
  @keys = []
  @characters =  [*'0'..'9', *'a'..'z',' ','.',","]
end

Instance Attribute Details

#dateObject

Returns the value of attribute date.



3
4
5
# File 'lib/encrypt.rb', line 3

def date
  @date
end

#input_fileObject

Returns the value of attribute input_file.



3
4
5
# File 'lib/encrypt.rb', line 3

def input_file
  @input_file
end

#keyObject

Returns the value of attribute key.



3
4
5
# File 'lib/encrypt.rb', line 3

def key
  @key
end

#keysObject

Returns the value of attribute keys.



3
4
5
# File 'lib/encrypt.rb', line 3

def keys
  @keys
end

#messageObject

Returns the value of attribute message.



3
4
5
# File 'lib/encrypt.rb', line 3

def message
  @message
end

#output_fileObject

Returns the value of attribute output_file.



3
4
5
# File 'lib/encrypt.rb', line 3

def output_file
  @output_file
end

#processed_messageObject

Returns the value of attribute processed_message.



3
4
5
# File 'lib/encrypt.rb', line 3

def processed_message
  @processed_message
end

Instance Method Details

#check_messageObject



62
63
64
65
66
67
68
# File 'lib/encrypt.rb', line 62

def check_message
  @message.downcase!
  pattern = /[^a-z0-9 ,.]/
  if !(@message =~ pattern).nil?
    Kernel.abort("ABORTED! Invalid character(s) in message")
  end
end

#cipher(rotation) ⇒ Object



44
45
46
47
# File 'lib/encrypt.rb', line 44

def cipher(rotation)
  rotated_characters = @characters.rotate(rotation)
  Hash[@characters.zip(rotated_characters)]
end

#executeObject



82
83
84
85
86
87
88
89
90
# File 'lib/encrypt.rb', line 82

def execute
  read_in_message
  check_message
  get_date
  get_key
  generate_keys
  process_message
  output_message
end

#generate_keyObject



33
34
35
36
37
38
39
40
# File 'lib/encrypt.rb', line 33

def generate_key
  key = []
  key << @key[0..1].to_i
  key << @key[1..2].to_i
  key << @key[2..3].to_i
  key << @key[3..4].to_i
  key
end

#generate_keysObject



24
25
26
27
28
29
30
31
32
# File 'lib/encrypt.rb', line 24

def generate_keys
  key = generate_key
  offset = generate_offset
  @keys << key[0] + offset[0]
  @keys << key[1] + offset[1]
  @keys << key[2] + offset[2]
  @keys << key[3] + offset[3]
  @keys
end

#generate_offsetObject



41
42
43
# File 'lib/encrypt.rb', line 41

def generate_offset
  (@date ** 2).to_s.split(//).map(&:to_i).last(4)
end

#get_dateObject



20
21
22
# File 'lib/encrypt.rb', line 20

def get_date
  @date = Time.now.strftime("%d%m%y").to_i
end

#get_keyObject



16
17
18
# File 'lib/encrypt.rb', line 16

def get_key
  @key = 5.times.map { Random.rand(9) }.join
end

#output_messageObject



91
92
93
94
# File 'lib/encrypt.rb', line 91

def output_message
  File.open(@output_file, "w") { |f| f.write(@processed_message)}
  puts "Created #{@output_file} with the key #{@key} and date #{@date}"
end

#process_letter(letter, rotation) ⇒ Object



49
50
51
52
# File 'lib/encrypt.rb', line 49

def process_letter(letter,rotation)
  cipher_for_rotation = cipher(rotation)
  cipher_for_rotation[letter]
end

#process_messageObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/encrypt.rb', line 70

def process_message
  count = 0
  strlen = @message.length
  for i in 0...strlen do
    @processed_message += process_letter(@message[i], @keys[count])
    count +=1
    if count == 4
      count = 0
    end
  end
  @processed_message
end

#read_in_messageObject



54
55
56
57
58
59
60
# File 'lib/encrypt.rb', line 54

def read_in_message
  if File.exist?(@input_file)
    @message = File.open(@input_file, "r") { |f| f.read}
  else
    Kernel.abort("ABORTED! File: #{@input_file} Not Found in current directory")
  end
end