diff --git a/pynfe/processamento/assinatura.py b/pynfe/processamento/assinatura.py index 0db1450..28f5d49 100644 --- a/pynfe/processamento/assinatura.py +++ b/pynfe/processamento/assinatura.py @@ -26,21 +26,26 @@ class AssinaturaA1(Assinatura): """Classe responsavel por efetuar a assinatura do certificado digital no XML informado. Passar XML como string.""" - def assinar(self, xml, retona_string=False: - arquivo_cert = CertificadoA1(self.certificado) - chave, cert = arquivo_cert.separar_arquivo(self.senha, caminho=False) + def assinar(self, xml, retorna_string=False): + try: + arquivo_cert = CertificadoA1(self.certificado) + chave, cert = arquivo_cert.separar_arquivo(self.senha, caminho=False) + + signer = signxml.xmldsig(xml, digest_algorithm="sha1") + signer.sign(method=signxml.methods.enveloped, key=chave, cert=cert, + algorithm="rsa-sha1", c14n_algorithm='http://www.w3.org/TR/2001/REC-xml-c14n-20010315') + #signer.verify() + # reference_uri nao funciona + #verified_data = signer.verify(require_x509=True, ca_pem_file="cert.pem") + + ##chave_id = xml.find('.//infNFe[@Id]').attrib['Id'] + xml.findall('.//{http://www.w3.org/2000/09/xmldsig#}Reference')[0] \ + .attrib['URI'] = '#'+xml.findall('infNFe')[0].attrib['Id'] + + if retorna_string: + return etree.tostring(xml, encoding="unicode", pretty_print=True) + else: + return xml + except Exception as e: + raise e - signer = signxml.xmldsig(xml, digest_algorithm="sha1") - signer.sign(method=signxml.methods.enveloped, key=chave, cert=cert, - algorithm="rsa-sha1", c14n_algorithm='http://www.w3.org/TR/2001/REC-xml-c14n-20010315') - # reference_uri nao funciona - #verified_data = signer.verify(require_x509=True, ca_pem_file="cert.pem") - - ##chave_id = xml.find('.//infNFe[@Id]').attrib['Id'] - xml.findall('.//{http://www.w3.org/2000/09/xmldsig#}Reference')[0] \ - .attrib['URI'] = '#'+xml.findall('infNFe')[0].attrib['Id'] - - if retona_string: - return etree.tostring(xml, encoding="unicode", pretty_print=True) - else: - return xml