Sha256: bebccdd2e957a08a8cc5542d7820e97ae1d67ea1b826d28647ab4202802e4ad3

Contents?: true

Size: 1.82 KB

Versions: 5

Compression:

Stored size: 1.82 KB

Contents

module Xmlenc
  module Builder
    class EncryptedData
      include Xmlenc::Builder::ComplexTypes::EncryptedType

      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,
      }

      tag "EncryptedData"
      namespace "xenc"

      attribute :id, String, :tag => "Id"
      attribute :type, String, :tag => "Type"

      def type
        'http://www.w3.org/2001/04/xmlenc#Element'
      end

      def initialize(*args)
        options = args.extract_options!
        if options.key?(:id)
          self.id = options.delete(:id)
        else
          self.id = SecureRandom.hex(5)
        end
        super(*(args << options))
      end

      def encrypt(data, key_options = {})
        encryptor = algorithm.setup
        encrypted = encryptor.encrypt(data, :node => encryption_method)
        cipher_data.cipher_value = Base64.encode64(encrypted)
        key_params = { :data => encryptor.key }
        encrypted_key = EncryptedKey.new(key_params.merge(key_options))
        encrypted_key.add_data_reference(id)
        encrypted_key
      end

      def set_key_retrieval_method(retrieval_method)
        if retrieval_method
          self.key_info ||= KeyInfo.new
          self.key_info.retrieval_method = retrieval_method
        end
      end

      private

      def algorithm
        algorithm = encryption_method.algorithm
        ALGORITHMS[algorithm] ||
            raise(UnsupportedError.new("Unsupported encryption method #{algorithm}"))
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
xmlenc-0.6.1 lib/xmlenc/builder/encrypted_data.rb
xmlenc-0.6.0 lib/xmlenc/builder/encrypted_data.rb
xmlenc-0.5.0 lib/xmlenc/builder/encrypted_data.rb
xmlenc-0.4.1 lib/xmlenc/builder/encrypted_data.rb
xmlenc-0.4.0 lib/xmlenc/builder/encrypted_data.rb