module Eligible::Encryptor

A simple wrapper for the standard OpenSSL library

Public Instance Methods

decrypt(*args, &block) click to toggle source

Decrypts a :value with a specified :key and :iv.

Optionally accepts :salt, :auth_data, :algorithm, :hmac_iterations, and :insecure_mode options.

Example

decrypted_value = Encryptor.decrypt(value: 'some encrypted string', key: 'some secret key', iv: 'some unique value', salt: 'another unique value')
# or
decrypted_value = Encryptor.decrypt('some encrypted string', key: 'some secret key', iv: 'some unique value', salt: 'another unique value')
# File lib/eligible/encryptor.rb, line 48
def decrypt(*args, &block)
  crypt :decrypt, *args, &block
end
default_options() click to toggle source

The default options to use when calling the encrypt and decrypt methods

Defaults to { algorithm: ‘aes-256-gcm’,

auth_data: '',
insecure_mode: false,
hmac_iterations: 2000,
v2_gcm_iv: false }

Run ‘openssl list-cipher-commands’ in your terminal to view a list all cipher algorithms that are supported on your platform

# File lib/eligible/encryptor.rb, line 16
def default_options
  @default_options ||= {
    algorithm: 'aes-256-cbc',
    auth_data: '',
    insecure_mode: false,
    hmac_iterations: 2000,
    v2_gcm_iv: false
  }
end
encrypt(*args, &block) click to toggle source

Encrypts a :value with a specified :key and :iv.

Optionally accepts :salt, :auth_data, :algorithm, :hmac_iterations, and :insecure_mode options.

Example

encrypted_value = Encryptor.encrypt(value: 'some string to encrypt', key: 'some secret key', iv: 'some unique value', salt: 'another unique value')
# or
encrypted_value = Encryptor.encrypt('some string to encrypt', key: 'some secret key', iv: 'some unique value', salt: 'another unique value')
# File lib/eligible/encryptor.rb, line 35
def encrypt(*args, &block)
  crypt :encrypt, *args, &block
end

Protected Instance Methods

encryption?(cipher_method) click to toggle source
# File lib/eligible/encryptor.rb, line 109
def encryption?(cipher_method)
  cipher_method == :encrypt
end
extract_auth_tag(value) click to toggle source
# File lib/eligible/encryptor.rb, line 117
def extract_auth_tag(value)
  value[-16..-1]
end
extract_cipher_text(value) click to toggle source
# File lib/eligible/encryptor.rb, line 113
def extract_cipher_text(value)
  value[0..-17]
end