lib/xml/kit/decryption.rb in xml-kit-0.1.14 vs lib/xml/kit/decryption.rb in xml-kit-0.2.0

- old
+ new

@@ -3,14 +3,15 @@ module Xml module Kit # {include:file:spec/saml/xml_decryption_spec.rb} class Decryption # The list of private keys to use to attempt to decrypt the document. - attr_reader :private_keys + attr_reader :cipher_registry, :private_keys - def initialize(private_keys:) + def initialize(private_keys:, cipher_registry: ::Xml::Kit::Crypto) @private_keys = private_keys + @cipher_registry = cipher_registry end # Decrypts an EncryptedData section of an XML document. # # @param data [Hash] the XML document converted to a [Hash] using Hash.from_xml. @@ -29,15 +30,15 @@ # Decrypts an EncryptedData section of an XML document. # # @param hash [Hash] the XML document converted to a [Hash] using Hash.from_xml. 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) + to_plaintext( + Base64.decode64(encrypted_data['CipherData']['CipherValue']), + symmetric_key_from(encrypted_data), + encrypted_data['EncryptionMethod']['Algorithm'] + ) end # Decrypts an EncryptedData Nokogiri::XML::Element. # # @param node [Nokogiri::XML::Element.] the XML node to decrypt. @@ -60,11 +61,11 @@ end end raise DecryptionError, private_keys end - def to_plaintext(cipher_text, symmetric_key, algorithm) - Crypto.cipher_for(algorithm, symmetric_key).decrypt(cipher_text) + def to_plaintext(cipher_text, private_key, algorithm) + cipher_registry.cipher_for(algorithm, private_key).decrypt(cipher_text) end end end end