class SSEncryptor::Encryptor

Attributes

algorithm[R]

Public Class Methods

new(key, options = {}) click to toggle source
# File lib/SSEncryptor/encryptor.rb, line 7
def initialize(key, options = {})
  @algorithm = options[:algorithm] || 'AES-256-CBC'
  @iv ||= options[:iv]
  process_and_set_key(key)
end

Public Instance Methods

decrypt(data) click to toggle source
# File lib/SSEncryptor/encryptor.rb, line 22
def decrypt(data)
  call(:decrypt, data)
end
encrypt(data) click to toggle source
# File lib/SSEncryptor/encryptor.rb, line 18
def encrypt(data)
  call(:encrypt, data)
end
process_and_set_key(key) click to toggle source
# File lib/SSEncryptor/encryptor.rb, line 13
def process_and_set_key(key)
  key_len = OpenSSL::Cipher.new(@algorithm).key_len
  @key = key.rjust(key_len, key)[0..key_len - 1]
end

Private Instance Methods

call(work, data) click to toggle source
# File lib/SSEncryptor/encryptor.rb, line 28
def call(work, data)
  cipher = OpenSSL::Cipher.new(@algorithm)
  cipher.send(work)
  cipher.key = @key
  cipher.iv = @iv if @iv
  cipher.update(data) + cipher.final
end