@staticmethod<EOL><INDENT>def add_sign(xml, key, cert, debug=False, sign_algorithm=OneLogin_Saml2_Constants.RSA_SHA1, digest_algorithm=OneLogin_Saml2_Constants.SHA1):<DEDENT>
if xml is None or xml == '<STR_LIT>':<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>elem = OneLogin_Saml2_XML.to_etree(xml)<EOL>sign_algorithm_transform_map = {<EOL>OneLogin_Saml2_Constants.DSA_SHA1: xmlsec.Transform.DSA_SHA1,<EOL>OneLogin_Saml2_Constants.RSA_SHA1: xmlsec.Transform.RSA_SHA1,<EOL>OneLogin_Saml2_Constants.RSA_SHA256: xmlsec.Transform.RSA_SHA256,<EOL>OneLogin_Saml2_Constants.RSA_SHA384: xmlsec.Transform.RSA_SHA384,<EOL>OneLogin_Saml2_Constants.RSA_SHA512: xmlsec.Transform.RSA_SHA512<EOL>}<EOL>sign_algorithm_transform = sign_algorithm_transform_map.get(sign_algorithm, xmlsec.Transform.RSA_SHA1)<EOL>signature = xmlsec.template.create(elem, xmlsec.Transform.EXCL_C14N, sign_algorithm_transform, ns='<STR_LIT>')<EOL>issuer = OneLogin_Saml2_XML.query(elem, '<STR_LIT>')<EOL>if len(issuer) > <NUM_LIT:0>:<EOL><INDENT>issuer = issuer[<NUM_LIT:0>]<EOL>issuer.addnext(signature)<EOL>elem_to_sign = issuer.getparent()<EOL><DEDENT>else:<EOL><INDENT>entity_descriptor = OneLogin_Saml2_XML.query(elem, '<STR_LIT>')<EOL>if len(entity_descriptor) > <NUM_LIT:0>:<EOL><INDENT>elem.insert(<NUM_LIT:0>, signature)<EOL><DEDENT>else:<EOL><INDENT>elem[<NUM_LIT:0>].insert(<NUM_LIT:0>, signature)<EOL><DEDENT>elem_to_sign = elem<EOL><DEDENT>elem_id = elem_to_sign.get('<STR_LIT>', None)<EOL>if elem_id is not None:<EOL><INDENT>if elem_id:<EOL><INDENT>elem_id = '<STR_LIT:#>' + elem_id<EOL><DEDENT><DEDENT>else:<EOL><INDENT>generated_id = generated_id = OneLogin_Saml2_Utils.generate_unique_id()<EOL>elem_id = '<STR_LIT:#>' + generated_id<EOL>elem_to_sign.attrib['<STR_LIT>'] = generated_id<EOL><DEDENT>xmlsec.enable_debug_trace(debug)<EOL>xmlsec.tree.add_ids(elem_to_sign, ["<STR_LIT>"])<EOL>digest_algorithm_transform_map = {<EOL>OneLogin_Saml2_Constants.SHA1: xmlsec.Transform.SHA1,<EOL>OneLogin_Saml2_Constants.SHA256: xmlsec.Transform.SHA256,<EOL>OneLogin_Saml2_Constants.SHA384: xmlsec.Transform.SHA384,<EOL>OneLogin_Saml2_Constants.SHA512: xmlsec.Transform.SHA512<EOL>}<EOL>digest_algorithm_transform = digest_algorithm_transform_map.get(digest_algorithm, xmlsec.Transform.SHA1)<EOL>ref = xmlsec.template.add_reference(signature, digest_algorithm_transform, uri=elem_id)<EOL>xmlsec.template.add_transform(ref, xmlsec.Transform.ENVELOPED)<EOL>xmlsec.template.add_transform(ref, xmlsec.Transform.EXCL_C14N)<EOL>key_info = xmlsec.template.ensure_key_info(signature)<EOL>xmlsec.template.add_x509_data(key_info)<EOL>dsig_ctx = xmlsec.SignatureContext()<EOL>sign_key = xmlsec.Key.from_memory(key, xmlsec.KeyFormat.PEM, None)<EOL>sign_key.load_cert_from_memory(cert, xmlsec.KeyFormat.PEM)<EOL>dsig_ctx.key = sign_key<EOL>dsig_ctx.sign(signature)<EOL>return OneLogin_Saml2_XML.to_string(elem)<EOL>
Adds signature key and senders certificate to an element (Message or
Assertion).

:param xml: The element we should sign
:type: string | Document

:param key: The private key
:type: string

:param cert: The public
:type: string

:param debug: Activate the xmlsec debug
:type: bool

:param sign_algorithm: Signature algorithm method
:type sign_algorithm: string

:param digest_algorithm: Digest algorithm method
:type digest_algorithm: string

:returns: Signed XML
:rtype: string