class Ork::Encryption::Cipher
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
Public Class Methods
new(config = {})
click to toggle source
Creates a cipher that is prepared to encrypt/decrypt a blob. @param [Hash] config the key/cipher/iv needed to initialize OpenSSL
# File lib/ork/cipher.rb, line 18 def initialize(config = {}) Cipher.validate_config @config = config @cipher = OpenSSL::Cipher.new @config[:cipher] end
validate_config(config)
click to toggle source
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.
# 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
Public Instance Methods
decrypt(blob)
click to toggle source
Decrypt stuff. @param [Object] blob the encrypted data to decrypt
# File lib/ork/cipher.rb, line 56 def decrypt(blob) initialize_cipher_for :decrypt "#{@cipher.update blob}#{@cipher.final}" end
encrypt(blob)
click to toggle source
Encrypt stuff. @param [Object] blob the data to encrypt
# File lib/ork/cipher.rb, line 49 def encrypt(blob) initialize_cipher_for :encrypt "#{@cipher.update blob}#{@cipher.final}" end
iv=(iv)
click to toggle source
# File lib/ork/cipher.rb, line 43 def iv=(iv) @config[:iv] = iv end
random_iv!()
click to toggle source
# File lib/ork/cipher.rb, line 39 def random_iv! self.iv = @cipher.random_iv end
Private Instance Methods
initialize_cipher_for(mode)
click to toggle source
This sets the mode so OpenSSL knows to encrypt or decrypt, etc. @param [Symbol] mode either :encrypt or :decrypt
# File lib/ork/cipher.rb, line 65 def initialize_cipher_for(mode) @cipher.send mode @cipher.key = @config[:key] @cipher.iv = @config[:iv] end