module InlineEncryption::Base

Public Instance Methods

config() click to toggle source

@return [InlineEncryption::Config] the configuration instance

# File lib/inline_encryption/base.rb, line 69
def config
  @config ||= Config.new
end
decrypt(data, fail_text=nil) click to toggle source

@param [String] data decryption target @param [String] fail_text text to be returned in the case of a decryption failure @return [String] decrypted target

# File lib/inline_encryption/base.rb, line 57
def decrypt(data, fail_text=nil)
  config.check_required_variables

  begin
    decrypt!(data)
  rescue DecryptionFailureError => e
    return fail_text.nil? ? data : fail_text.to_s
  end
end
decrypt!(data) click to toggle source

@param [String] data decryption target @return [String] decrypted target @raise [DecryptionFailureError] couldn't decrypt the target

# File lib/inline_encryption/base.rb, line 40
def decrypt!(data)
  config.check_required_variables
  raise MisconfigurationError.new(I18n.t('error.pub_key_decrypt')) unless config.real_key.private?

  begin
    converted = Base64.decode64(data)
    decrypted = config.real_key.private_decrypt(converted)
  rescue => e
    err = DecryptionFailureError.exception I18n.t('encrypted', data)
    raise err
  end
end
encrypt(data, fail_text=nil) click to toggle source

@param [String] data encryption target @return [String] encrypted target, or fail_text on error (default data)

# File lib/inline_encryption/base.rb, line 26
def encrypt(data, fail_text=nil)
  config.check_required_variables

  begin
    encrypt!(data)
  rescue EncryptionFailureError => e
    return fail_text.nil? ? data : fail_text.to_s
  end
end
encrypt!(data) click to toggle source

@param [String] data encryption target @return [String] encrypted target @raise [EncryptionFailureError] couldn't encrypt the target

# File lib/inline_encryption/base.rb, line 11
def encrypt!(data)
  config.check_required_variables

  begin
    encrypted = config.real_key.public_encrypt(data)
    converted = Base64.encode64(encrypted)
  rescue => e
    err = EncryptionFailureError.exception I18n.t('target', data: data)
    raise err
  end
end