class IOP::CipherDecryptor

Filter class to perform decryption with a symmetric key algorithm (ciphering) of the data passed through.

The class is an adapter for OpenSSL::Cipher & compatible classes.

### Use case: decrypt a file with default algorithm and embedded initial vector.

require 'iop/file'
require 'iop/openssl'
( IOP::FileReader.new('input.aes') | IOP::CipherDecryptor.new(key: my_secret_key) | (s = IOP::StringMerger.new) ).process!
puts s.to_s

@since 0.1

Attributes

iv[R]

Returns initial vector (IV) for decryption session.

key[R]

Returns decryption key.

Public Class Methods

new(cipher = DEFAULT_OPENSSL_CIPHER, key:, iv: nil) click to toggle source

Creates class instance.

@param cipher [String, OpenSSL::Cipher] cipher used for decryption

@param key [String] string representing an encryption key

@param iv [String] string representing an initial vector or nil

cipher can be either a String or OpenSSL::Cipher instance. If it is a string, a corresponding OpenSSL::Cipher instance will be created.

If iv is nil, the initial vector will be obtained from the upstream data. Refer to {CipherEncryptor#initialize} for details.

# File lib/iop/openssl.rb, line 120
def initialize(cipher = DEFAULT_OPENSSL_CIPHER, key:, iv: nil)
  @cipher = cipher.is_a?(String) ? OpenSSL::Cipher.new(cipher) : cipher
  @cipher.decrypt
  @cipher.key = @key = key
  @cipher.iv = @iv = iv unless iv.nil?
end

Public Instance Methods

process(data = nil) click to toggle source
Calls superclass method IOP::Sink#process
# File lib/iop/openssl.rb, line 127
def process(data = nil)
  unless @continue
    @continue = true
    @buffer = IOP.allocate_string(data.size)
    if iv.nil?
      @cipher.iv = @iv = data[0, @cipher.iv_len]
      data = data[@cipher.iv_len..-1]
    end
  end
  if data.nil?
    super(@cipher.final)
    super
  else
    super(@cipher.update(data, @buffer)) unless data.size.zero?
  end
end