Module: Sym::Extensions::InstanceMethods

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

Overview

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

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



36
37
38
39
40
41
42
# File 'lib/sym/extensions/instance_methods.rb', line 36

def decr(encrypted_data, key, iv = nil)
  raise Sym::Errors::NoPrivateKeyFound unless key.present?
  raise Sym::Errors::NoDataProvided unless encrypted_data.present?
  decrypt_data(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



54
55
56
57
58
59
60
61
# File 'lib/sym/extensions/instance_methods.rb', line 54

def decr_password(encrypted_data, password, iv = nil)
  raise Sym::Errors::NoDataProvided unless encrypted_data.present?
  raise Sym::Errors::NoPasswordProvided unless password.present?
  decrypt_data(encrypted_data, encryption_config.password_cipher, iv) do |cipher_struct|
    key,                     = make_password_key(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
32
33
# File 'lib/sym/extensions/instance_methods.rb', line 27

def encr(data, key, iv = nil)
  raise Sym::Errors::NoPrivateKeyFound unless key.present?
  raise Sym::Errors::NoDataProvided unless data.present?
  encrypt_data(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



44
45
46
47
48
49
50
51
52
# File 'lib/sym/extensions/instance_methods.rb', line 44

def encr_password(data, password, iv = nil)
  raise Sym::Errors::NoDataProvided unless data.present?
  raise Sym::Errors::NoPasswordProvided unless password.present?
  encrypt_data(data, encryption_config.password_cipher, iv) do |cipher_struct|
    key, salt                = make_password_key(cipher_struct.cipher, password)
    cipher_struct.cipher.key = key
    cipher_struct.salt       = salt
  end
end

#encryption_configObject



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

def encryption_config
  Sym::Configuration.config
end