Browse Source

mudança de assinatura

pull/1/head
leotada 11 years ago
parent
commit
68517e6e4c
  1. 6
      pynfe/entidades/certificado.py
  2. 18
      pynfe/processamento/assinatura.py
  3. 28
      pynfe/utils/__init__.py

6
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

18
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')

28
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

Loading…
Cancel
Save