Class: Xmlenc::EncryptedDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/xmlenc/encrypted_document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ EncryptedDocument

Returns a new instance of EncryptedDocument.


5
6
7
# File 'lib/xmlenc/encrypted_document.rb', line 5

def initialize(xml)
  @xml = xml
end

Instance Attribute Details

#xmlObject

Returns the value of attribute xml


3
4
5
# File 'lib/xmlenc/encrypted_document.rb', line 3

def xml
  @xml
end

Instance Method Details

#decrypt(key, fail_silent = false) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/xmlenc/encrypted_document.rb', line 17

def decrypt(key, fail_silent = false)
  encrypted_keys.each do |encrypted_key|
    begin
      encrypted_data = encrypted_key.encrypted_data
      data_key       = encrypted_key.decrypt(key)
      encrypted_data.decrypt(data_key)
    rescue OpenSSL::PKey::RSAError => e
      raise e unless fail_silent
    end
  end
  @document.to_xml
end

#documentObject


9
10
11
# File 'lib/xmlenc/encrypted_document.rb', line 9

def document
  @document ||= Nokogiri::XML(xml, nil, nil, Nokogiri::XML::ParseOptions::STRICT)
end

#encrypted_keysObject


13
14
15
# File 'lib/xmlenc/encrypted_document.rb', line 13

def encrypted_keys
  document.xpath('//xenc:EncryptedKey', NAMESPACES).collect { |n| EncryptedKey.new(n) }
end