Class: Zola::Encryptor
- Inherits:
-
Object
- Object
- Zola::Encryptor
- Defined in:
- lib/encrypt.rb
Instance Attribute Summary collapse
-
#date ⇒ Object
Returns the value of attribute date.
-
#input_file ⇒ Object
Returns the value of attribute input_file.
-
#key ⇒ Object
Returns the value of attribute key.
-
#keys ⇒ Object
Returns the value of attribute keys.
-
#message ⇒ Object
Returns the value of attribute message.
-
#output_file ⇒ Object
Returns the value of attribute output_file.
-
#processed_message ⇒ Object
Returns the value of attribute processed_message.
Instance Method Summary collapse
- #check_message ⇒ Object
- #cipher(rotation) ⇒ Object
- #execute ⇒ Object
- #generate_key ⇒ Object
- #generate_keys ⇒ Object
- #generate_offset ⇒ Object
- #get_date ⇒ Object
- #get_key ⇒ Object
-
#initialize(input_file, output_file) ⇒ Encryptor
constructor
A new instance of Encryptor.
- #output_message ⇒ Object
- #process_letter(letter, rotation) ⇒ Object
- #process_message ⇒ Object
- #read_in_message ⇒ Object
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
#date ⇒ Object
Returns the value of attribute date.
3 4 5 |
# File 'lib/encrypt.rb', line 3 def date @date end |
#input_file ⇒ Object
Returns the value of attribute input_file.
3 4 5 |
# File 'lib/encrypt.rb', line 3 def input_file @input_file end |
#key ⇒ Object
Returns the value of attribute key.
3 4 5 |
# File 'lib/encrypt.rb', line 3 def key @key end |
#keys ⇒ Object
Returns the value of attribute keys.
3 4 5 |
# File 'lib/encrypt.rb', line 3 def keys @keys end |
#message ⇒ Object
Returns the value of attribute message.
3 4 5 |
# File 'lib/encrypt.rb', line 3 def @message end |
#output_file ⇒ Object
Returns the value of attribute output_file.
3 4 5 |
# File 'lib/encrypt.rb', line 3 def output_file @output_file end |
#processed_message ⇒ Object
Returns the value of attribute processed_message.
3 4 5 |
# File 'lib/encrypt.rb', line 3 def @processed_message end |
Instance Method Details
#check_message ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/encrypt.rb', line 62 def @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 |
#execute ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/encrypt.rb', line 82 def execute get_date get_key generate_keys end |
#generate_key ⇒ Object
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_keys ⇒ Object
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_offset ⇒ Object
41 42 43 |
# File 'lib/encrypt.rb', line 41 def generate_offset (@date ** 2).to_s.split(//).map(&:to_i).last(4) end |
#get_date ⇒ Object
20 21 22 |
# File 'lib/encrypt.rb', line 20 def get_date @date = Time.now.strftime("%d%m%y").to_i end |
#get_key ⇒ Object
16 17 18 |
# File 'lib/encrypt.rb', line 16 def get_key @key = 5.times.map { Random.rand(9) }.join end |
#output_message ⇒ Object
91 92 93 94 |
# File 'lib/encrypt.rb', line 91 def 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_message ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/encrypt.rb', line 70 def 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_message ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/encrypt.rb', line 54 def 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 |