Class: Xml::Kit::Decryption

Inherits:
Object
  • Object
show all
Defined in:
lib/xml/kit/decryption.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(private_keys:) ⇒ Decryption

Returns a new instance of Decryption.



10
11
12
# File 'lib/xml/kit/decryption.rb', line 10

def initialize(private_keys:)
  @private_keys = private_keys
end

Instance Attribute Details

#private_keysObject (readonly)

The list of private keys to use to attempt to decrypt the document.



8
9
10
# File 'lib/xml/kit/decryption.rb', line 8

def private_keys
  @private_keys
end

Instance Method Details

#decrypt(data) ⇒ Object

Decrypts an EncryptedData section of an XML document.

Parameters:

  • data (Hash)

    the XML document converted to a [Hash] using Hash.from_xml.



17
18
19
20
# File 'lib/xml/kit/decryption.rb', line 17

def decrypt(data)
  ::Xml::Kit.deprecate('decrypt is deprecated. Use decrypt_xml or decrypt_hash instead.')
  decrypt_hash(data)
end

#decrypt_hash(hash) ⇒ Object

Decrypts an EncryptedData section of an XML document.

Parameters:

  • hash (Hash)

    the XML document converted to a [Hash] using Hash.from_xml.



32
33
34
35
36
37
38
39
# File 'lib/xml/kit/decryption.rb', line 32

def decrypt_hash(hash)
  encrypted_data = hash['EncryptedData']
  symmetric_key = symmetric_key_from(encrypted_data)
  cipher_value = encrypted_data['CipherData']['CipherValue']
  cipher_text = Base64.decode64(cipher_value)
  algorithm = encrypted_data['EncryptionMethod']['Algorithm']
  to_plaintext(cipher_text, symmetric_key, algorithm)
end

#decrypt_node(node) ⇒ Object

Decrypts an EncryptedData Nokogiri::XML::Element.

Parameters:

  • node (Nokogiri::XML::Element.)

    the XML node to decrypt.



44
45
46
47
48
# File 'lib/xml/kit/decryption.rb', line 44

def decrypt_node(node)
  return node unless !node.nil? && node.name == 'EncryptedData'

  node.parent.replace(decrypt_xml(node.to_s))[0]
end

#decrypt_xml(raw_xml) ⇒ Object

Decrypts an EncryptedData section of an XML document.

Parameters:

  • raw_xml (String)

    the XML document as a string.



25
26
27
# File 'lib/xml/kit/decryption.rb', line 25

def decrypt_xml(raw_xml)
  decrypt_hash(Hash.from_xml(raw_xml))
end