Sha256: 0ba126d95f98ed0d210746da0edaf45b1e8692213fbea71532190d2c4321159a

Contents?: true

Size: 1.62 KB

Versions: 6

Compression:

Stored size: 1.62 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(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

6 entries across 6 versions & 1 rubygems

Version Path
xmlenc-0.1.5 lib/xmlenc/encrypted_data.rb
xmlenc-0.1.4 lib/xmlenc/encrypted_data.rb
xmlenc-0.1.3 lib/xmlenc/encrypted_data.rb
xmlenc-0.1.2 lib/xmlenc/encrypted_data.rb
xmlenc-0.1.1 lib/xmlenc/encrypted_data.rb
xmlenc-0.1.0 lib/xmlenc/encrypted_data.rb