Module: Secrets::Extensions::InstanceMethods

Includes:
CipherHandler, Data
Defined in:
lib/secrets/extensions/instance_methods.rb

Overview

This is the module that is really included in your class when you include Secrets.

The module provides easy access to the encryption configuration via the #encryption_config method, as well as two key methods: #encr and #decr.

Methods #encr_password and #decr_password provide a good example of how this module can be extended to provide more uses of various ciphers, by calling into the private _encr and _decr methods.f

Constant Summary

Constants included from CipherHandler

CipherHandler::CREATE_CIPHER

Instance Method Summary collapse

Methods included from CipherHandler

#create_cipher, #new_cipher, #update_cipher

Methods included from Data

#decode, #encode

Instance Method Details

#decr(encrypted_data, key, iv = nil) ⇒ Object

Expects key to be a base64 encoded key



34
35
36
37
38
# File 'lib/secrets/extensions/instance_methods.rb', line 34

def decr(encrypted_data, key, iv = nil)
  _decr(encrypted_data, encryption_config.data_cipher, iv) do |cipher_struct|
    cipher_struct.cipher.key = decode_key(key)
  end
end

#decr_password(encrypted_data, password, iv = nil) ⇒ Object



48
49
50
51
52
53
# File 'lib/secrets/extensions/instance_methods.rb', line 48

def decr_password(encrypted_data, password, iv = nil)
  _decr(encrypted_data, encryption_config.password_cipher, iv) do |cipher_struct|
    key,                     = _key_from_password(cipher_struct.cipher, password, cipher_struct.salt)
    cipher_struct.cipher.key = key
  end
end

#encr(data, key, iv = nil) ⇒ Object

Expects key to be a base64 encoded key



27
28
29
30
31
# File 'lib/secrets/extensions/instance_methods.rb', line 27

def encr(data, key, iv = nil)
  _encr(data, encryption_config.data_cipher, iv) do |cipher_struct|
    cipher_struct.cipher.key = decode_key(key)
  end
end

#encr_password(data, password, iv = nil) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/secrets/extensions/instance_methods.rb', line 40

def encr_password(data, password, iv = nil)
  _encr(data, encryption_config.password_cipher, iv) do |cipher_struct|
    key, salt                = _key_from_password(cipher_struct.cipher, password)
    cipher_struct.cipher.key = key
    cipher_struct.salt       = salt
  end
end

#encryption_configObject



22
23
24
# File 'lib/secrets/extensions/instance_methods.rb', line 22

def encryption_config
  Secrets::Configuration.config
end