lib/xml/kit/templatable.rb in xml-kit-0.3.1 vs lib/xml/kit/templatable.rb in xml-kit-0.4.0
- old
+ new
@@ -15,10 +15,18 @@
attr_accessor :signing_key_pair
# The [Xml::Kit::Certificate] that contains the public key to use for encrypting the document.
attr_accessor :encryption_certificate
+ # Allows you to specify the digest method algorithm. (Default: SHA256)
+ # A list of digest methods can be found in [Xml::Kit::Signature].
+ attr_accessor :digest_method
+
+ # Allows you to specify the signature method algorithm. (Default: SHA256)
+ # A list of signature methods can be found in [Xml::Kit::Signature].
+ attr_accessor :signature_method
+
# Returns the generated XML document with an XML Digital Signature and XML Encryption.
def to_xml(xml: ::Builder::XmlMarkup.new, pretty: false)
result = signatures.complete(render(self, xml: xml))
pretty ? Nokogiri::XML(result).to_xml(indent: 2) : result
end
@@ -26,15 +34,16 @@
# Generates an {#Xml::Kit::EncryptedKey} section. https://www.w3.org/TR/xmlenc-core1/#sec-EncryptedKey
#
# @since 0.3.0
# @param xml [Builder::XmlMarkup] the xml builder instance
# @param id [String] the id of EncryptedKey element
- def encrypt_key_for(xml:, id:)
+ def encrypt_key_for(xml:, id:, key_info: nil)
::Xml::Kit::EncryptedKey.new(
id: id,
asymmetric_cipher: asymmetric_cipher,
- symmetric_cipher: symmetric_cipher
+ symmetric_cipher: symmetric_cipher,
+ key_info: key_info
).to_xml(xml: xml)
end
# @deprecated Use {#encrypt_data_for} instead of this
def encryption_for(*args, &block)
@@ -65,10 +74,14 @@
# Provides a default RSA asymmetric cipher. Can be overridden to provide custom ciphers.
#
# @abstract
# @since 0.3.0
def asymmetric_cipher(algorithm: Crypto::RsaCipher::ALGORITHM)
+ unless encryption_certificate
+ raise Xml::Kit::Error, 'encryption_certificate is not specified.'
+ end
+
@asymmetric_cipher ||= Crypto.cipher_for(
algorithm,
encryption_certificate.public_key
)
end
@@ -92,13 +105,15 @@
end
# Allows you to specify which key pair to use for generating an XML digital signature.
#
# @param key_pair [Xml::Kit::KeyPair] the key pair to use for signing.
- def sign_with(key_pair)
+ def sign_with(key_pair, signature_method: :SHA256, digest_method: :SHA256)
self.signing_key_pair = key_pair
self.embed_signature = true
+ self.signature_method = signature_method
+ self.digest_method = digest_method
signatures.sign_with(key_pair)
end
# Allows you to specify which public key to use for generating an XML encrypted element.
#
@@ -116,20 +131,12 @@
# @!visibility private
def signatures
@signatures ||= ::Xml::Kit::Signatures.new(
key_pair: signing_key_pair,
- digest_method: digest_method,
- signature_method: signature_method
+ digest_method: digest_method || :SHA256,
+ signature_method: signature_method || :SHA256
)
- end
-
- def digest_method
- :SHA256
- end
-
- def signature_method
- :SHA256
end
# @!visibility private
def encrypt?
encrypt && encryption_certificate