module Philotic::Serialization::Encryptor

Public Instance Methods

content_type() click to toggle source
# File lib/philotic/serialization/encryptor.rb, line 11
def content_type
  'application/json'
end
default_algorithm() click to toggle source
# File lib/philotic/serialization/encryptor.rb, line 27
def default_algorithm
  'aes-256-cbc'
end
default_encryption_key() click to toggle source
# File lib/philotic/serialization/encryptor.rb, line 19
def default_encryption_key
  Philotic.config.encryption_key
end
dump(payload, metadata) click to toggle source
# File lib/philotic/serialization/encryptor.rb, line 39
def dump(payload, metadata)
  metadata[:headers][:encryption] ||= {}
  algorithm                       = (metadata[:headers][:encryption][:algorithm] ||= default_algorithm)
  iv                              = Base64.decode64(metadata[:headers][:encryption][:iv] ||= Base64.encode64(random_iv(algorithm)))
  salt                            = Base64.decode64(metadata[:headers][:encryption][:salt] ||= Base64.encode64(random_salt))
  Base64.encode64 ::Encryptor.encrypt(payload, key: key, iv: iv, salt: salt)
end
key() click to toggle source
# File lib/philotic/serialization/encryptor.rb, line 35
def key
  @key ||= default_encryption_key
end
load(payload, metadata) click to toggle source
# File lib/philotic/serialization/encryptor.rb, line 47
def load(payload, metadata)
  headers = Marshal.load(Marshal.dump(metadata[:headers])).deep_symbolize_keys

  iv = Base64.decode64 headers[:encryption][:iv]
  salt = Base64.decode64 headers[:encryption][:salt]
  ::Encryptor.decrypt(Base64.decode64(payload), key: key, iv: iv, salt: salt)
end
random_iv(algorithm = default_algorithm) click to toggle source
# File lib/philotic/serialization/encryptor.rb, line 31
def random_iv(algorithm = default_algorithm)
  OpenSSL::Cipher::Cipher.new(algorithm).random_iv
end
random_salt() click to toggle source
# File lib/philotic/serialization/encryptor.rb, line 23
def random_salt
  Base64.encode64 SecureRandom.random_bytes(256)
end
serialization() click to toggle source
# File lib/philotic/serialization/encryptor.rb, line 15
def serialization
  :encrypted
end