Sha256: fb3d0bb7e8e6931018dace08501218f5e4610a5602ae5ffec791926b913b5731

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

module Xml
  module Kit
    # An implementation of the EncryptedKey element.
    # https://www.w3.org/TR/xmlenc-core1/#sec-EncryptedData
    #
    # @since 0.3.0
    class EncryptedData
      attr_reader :id
      attr_reader :key_info
      attr_reader :symmetric_cipher
      attr_reader :symmetric_cipher_value

      def initialize(raw_xml,
                     id: Id.generate,
                     symmetric_cipher: nil,
                     asymmetric_cipher: nil,
                     key_info: nil)
        @id = id
        @symmetric_cipher = symmetric_cipher ||
          key_info.try(:symmetric_cipher) ||
          Xml::Kit::Crypto::SymmetricCipher.new
        @symmetric_cipher_value = Base64.strict_encode64(
          @symmetric_cipher.encrypt(raw_xml)
        )
        @key_info = key_info ||
          create_key_info_for(@symmetric_cipher, asymmetric_cipher)
      end

      def to_xml(xml: ::Builder::XmlMarkup.new)
        ::Xml::Kit::Template.new(self).to_xml(xml: xml)
      end

      def render(model, options)
        ::Xml::Kit::Template.new(model).to_xml(options)
      end

      private

      def create_key_info_for(symmetric_cipher, asymmetric_cipher)
        KeyInfo.new do |x|
          x.encrypted_key = EncryptedKey.new(
            asymmetric_cipher: asymmetric_cipher,
            symmetric_cipher: symmetric_cipher
          )
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
xml-kit-0.6.0 lib/xml/kit/encrypted_data.rb
xml-kit-0.5.0 lib/xml/kit/encrypted_data.rb
xml-kit-0.4.0 lib/xml/kit/encrypted_data.rb