class Mongoid::Encrypted::Encryptor

Public Class Methods

decrypt(value) click to toggle source
# File lib/mongoid/encrypted/encryptor.rb, line 17
def decrypt(value)
  crypt = instance

  if config.rotations.present?
    config.rotations.each { |r| crypt.rotate *Array.wrap(r) }
  end

  crypt.decrypt_and_verify(value)
end
encrypt(value) click to toggle source
# File lib/mongoid/encrypted/encryptor.rb, line 13
def encrypt(value)
  instance.encrypt_and_sign(value)
end

Private Class Methods

config() click to toggle source
# File lib/mongoid/encrypted/encryptor.rb, line 56
def config
  Encrypted.configuration
end
handle_missing_key() click to toggle source
# File lib/mongoid/encrypted/encryptor.rb, line 48
def handle_missing_key
  raise(
    MissingKeyError,
    key_path: config.key_path,
    env_key: config.env_key
  )
end
instance() click to toggle source
# File lib/mongoid/encrypted/encryptor.rb, line 29
def instance
  ActiveSupport::MessageEncryptor.new(
    [ key ].pack("H*"),
    cipher: config.cipher
  )
end
key() click to toggle source
# File lib/mongoid/encrypted/encryptor.rb, line 36
def key
  read_env_key || read_key_file || handle_missing_key
end
read_env_key() click to toggle source
# File lib/mongoid/encrypted/encryptor.rb, line 40
def read_env_key
  ENV[config.env_key]
end
read_key_file() click to toggle source
# File lib/mongoid/encrypted/encryptor.rb, line 44
def read_key_file
  Rails.root.join(config.key_path).binread.strip rescue nil
end