Module: Shhh::Extensions::InstanceMethods

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

Overview

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

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



35
36
37
38
39
40
# File 'lib/shhh/extensions/instance_methods.rb', line 35

def decr(encrypted_data, key, iv = nil)
  raise Shhh::Errors::NoPrivateKeyFound if key.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



50
51
52
53
54
55
# File 'lib/shhh/extensions/instance_methods.rb', line 50

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
32
# File 'lib/shhh/extensions/instance_methods.rb', line 27

def encr(data, key, iv = nil)
  raise Shhh::Errors::NoPrivateKeyFound if key.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



42
43
44
45
46
47
48
# File 'lib/shhh/extensions/instance_methods.rb', line 42

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/shhh/extensions/instance_methods.rb', line 22

def encryption_config
  Shhh::Configuration.config
end