Hi everybody i'm trying create and sign xml from json data. I retrieve my object as json from this i create the xml with my xsd file.
when i sign at the output file i can't see (this is empty on my xml) and the nodes's sign is inline. How i can get same result as example?
PS: I've got xsd schema for the the sign but i don't kown how tp use with signxml
part where i sign xml
key = open('../../firma/privatekey.pem','rb').read()
cert = open('../../firma/Octav.crs','rb').read()
print(cert)
signed_root=XMLSigner(method=signxml.methods.enveloped, signature_algorithm='rsa-sha256', digest_algorithm='sha256', c14n_algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments").sign(root, key=key, cert=cert)
ET.ElementTree(signed_root).write('factura_firmada.xml')
code complete with libraries
import xmlschema
from xml.etree.cElementTree import ElementTree
import json
from lxml import etree
import xml.etree.ElementTree as ET
from signxml import XMLSigner, XMLVerifier
import signxml
dataJSON={
'@xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'@xmlns:default': 'http://www.w3.org/2000/09/xmldsig#',
'@xsi:noNamespaceSchemaLocation': 'facturaElectronicaCompraVenta.xsd',
'cabecera':
{'nitEmisor': 1003579028,
'razonSocialEmisor': 'Carlos Loza',
'municipio': 'La Paz',
'telefono': '2846005',
'numeroFactura': 1,
'cuf': '44AAEC00DBD34C819B4D7AFD5F91900D3A059E06A467A75AC82F24C74',
'cufd': 'BQUE+QytqQUDBKVUFOSVRPQkxVRFZNVFVJBMDAwMDAwM',
'codigoSucursal': 0,
'direccion': 'AV. JORGE LOPEZ #123',
'codigoPuntoVenta': {'@xsi:nil': 'true'},
'fechaEmision': '2021-10-07T09:01:24.178',
'nombreRazonSocial': 'Mi razon social',
'codigoTipoDocumentoIdentidad': 1,
'numeroDocumento': '5115889',
'complemento': {'@xsi:nil': 'true'},
'codigoCliente': '51158891',
'codigoMetodoPago': 1,
'numeroTarjeta': {'@xsi:nil': 'true'},
'montoTotal': 99,
'montoTotalSujetoIva': 99,
'codigoMoneda': 1,
'tipoCambio': 1,
'montoTotalMoneda': 99,
'montoGiftCard': {'@xsi:nil': 'true'},
'descuentoAdicional': 1,
'codigoExcepcion': {'@xsi:nil': 'true'},
'cafc': {'@xsi:nil': 'true'},
'leyenda': 'Ley N° 453: Tienes derecho a recibir información sobre las características y contenidos de los\n servicios que utilices.\n ',
'usuario': 'pperez',
'codigoDocumentoSector': 1},
'detalle': [
{'actividadEconomica': '451010',
'codigoProductoSin': 49111,
'codigoProducto':
'JN-131231',
'descripcion':
'JUGO DE NARANJA EN VASO',
'cantidad': 1,
'unidadMedida': 1,
'precioUnitario': 100,
'montoDescuento': 0,
'subTotal': 100,
'numeroSerie': '124548',
'numeroImei': '545454'}],
}
dati=json.dumps(dataJSON)
xml=xmlschema.from_json(dati, schema=schema)
ElementTree(xml).write('myxml.xml')
#Now i open the file to sign but I don't sure if correct open file write or if i can sign without save before file
el= ET.parse('myxml.xml')
root=el.getroot()
key = open('../../firma/privatekey.pem','rb').read()
cert = open('../../firma/Octav.crs','rb').read()
print(cert)
signed_root=XMLSigner(method=signxml.methods.enveloped, signature_algorithm='rsa-sha256', digest_algorithm='sha256', c14n_algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments").sign(root, key=key, cert=cert)
ET.ElementTree(signed_root).write('factura_firmada.xml')
print('file writed!')
this is the result i need
<facturaElectronicaCompraVenta
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="facturaElectronicaCompraVenta.xsd">
<cabecera>
<nitEmisor>1003579028</nitEmisor>
<razonSocialEmisor>Carlos Loza</razonSocialEmisor>
<municipio>La Paz</municipio>
<telefono>2846005</telefono>
<numeroFactura>1</numeroFactura>
<cuf>44AAEC00DBD34C819B4D7AFD5F91900D3A059E06A467A75AC82F24C74</cuf>
<cufd>BQUE+QytqQUDBKVUFOSVRPQkxVRFZNVFVJBMDAwMDAwM</cufd>
<codigoSucursal>0</codigoSucursal>
<direccion>AV. JORGE LOPEZ #123</direccion>
<codigoPuntoVenta xsi:nil="true"/>
<fechaEmision>2021-10-07T09:01:24.178</fechaEmision>
<nombreRazonSocial>Mi razon social</nombreRazonSocial>
<codigoTipoDocumentoIdentidad>1</codigoTipoDocumentoIdentidad>
<numeroDocumento>5115889</numeroDocumento>
<complemento xsi:nil="true"/>
<codigoCliente>51158891</codigoCliente>
<codigoMetodoPago>1</codigoMetodoPago>
<numeroTarjeta xsi:nil="true"/>
<montoTotal>99</montoTotal>
<montoTotalSujetoIva>99</montoTotalSujetoIva>
<codigoMoneda>1</codigoMoneda>
<tipoCambio>1</tipoCambio>
<montoTotalMoneda>99</montoTotalMoneda>
<montoGiftCard xsi:nil="true"/>
<descuentoAdicional>1</descuentoAdicional>
<codigoExcepcion xsi:nil="true"/>
<cafc xsi:nil="true"/>
<leyenda>Ley N° 453: Tienes derecho a recibir información sobre las características y contenidos de los servicios que utilices. </leyenda>
<usuario>pperez</usuario>
<codigoDocumentoSector>1</codigoDocumentoSector>
</cabecera>
<detalle>
<actividadEconomica>451010</actividadEconomica>
<codigoProductoSin>49111</codigoProductoSin>
<codigoProducto>JN-131231</codigoProducto>
<descripcion>JUGO DE NARANJA EN VASO</descripcion>
<cantidad>1</cantidad>
<unidadMedida>1</unidadMedida>
<precioUnitario>100</precioUnitario>
<montoDescuento>0</montoDescuento>
<subTotal>100</subTotal>
<numeroSerie>124548</numeroSerie>
<numeroImei>545454</numeroImei>
</detalle>
<Signature
xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<DigestValue>WmFvnKBZIr9D37PaYuxM3aoXVu9nDZT+2MI1I+RUh8s=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue> itb123fGGhh12DpFDFas34ASDAPpSSSSadasDasAS1smkRsj5ksdjasd8asdkasjd8asdkas8asdk21v a1qf+kBKLwF39mj+5zKo1qf+kBKLD42qD/+yxSMMS6DM5SywPxO1oyjnSZtObIe/45fdS4sE9+aNOn UncYUlSDAPpSSSSadasgIMWwlX2XMJ4SDAPpSSSSadas6qihJt/3dEIdta1RETSDAPpSSSSadas9S2W ALbT3VV8pjLqikVLcSDAPpSSSSadaseCKG8abcdssM0Wm8p+5grNNpSDAPpSSSSadasy4TvT4C3xS 70zSbKWeBUUglRcU8FECEcacu+UJaBCgRW0S3Q== </SignatureValue>
<KeyInfo>
<X509Data>
<X509Certificate> TIIEuzCCZ7OgZIZsdZsDSZDSsdZsdSZSDZSDZKoZIhvcNZQEZBQZgbUxCzZZBgNVBZYTZkZ TIIEuzCCZ7OgZIZsdZsDSZDSsdZsdSZSDZSDZKoZIhvcNZQEZBQZgbUxCzZZBgNVBZYTZkZ bnRhbTlsbGZZZ1dVzdG9zLTdvYi5ibzCCZSIDQYZKoZIhvcNZQEBBQZDgasdaACZQoCggEB TIIEuzCCZ7OgZIZsdZsDSZDSsdZsdSZSDZSDZKoZIhvcNZQEZBQZgbUxCzZZBgNVBZYTZkZ bnRhbTlsbGZZZ1dVzdG9zLTdvYi5ibzCCZSIDQYZKoZIhvcNZQEBBQZDgasdaACZQoCggEB TIIEuzCCZ7OgZIZsdZsDSZDSsdZsdSZSDZSDZKoZIhvcNZQEZBQZgbUxCzZZBgNVBZYTZkZ bnRhbTlsbGZZZ1dVzdG9zLTdvYi5ibzCCZSIDQYZKoZIhvcNZQEBBQZDgasdaACZQoCggEB k/IsZZEBEETRTEQYDVQQZEoxTDZzNTc5TDI4TTITZYZKoZIhvcNZQkBZiNhcT5vbGQucXVp bnRhbTlsbGZZZ1dVzdG9zLTdvYi5ibzCCZSIDQYZKoZIhvcNZQEBBQZDgasdaACZQoCggEB bnRhbTlsbGZZZ1dVzdG9zLTdvYi5ibzCCZSIDQYZKoZIhvcNZQEBBQZDgasdaACZQoCggEB kTIh+T9dIRZ9GghiSZbP2Zk082ycgic2ZepdZLu7p2LEUZUhTXSXGZQkO4o/ZNZZk0ZQKgi bnRhbTlsbGZZZ1dVzdG9zLTdvYi5ibzCCZSIDQYZKoZIhvcNZQEBBQZDgasdaACZQoCggEB V8ZVyTkIDRCTcBYZO4ZZ8oV1UTbQEiDZZOYTZDHT2ZRy+kS+OzCdpZZKZZyds0ZClCZRlTg bnRhbTlsbGZZZ1dVzdG9zLTdvYi5ibzCCZSIDQYZKoZIhvcNZQEBBQZDgasdaACZQoCggEB Z1UdDQEZIE8DZuBgNVHREEZzZlhiNhcT5vZZZXVpbnRhbTlsbGZZZ1dVzdG9zLTdvYi5i7Z bzBRBgNVHR8ESZBITEZgRKBChkBodHRczovL2Rlc2ZycT9sbG8uYRzZIuZ29iLTZvL2Rlc2 bnRhbTlsbGZZZ1dVzdG9zLTdvYi5ibzCCZSIDQYZKoZIhvcNZQEBBQZDgasdaACZQoCggEB NRrZKQl3YhKZKRSZyeegVZIiXlYeEyOe5qdn3XdyTCDkz4bTn02KbcsnZP7oVTqsodBSd0B ptrxqoqeZHhGBzEl5/vxDS/Sx4eO7ZXd1T4HtZntETdsZXZuPZZt4Vr57R/hqdZeTkk7uO9 bnRhbTlsbGZZZ1dVzdG9zLTdvYi5ibzCCZSIDQYZKoZIhvcNZQEBBQZDgasdaACZQoCggEB YB+PC1LO7gnh4Zz9UsYb0YBG </X509Certificate>
</X509Data>
</KeyInfo>
</Signature>
</facturaElectronicaCompraVenta>
this my result
<facturaElectronicaCompraVenta xmlns:ns1="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="facturaElectronicaCompraVenta.xsd">
<cabecera>
<nitEmisor>1003579028</nitEmisor>
<razonSocialEmisor>Carlos Loza</razonSocialEmisor>
<municipio>La Paz</municipio>
<telefono>2846005</telefono>
<numeroFactura>1</numeroFactura>
<cuf>44AAEC00DBD34C819B4D7AFD5F91900D3A059E06A467A75AC82F24C74</cuf>
<cufd>BQUE+QytqQUDBKVUFOSVRPQkxVRFZNVFVJBMDAwMDAwM</cufd>
<codigoSucursal>0</codigoSucursal>
<direccion>AV. JORGE LOPEZ #123</direccion>
<codigoPuntoVenta xsi:nil="true" />
<fechaEmision>2021-10-07T09:01:24.178</fechaEmision>
<nombreRazonSocial>Mi razon social</nombreRazonSocial>
<codigoTipoDocumentoIdentidad>1</codigoTipoDocumentoIdentidad>
<numeroDocumento>5115889</numeroDocumento>
<complemento xsi:nil="true" />
<codigoCliente>51158891</codigoCliente>
<codigoMetodoPago>1</codigoMetodoPago>
<numeroTarjeta xsi:nil="true" />
<montoTotal>99</montoTotal>
<montoTotalSujetoIva>99</montoTotalSujetoIva>
<codigoMoneda>1</codigoMoneda>
<tipoCambio>1</tipoCambio>
<montoTotalMoneda>99</montoTotalMoneda>
<montoGiftCard xsi:nil="true" />
<descuentoAdicional>1</descuentoAdicional>
<codigoExcepcion xsi:nil="true" />
<cafc xsi:nil="true" />
<leyenda>Ley N° 453: Tienes derecho a recibir información sobre las características y contenidos de los
servicios que utilices.
</leyenda>
<usuario>pperez</usuario>
<codigoDocumentoSector>1</codigoDocumentoSector>
</cabecera>
<detalle>
<actividadEconomica>451010</actividadEconomica>
<codigoProductoSin>49111</codigoProductoSin>
<codigoProducto>JN-131231</codigoProducto>
<descripcion>JUGO DE NARANJA EN VASO</descripcion>
<cantidad>1</cantidad>
<unidadMedida>1</unidadMedida>
<precioUnitario>100</precioUnitario>
<montoDescuento>0</montoDescuento>
<subTotal>100</subTotal>
<numeroSerie>124548</numeroSerie>
<numeroImei>545454</numeroImei>
</detalle>
<ns1:Signature><ns1:SignedInfo><ns1:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments" /><ns1:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" /><ns1:Reference URI=""><ns1:Transforms><ns1:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /><ns1:Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments" /></ns1:Transforms><ns1:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" /><ns1:DigestValue>lHmci64/8XWGE3+IFjX4yeeBcnPP2KyZl6Q41MYAwFw=</ns1:DigestValue></ns1:Reference></ns1:SignedInfo><ns1:SignatureValue>WA0zeIG5E+OnPlTyGhvz0kd0RB+zCtsZUj7OkuyZduFrEnKEZGfvaRNldTCqadfeqhHqOPjCILZHEqGCN9cU90+pmnsy6nFt5jxr8cN5nAjwB9h/HMlxkLtfTG/xwskWbc22gEIiiyWLsQi2RbwIo4EKkqHXv5l1xghO74Wq3zvCv12OWaFU2ZkrJ7RyL8L0LlR5bigZ/oKtdRS5Am6vgpuVClf6lFEgeSATkY5A4DgiQ9z0ERBMc07t4Q6VAbxzFyKoFV0D9UsUjr6vZ/mS6jr3Ak3i/8IiaLrQJd9G3Qw5DqiotNWNGrQ9i5XoEPN2+cVvHMUxHe6nrmZeDHVMvw==</ns1:SignatureValue><ns1:KeyInfo><ns1:X509Data /></ns1:KeyInfo></ns1:Signature></facturaElectronicaCompraVenta>
The problem is your certificate, your certificate and key should contain the following
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.