Sha256: 11236b0ae1e94eed88a2fb9c0a9e9bbc8dc04f67333874e9068c8523cc3b69c9

Contents?: true

Size: 1.4 KB

Versions: 6

Compression:

Stored size: 1.4 KB

Contents

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

      ALGORITHMS = {
          'http://www.w3.org/2001/04/xmlenc#rsa-1_5'        => Algorithms::RSA15,
          'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p' => Algorithms::RsaOaepMgf1p
      }

      tag "EncryptedKey"
      namespace "xenc"

      attribute :id, String, tag: 'Id'
      attribute :recipient, String, tag: 'Recipient'

      has_one :reference_list, Xmlenc::Builder::ReferenceList, :xpath => "./"

      element :carried_key_name, String, tag: 'CarriedKeyName', namespace: 'xenc'

      attr_accessor :data

      def encrypt(key, data = nil)
        encryptor = algorithm.new(key)
        encrypted = encryptor.encrypt(data || self.data)
        cipher_data.cipher_value = Base64.encode64(encrypted)
      end

      def add_data_reference(data_id)
        self.reference_list ||= ReferenceList.new
        self.reference_list.add_data_reference(data_id)
      end

      def initialize(*args)
        options = args.extract_options!
        @recipient = options.delete(:recipient)
        @id = options.delete(:id)
        super(*(args << options))
      end

      private

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
xmlenc-0.8.0 lib/xmlenc/builder/encrypted_key.rb
xmlenc-0.7.1 lib/xmlenc/builder/encrypted_key.rb
xmlenc-0.7.0 lib/xmlenc/builder/encrypted_key.rb
xmlenc-0.6.9 lib/xmlenc/builder/encrypted_key.rb
xmlenc-0.6.8 lib/xmlenc/builder/encrypted_key.rb
xmlenc-0.6.6 lib/xmlenc/builder/encrypted_key.rb