Sha256: 4ed9b886392a0a273c6713f9e9bf76d1eb2e5a7b8db66db17aecad417104f581
Contents?: true
Size: 1.66 KB
Versions: 16
Compression:
Stored size: 1.66 KB
Contents
module Xmlenc class EncryptedData ALGORITHMS = { 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc' => Algorithms::DES3CBC, 'http://www.w3.org/2001/04/xmlenc#aes128-cbc' => Algorithms::AESCBC[128], 'http://www.w3.org/2001/04/xmlenc#aes256-cbc' => Algorithms::AESCBC[256] } TYPES = { 'http://www.w3.org/2001/04/xmlenc#Element' => :element, 'http://www.w3.org/2001/04/xmlenc#Content' => :content, } attr_accessor :node def initialize(node) @node = node end def document @node.document end def encryption_method at_xpath('./xenc:EncryptionMethod') end def cipher_value at_xpath('./xenc:CipherData/xenc:CipherValue').content.gsub(/[\n\s]/, '') end def cipher_value=(value) at_xpath('./xenc:CipherData/xenc:CipherValue').content = value end def decrypt(key) decryptor = algorithm.setup(key) decrypted = decryptor.decrypt(Base64.decode64(cipher_value), :node => encryption_method) @node.replace(Nokogiri::XML::DocumentFragment.parse(decrypted)) unless @node == document.root decrypted end def encrypt(data) encryptor = algorithm.setup encrypted = encryptor.encrypt(data, :node => encryption_method) self.cipher_value = Base64.encode64(encrypted) encryptor.key end def type TYPES[@node['Type']] end private def at_xpath(xpath) @node.at_xpath(xpath, NAMESPACES) end def algorithm algorithm = encryption_method['Algorithm'] ALGORITHMS[algorithm] || raise(UnsupportedError.new("Unsupported encryption method #{algorithm}")) end end end
Version data entries
16 entries across 16 versions & 1 rubygems