Module: Sym::Crypt::Extensions::InstanceMethods
- Includes:
- CipherHandler, Data
- Defined in:
- lib/sym/crypt/extensions/instance_methods.rb
Overview
This is the module that is really included in your class when you include Sym::Crypt
.
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
Instance Method Summary collapse
-
#decr(encrypted_data, key, iv = nil) ⇒ Object
Expects key to be a base64 encoded key.
- #decr_password(encrypted_data, password, iv = nil) ⇒ Object
-
#encr(data, key, iv = nil) ⇒ Object
Expects key to be a base64 encoded key.
- #encr_password(data, password, iv = nil) ⇒ Object
Methods included from CipherHandler
#create_cipher, #update_cipher
Methods included from Data
Instance Method Details
#decr(encrypted_data, key, iv = nil) ⇒ Object
Expects key to be a base64 encoded key
33 34 35 36 37 38 39 |
# File 'lib/sym/crypt/extensions/instance_methods.rb', line 33 def decr(encrypted_data, key, iv = nil) raise Sym::Crypt::Errors::NoPrivateKeyFound unless key.present? raise Sym::Crypt::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
51 52 53 54 55 56 57 58 |
# File 'lib/sym/crypt/extensions/instance_methods.rb', line 51 def decr_password(encrypted_data, password, iv = nil) raise Sym::Crypt::Errors::NoDataProvided unless encrypted_data.present? raise Sym::Crypt::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
24 25 26 27 28 29 30 |
# File 'lib/sym/crypt/extensions/instance_methods.rb', line 24 def encr(data, key, iv = nil) raise Sym::Crypt::Errors::NoPrivateKeyFound unless key.present? raise Sym::Crypt::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
41 42 43 44 45 46 47 48 49 |
# File 'lib/sym/crypt/extensions/instance_methods.rb', line 41 def encr_password(data, password, iv = nil) raise Sym::Crypt::Errors::NoDataProvided unless data.present? raise Sym::Crypt::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 |