@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>elif isinstance(xml, etree._Element):<EOL><INDENT>elem = xml<EOL><DEDENT>elif isinstance(xml, Document):<EOL><INDENT>xml = xml.toxml()<EOL>elem = fromstring(xml.encode('<STR_LIT:utf-8>'), forbid_dtd=True)<EOL><DEDENT>elif isinstance(xml, Element):<EOL><INDENT>xml.setAttributeNS(<EOL>unicode(OneLogin_Saml2_Constants.NS_SAMLP),<EOL>'<STR_LIT>',<EOL>unicode(OneLogin_Saml2_Constants.NS_SAMLP)<EOL>)<EOL>xml.setAttributeNS(<EOL>unicode(OneLogin_Saml2_Constants.NS_SAML),<EOL>'<STR_LIT>',<EOL>unicode(OneLogin_Saml2_Constants.NS_SAML)<EOL>)<EOL>xml = xml.toxml()<EOL>elem = fromstring(xml.encode('<STR_LIT:utf-8>'), forbid_dtd=True)<EOL><DEDENT>elif isinstance(xml, basestring):<EOL><INDENT>elem = fromstring(xml.encode('<STR_LIT:utf-8>'), forbid_dtd=True)<EOL><DEDENT>else:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>error_callback_method = None<EOL>if debug:<EOL><INDENT>error_callback_method = print_xmlsec_errors<EOL><DEDENT>xmlsec.set_error_callback(error_callback_method)<EOL>sign_algorithm_transform_map = {<EOL>OneLogin_Saml2_Constants.DSA_SHA1: xmlsec.TransformDsaSha1,<EOL>OneLogin_Saml2_Constants.RSA_SHA1: xmlsec.TransformRsaSha1,<EOL>OneLogin_Saml2_Constants.RSA_SHA256: xmlsec.TransformRsaSha256,<EOL>OneLogin_Saml2_Constants.RSA_SHA384: xmlsec.TransformRsaSha384,<EOL>OneLogin_Saml2_Constants.RSA_SHA512: xmlsec.TransformRsaSha512<EOL>}<EOL>sign_algorithm_transform = sign_algorithm_transform_map.get(sign_algorithm, xmlsec.TransformRsaSha1)<EOL>signature = Signature(xmlsec.TransformExclC14N, sign_algorithm_transform, nsPrefix='<STR_LIT>')<EOL>issuer = OneLogin_Saml2_Utils.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_Utils.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.addIDs(elem_to_sign, ["<STR_LIT>"])<EOL>digest_algorithm_transform_map = {<EOL>OneLogin_Saml2_Constants.SHA1: xmlsec.TransformSha1,<EOL>OneLogin_Saml2_Constants.SHA256: xmlsec.TransformSha256,<EOL>OneLogin_Saml2_Constants.SHA384: xmlsec.TransformSha384,<EOL>OneLogin_Saml2_Constants.SHA512: xmlsec.TransformSha512<EOL>}<EOL>digest_algorithm_transform = digest_algorithm_transform_map.get(digest_algorithm, xmlsec.TransformSha1)<EOL>ref = signature.addReference(digest_algorithm_transform)<EOL>if elem_id:<EOL><INDENT>ref.attrib['<STR_LIT>'] = elem_id<EOL><DEDENT>ref.addTransform(xmlsec.TransformEnveloped)<EOL>ref.addTransform(xmlsec.TransformExclC14N)<EOL>key_info = signature.ensureKeyInfo()<EOL>key_info.addX509Data()<EOL>dsig_ctx = xmlsec.DSigCtx()<EOL>sign_key = xmlsec.Key.loadMemory(key, xmlsec.KeyDataFormatPem, None)<EOL>file_cert = OneLogin_Saml2_Utils.write_temp_file(cert)<EOL>sign_key.loadCert(file_cert.name, xmlsec.KeyDataFormatCertPem)<EOL>file_cert.close()<EOL>dsig_ctx.signKey = sign_key<EOL>dsig_ctx.sign(signature)<EOL>return tostring(elem, encoding='<STR_LIT>').encode('<STR_LIT:utf-8>')<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