main
 1# frozen_string_literal: true
 2
 3module Xml
 4  module Kit
 5    # An implementation of the Signature element.
 6    # https://www.w3.org/TR/xmldsig-core1/#sec-Signature
 7    #
 8    # @since 0.1.0
 9    class Signature
10      SIGNATURE_METHODS = {
11        SHA1: 'http://www.w3.org/2000/09/xmldsig#rsa-sha1',
12        SHA224: 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha224',
13        SHA256: 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256',
14        SHA384: 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha384',
15        SHA512: 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha512',
16      }.freeze
17      DIGEST_METHODS = {
18        SHA1: 'http://www.w3.org/2000/09/xmldsig#sha1',
19        SHA224: 'http://www.w3.org/2001/04/xmldsig-more#sha224',
20        SHA256: 'http://www.w3.org/2001/04/xmlenc#sha256',
21        SHA384: 'http://www.w3.org/2001/04/xmldsig-more#sha384',
22        SHA512: 'http://www.w3.org/2001/04/xmlenc#sha512',
23      }.freeze
24
25      attr_reader :certificate
26      attr_reader :digest_method
27      attr_reader :reference_id
28      attr_reader :signature_method
29
30      def initialize(reference_id,
31                     signature_method: :SH256,
32                     digest_method: :SHA256,
33                     certificate:)
34        @certificate = certificate
35        @digest_method = DIGEST_METHODS[digest_method]
36        @reference_id = reference_id
37        @signature_method = SIGNATURE_METHODS[signature_method]
38      end
39
40      def to_xml(xml: ::Builder::XmlMarkup.new)
41        ::Xml::Kit::Template.new(self).to_xml(xml: xml)
42      end
43    end
44  end
45end