Class: CcipherBox::DecryptionEngine

Inherits:
Object
  • Object
show all
Includes:
TR::CondUtils
Defined in:
lib/ccipher_box/decryption_engine.rb

Instance Method Summary collapse

Constructor Details

#initialize(vault) ⇒ DecryptionEngine

Returns a new instance of DecryptionEngine.



7
8
9
# File 'lib/ccipher_box/decryption_engine.rb', line 7

def initialize(vault)
  @vault = vault
end

Instance Method Details

#finalObject



69
70
71
# File 'lib/ccipher_box/decryption_engine.rb', line 69

def final
  @dec.decrypt_final if not @dec.nil? 
end

#init(output) ⇒ Object

Raises:



11
12
13
14
15
16
17
18
# File 'lib/ccipher_box/decryption_engine.rb', line 11

def init(output)
   
  raise CcipherBox::Error, "Output is mandatory" if output.nil?

  @output = output
  @intOut = MemBuf.new

end

#update(data) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ccipher_box/decryption_engine.rb', line 20

def update(data)
 
  if @dec.nil?
    @intOut.write(data)

    BinStruct.instance.find_struct(@intOut) do |meta, data|

      st = BinStruct.instance.struct_from_bin(meta)

      st.baseMaterial.each do |ebm|

        begin
          
          baseMat = @vault.decrypt(ebm)

          sk = CcipherFactory::SymKey.from_encoded(st.keyConfig) do |ops|
            case ops
            when :password
              baseMat
            end
          end

          @dec = CcipherFactory::SymKeyCipher.decryptor
          @dec.output(@output)
          @dec.key = sk
          @dec.decrypt_init

          @dec.decrypt_update_meta(st.cipherConfig)

          @dec.decrypt_update_cipher(data) if not_empty?(data)

          break

        rescue KeyNotRegistered
          # retry with next key
        end

      end

      raise KeyNotRegistered, "Cannot find any loaded key decrypt this data" if @dec.nil?

    end

  else
    @dec.decrypt_update_cipher(data)
  end

end