Sha256: a61fa7f2e8aaa6e733554bd5464361cc1d969eec74181687560ee936d5b81436

Contents?: true

Size: 901 Bytes

Versions: 1

Compression:

Stored size: 901 Bytes

Contents

module Encrubto::RotN
  class Encryptor
  
    ORIGINAL = 'abcdefghijklmnopqrstuvwxyz'

    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

    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

    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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
encrubto-0.2.0 lib/encrubto/rotn/encryptor.rb