module 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/encryptor.rb
48 def decrypt(*args, &block)
49   crypt :decrypt, *args, &block
50 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/encryptor.rb
18 def default_options
19   @default_options ||= { algorithm: 'aes-256-gcm',
20                          auth_data: '',
21                          insecure_mode: false,
22                          hmac_iterations: 2000,
23                          v2_gcm_iv: false }
24 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/encryptor.rb
35 def encrypt(*args, &block)
36   crypt :encrypt, *args, &block
37 end

Protected Instance Methods

encryption?(cipher_method) click to toggle source
    # File lib/encryptor.rb
103 def encryption?(cipher_method)
104   cipher_method == :encrypt
105 end
extract_auth_tag(value) click to toggle source
    # File lib/encryptor.rb
111 def extract_auth_tag(value)
112   value[-16..-1]
113 end
extract_cipher_text(value) click to toggle source
    # File lib/encryptor.rb
107 def extract_cipher_text(value)
108   value[0..-17]
109 end