Class: Ciphr::Functions::OpenSSL::OpenSslCipher

Inherits:
InvertibleFunction show all
Defined in:
lib/ciphr/functions/openssl.rb

Instance Attribute Summary

Attributes inherited from InvertibleFunction

#invert

Attributes inherited from Function

#args, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from InvertibleFunction

invertable?

Methods inherited from Function

aligned, inherited, #initialize, invertable?, #prepend, #read

Constructor Details

This class inherits a constructor from Ciphr::Functions::Function

Class Method Details

.paramsObject



100
101
102
# File 'lib/ciphr/functions/openssl.rb', line 100

def self.params 
  [:input, :key]
end

.variantsObject



94
95
96
97
98
# File 'lib/ciphr/functions/openssl.rb', line 94

def self.variants
  OpenSSL::Cipher.ciphers.map{|c| c.downcase}.uniq.map do |c|
    [[c, c.gsub(/-/, "")], {:variant => c}]
  end
end

Instance Method Details

#applyObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ciphr/functions/openssl.rb', line 59

def apply
  input, key = @args
  cipher = OpenSSL::Cipher.new(@options[:variant])
  cipher.send(invert ? :decrypt : :encrypt)
  cipher.key = key.read
  random_iv = cipher.random_iv
  if random_iv.size > 0
    cipher.iv = invert ? input.read(random_iv.size) : random_iv
  end
  Proc.new do
    if ! invert && random_iv
      begin
        random_iv
      ensure
        random_iv = nil
      end
    else
      chunk = input.read(256)
      if cipher
        if chunk
          cipher.update(chunk)
        else
          begin
            cipher.final
          ensure
            cipher = nil
          end
        end
      else
        nil
      end
    end
  end
end