Class: Ripple::Encryption::Encryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/ripple-encryption/encryptor.rb

Overview

Implements a simple object that can either encrypt or decrypt arbitrary data.

Example usage:

encryptor = Ripple::Encryption::Encryptor.new Ripple::Encryption::Config.defaults
encryptor.encrypt stuff
encryptor.decrypt stuff

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Encryptor

Creates an Encryptor that is prepared to encrypt/decrypt a blob.

Parameters:

  • config (Hash)

    the key/cipher/iv needed to initialize OpenSSL



12
13
14
15
16
17
18
19
# File 'lib/ripple-encryption/encryptor.rb', line 12

def initialize(config)
  # ensure that we have the required configuration keys
  %w(cipher key iv).each do |option|
    raise(Ripple::Encryption::EncryptorConfigError, "Missing configuration option '#{option}'.") if config[option].nil?
  end
  @config = config
  @cipher = OpenSSL::Cipher.new(@config['cipher'])
end

Instance Method Details

#decrypt(blob) ⇒ Object

Decrypt stuff.

Parameters:

  • blob (Object)

    the encrypted data to decrypt



30
31
32
33
# File 'lib/ripple-encryption/encryptor.rb', line 30

def decrypt(blob)
  initialize_cipher_for :decrypt
  "#{@cipher.update blob}#{@cipher.final}"
end

#encrypt(blob) ⇒ Object

Encrypt stuff.

Parameters:

  • blob (Object)

    the data to encrypt



23
24
25
26
# File 'lib/ripple-encryption/encryptor.rb', line 23

def encrypt(blob)
  initialize_cipher_for :encrypt
  "#{@cipher.update blob}#{@cipher.final}"
end