Sei sulla pagina 1di 2

www.impuestos.gob.

bo
SISTEMA DE FACTURACIÓN
ELECTRÓNICA
1

FIRMADO DE DOCUMENTO

Ejemplo de firmado XML, basado en librería en java que implementa


funciones para la firma digital.
fuente: https://gitlab.geo.gob.bo/firmador_estatal/firmador-libreria/tree/
digest_sha256
package bo.gob.sin.firmaxml.ejemplo;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.xml.security.algorithms.MessageDigestAlgorithm;
import org.apache.xml.security.exceptions.XMLSecurityException;
import org.apache.xml.security.keys.KeyInfo;
import org.apache.xml.security.signature.XMLSignature;
import org.apache.xml.security.transforms.Transforms;
import org.apache.xml.security.utils.Constants;
import org.apache.xml.security.utils.ElementProxy;
import org.apache.xml.security.utils.XMLUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class Ejemplo {


/**
* @param datos - array de bytes correspondiente al archivo XML
que se va a firmar
* @param priv - llave privada para el firmado
* @param cert - certificado emitido por la ADSIB asociado a la
llave publica
* @return array de bytes correspondiente al archivo XML con la
firma enveloped
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
* @throws XMLSecurityException
*/
public byte[] firmarDsig(byte[] datos, PrivateKey priv,
X509Certificate... cert) throws ParserConfigurationException, IOException,
SAXException, XMLSecurityException {

ElementProxy.setDefaultPrefix(Constants.SignatureSpecNS, “”);

LÍNEA GRATUITA DE CONSULTAS TRIBUTARIAS 800 10 34 44


www.impuestos.gob

SISTEMA DE FACTURACIÓN
ELECTRÓNICA
2

Document documento = leerXML(datos);

Element root = (Element) documento.getFirstChild();


documento.setXmlStandalone(false);

XMLSignature signature = new XMLSignature(documento, null,


XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256);

root.appendChild(signature.getElement());

Transforms transforms = new Transforms(documento);


transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
signature.addDocument(“”, transforms, MessageDigestAlgorithm.ALGO_
ID_DIGEST_SHA256);
if (cert != null) {
signature.addKeyInfo(cert[0]);
KeyInfo ki = signature.getKeyInfo();
ki.itemX509Data(0).addSubjectName(cert[0].
getSubjectX500Principal().getName());
ki.itemX509Data(0).addIssuerSerial(cert[0].
getIssuerX500Principal().getName(), cert[0].getSerialNumber());
}
signature.sign(priv);

ByteArrayOutputStream baos = new ByteArrayOutputStream();


XMLUtils.outputDOM(documento, baos, true);

return baos.toString().getBytes();
}

private Document leerXML(byte datos[]) throws
ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.
newInstance();
DocumentBuilder builder;

factory.setNamespaceAware(true);
builder = factory.newDocumentBuilder();

return builder.parse(new ByteArrayInputStream(datos));


}
}

www.impuestos.gob.bo La Paz 21 de noviembre del 2018

Potrebbero piacerti anche