Class: Ork::Encryption::Cipher
- Inherits:
-
Object
- Object
- Ork::Encryption::Cipher
- Defined in:
- lib/ork/cipher.rb
Overview
Implements a simple object that can either encrypt or decrypt arbitrary data.
Example:
cipher = Ork::Encryption::Cipher.new config_hash
cipher.encrypt stuff
cipher.decrypt stuff
Class Method Summary collapse
-
.validate_config(config) ⇒ Object
Validates the configuration has all the required values to encrypt and decrypt an object.
Instance Method Summary collapse
-
#decrypt(blob) ⇒ Object
Decrypt stuff.
-
#encrypt(blob) ⇒ Object
Encrypt stuff.
-
#initialize(config = {}) ⇒ Cipher
constructor
Creates a cipher that is prepared to encrypt/decrypt a blob.
- #iv=(iv) ⇒ Object
- #random_iv! ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ Cipher
Creates a cipher that is prepared to encrypt/decrypt a blob.
18 19 20 21 |
# File 'lib/ork/cipher.rb', line 18 def initialize(config = {}) Cipher.validate_config @config = config @cipher = OpenSSL::Cipher.new @config[:cipher] end |
Class Method Details
.validate_config(config) ⇒ Object
Validates the configuration has all the required values to encrypt and decrypt an object
Note: if the configuration is invalid, an ‘Ork::Encryption::MissingConfig` error is raised.
29 30 31 32 33 34 35 36 37 |
# File 'lib/ork/cipher.rb', line 29 def self.validate_config(config) if config.nil? || ([:cipher, :key] - config.keys).any? raise MissingConfig, 'Make sure to provide the full configuration to Ork::Encryption. ' + 'Use Ork::Encryption.init(config_hash) to set the configuration ' + 'or assert that Ork::Encryption::Cipher.new receives a non empty' + ' hash with :cipher and :key values.' end end |
Instance Method Details
#decrypt(blob) ⇒ Object
Decrypt stuff.
56 57 58 59 |
# File 'lib/ork/cipher.rb', line 56 def decrypt(blob) initialize_cipher_for :decrypt "#{@cipher.update blob}#{@cipher.final}" end |
#encrypt(blob) ⇒ Object
Encrypt stuff.
49 50 51 52 |
# File 'lib/ork/cipher.rb', line 49 def encrypt(blob) initialize_cipher_for :encrypt "#{@cipher.update blob}#{@cipher.final}" end |
#iv=(iv) ⇒ Object
43 44 45 |
# File 'lib/ork/cipher.rb', line 43 def iv=(iv) @config[:iv] = iv end |
#random_iv! ⇒ Object
39 40 41 |
# File 'lib/ork/cipher.rb', line 39 def random_iv! self.iv = @cipher.random_iv end |