-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
59 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# -*- coding: utf-8 -*- | ||
# © 2016 Danimar Ribeiro, Trustcode | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
|
||
import signxml | ||
from lxml import etree | ||
from pytrustnfe.certificado import extract_cert_and_key_from_pfx | ||
from signxml import XMLSigner | ||
|
||
|
||
class Assinatura(object): | ||
|
||
def __init__(self, arquivo, senha): | ||
self.arquivo = arquivo | ||
self.senha = senha | ||
|
||
def assina_xml(self, xml_element, reference): | ||
cert, key = extract_cert_and_key_from_pfx(self.arquivo, self.senha) | ||
|
||
for element in xml_element.iter("*"): | ||
if element.text is not None and not element.text.strip(): | ||
element.text = None | ||
|
||
signer = XMLSigner( | ||
method=signxml.methods.enveloped, signature_algorithm="rsa-sha1", | ||
digest_algorithm='sha1', | ||
c14n_algorithm='http://www.w3.org/TR/2001/REC-xml-c14n-20010315') | ||
|
||
ns = {} | ||
ns[None] = signer.namespaces['ds'] | ||
signer.namespaces = ns | ||
|
||
ref_uri = ('#%s' % reference) if reference else None | ||
signed_root = signer.sign( | ||
xml_element, key=key.encode(), cert=cert.encode(), | ||
reference_uri=ref_uri) | ||
if reference: | ||
element_signed = signed_root.find(".//*[@Id='%s']" % reference) | ||
signature = signed_root.find(".//*[@URI='#%s']" % reference).getparent().getparent() | ||
|
||
if element_signed is not None and signature is not None: | ||
parent = element_signed.getparent() | ||
parent.append(signature) | ||
return etree.tostring(signed_root, encoding=str) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters