diff --git a/pynfe/entidades/certificado.py b/pynfe/entidades/certificado.py index 507c754..2f7fe67 100644 --- a/pynfe/entidades/certificado.py +++ b/pynfe/entidades/certificado.py @@ -40,7 +40,7 @@ class CertificadoA1(Certificado): caminho_cert = caminho_cert or os.path.join(self.pasta_temporaria, self.arquivo_cert) # Lendo o arquivo pfx no formato pkcs12 como binario - pkcs12 = crypto.load_pkcs12(file(self.caminho_arquivo, 'rb').read(), senha) + pkcs12 = crypto.load_pkcs12(open(self.caminho_arquivo, 'rb').read(), senha) # Retorna a string decodificado da chave privada key_str = crypto.dump_privatekey(crypto.FILETYPE_PEM, pkcs12.get_privatekey()) @@ -49,10 +49,10 @@ class CertificadoA1(Certificado): cert_str = crypto.dump_certificate(crypto.FILETYPE_PEM, pkcs12.get_certificate()) # Gravando a string no dicso - file(caminho_cert, 'wb').write(cert_str) + open(caminho_cert, 'wb').write(cert_str) # Gravando a string no dicso - file(caminho_chave, 'wb').write(key_str) + open(caminho_chave, 'wb').write(key_str) return caminho_chave, caminho_cert diff --git a/pynfe/processamento/assinatura.py b/pynfe/processamento/assinatura.py index d2c5a7e..3efbc96 100644 --- a/pynfe/processamento/assinatura.py +++ b/pynfe/processamento/assinatura.py @@ -1,7 +1,11 @@ # -*- coding: utf-8 -*- +import signxml +from pynfe.utils import etree +from pynfe.entidades.certificado import CertificadoA1 from pynfe.utils.flags import NAMESPACE_NFE, NAMESPACE_SIG + class Assinatura(object): """Classe abstrata responsavel por definir os metodos e logica das classes de assinatura digital.""" @@ -18,19 +22,17 @@ class Assinatura(object): pass class AssinaturaA1(Assinatura): - """Classe abstrata responsavel por efetuar a assinatura do certificado + """Classe responsavel por efetuar a assinatura do certificado digital no XML informado.""" def assinar_nfe(self, xml): - #from lxml import etree - import signxml - from signxml import xmldsig - - cert = open("cert.pem").read() - key = open("key.pem", "rb").read() + arquivo_cert = CertificadoA1("cert.pfx") + key, cert = arquivo_cert.separar_arquivo('12345678') + #cert = open("cert.pem").read() + #key = open("key.pem", "rb").read() root = etree.parse(xml).getroot() - signer = xmldsig(root, digest_algorithm="sha1") + signer = signxml.xmldsig(root, digest_algorithm="sha1") signer.sign(method=signxml.methods.enveloped, key=key, cert=cert, algorithm="rsa-sha1", c14n_algorithm='http://www.w3.org/TR/2001/REC-xml-c14n-20010315', reference_uri='#NFe41150715380524000122651010000000271333611649') diff --git a/pynfe/utils/__init__.py b/pynfe/utils/__init__.py index 17ea422..3b44f15 100644 --- a/pynfe/utils/__init__.py +++ b/pynfe/utils/__init__.py @@ -7,37 +7,21 @@ import unicodedata try: from lxml import etree except ImportError: + # Instalacao normal do ElementTree try: - # Python 2.5 - cElementTree - import xml.etree.cElementTree as etree + import xml.etree.ElementTree as etree except ImportError: - try: - # Python 2.5 - ElementTree - import xml.etree.ElementTree as etree - except ImportError: - try: - # Instalacao normal do cElementTree - import cElementTree as etree - except ImportError: - try: - # Instalacao normal do ElementTree - import elementtree.ElementTree as etree - except ImportError: - raise Exception('Falhou ao importar lxml/ElementTree') + raise Exception('Falhou ao importar lxml/ElementTree') try: - from cStringIO import StringIO + from StringIO import StringIO except ImportError: - try: - from StringIO import StringIO - except ImportError: - from io import StringIO + from io import StringIO try: import flags except ImportError: - pass - #raise Exception('Falhou ao importar flags') + raise Exception('Falhou ao importar flags') # from geraldo.utils import memoize