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:, cipher_registry: ::Xml::Kit::Crypto) ⇒ Decryption

Returns a new instance of Decryption.



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

def initialize(private_keys:, cipher_registry: ::Xml::Kit::Crypto)
  @private_keys = private_keys
  @cipher_registry = cipher_registry
end

Instance Attribute Details

#cipher_registryObject (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 cipher_registry
  @cipher_registry
end

#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

Deprecated.

Use #decrypt_hash instead of this

Decrypts an EncryptedData section of an XML document.

Parameters:

  • data (Hash)

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



19
20
21
22
23
24
# File 'lib/xml/kit/decryption.rb', line 19

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.



36
37
38
39
40
41
42
43
# File 'lib/xml/kit/decryption.rb', line 36

def decrypt_hash(hash)
  data = hash['EncryptedData']
  to_plaintext(
    Base64.decode64(data['CipherData']['CipherValue']),
    symmetric_key_from(data['KeyInfo']['EncryptedKey']),
    data['EncryptionMethod']['Algorithm']
  )
end

#decrypt_node(node) ⇒ Object

Decrypts an EncryptedData Nokogiri::XML::Element.

Parameters:

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

    the XML node to decrypt.



48
49
50
51
52
# File 'lib/xml/kit/decryption.rb', line 48

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.



29
30
31
# File 'lib/xml/kit/decryption.rb', line 29

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