class Portunus::Encrypters::OpenSslAes

Attributes

key[R]
value[R]

Public Class Methods

decrypt(key:, value:) click to toggle source
# File lib/portunus/encrypters/open_ssl_aes.rb, line 14
def self.decrypt(key:, value:)
  new(key: key, value: value).decrypt
end
encrypt(key:, value:) click to toggle source
# File lib/portunus/encrypters/open_ssl_aes.rb, line 4
def self.encrypt(key:, value:)
  new(key: key, value: value).encrypt
end
generate_key() click to toggle source
# File lib/portunus/encrypters/open_ssl_aes.rb, line 8
def self.generate_key
  cipher = OpenSSL::Cipher::AES256.new :CBC
  cipher.encrypt
  Base64.strict_encode64(cipher.random_key)
end
new(key:, value:) click to toggle source
# File lib/portunus/encrypters/open_ssl_aes.rb, line 18
def initialize(key:, value:)
  @key = Base64.strict_decode64(key)
  @value = value
end

Public Instance Methods

decrypt() click to toggle source
# File lib/portunus/encrypters/open_ssl_aes.rb, line 39
def decrypt
  iv, encrypted_text = value.split("$")

  decipher = OpenSSL::Cipher::AES256.new :CBC
  decipher.decrypt

  decipher.iv = Base64.strict_decode64(iv) # previously saved
  decipher.key = key

  output = decipher.update(Base64.strict_decode64(encrypted_text)) + decipher.final

  output
end
encrypt() click to toggle source
# File lib/portunus/encrypters/open_ssl_aes.rb, line 23
def encrypt
  cipher = OpenSSL::Cipher::AES256.new :CBC
  cipher.encrypt
  iv = cipher.random_iv

  cipher.key = key
  cipher.iv = iv

  encrypted_output = cipher.update(value) + cipher.final

  [
    Base64.strict_encode64(iv),
    Base64.strict_encode64(encrypted_output)
  ].join("$")
end