Class: Encrubto::RotN::Encryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/encrubto/rotn/encryptor.rb

Constant Summary collapse

ORIGINAL =
'abcdefghijklmnopqrstuvwxyz'

Instance Method Summary collapse

Instance Method Details

#decrypt(encrypted, offset) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/encrubto/rotn/encryptor.rb', line 15

def decrypt(encrypted, offset)
  if encrypted.is_a?(String) && offset.is_a?(Integer)
    rotn = shift(offset % 26)
    encrypted.tr("#{ rotn }#{ rotn.to_s.upcase }", "#{ ORIGINAL }#{ ORIGINAL.upcase }")
  else
      raise 'First param must be String, second param must be Integer!'
  end
end

#encrypt(str, offset) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/encrubto/rotn/encryptor.rb', line 6

def encrypt(str, offset)
  if str.is_a?(String) && offset.is_a?(Integer)
      rotn = shift(offset % 26)
      str.tr("#{ ORIGINAL }#{ ORIGINAL.upcase }", "#{ rotn }#{ rotn.to_s.upcase }")
  else
    raise 'First param must be String, second param must be Integer!'
  end
end

#shift(offset) ⇒ Object



24
25
26
27
28
# File 'lib/encrubto/rotn/encryptor.rb', line 24

def shift(offset)
  first_part = ORIGINAL[offset..ORIGINAL.length-1]
  second_part = ORIGINAL[0..offset-1]
  first_part.to_s + second_part.to_s
end