0

Precisa salvar apenas a tag assinatura de um xml. O método para assinatura é este:

private void assinarMdfe(String tipo, XMLSignatureFactory fac,
        ArrayList<Transform> transformList, PrivateKey privateKey,
        KeyInfo ki, Document document, int index) throws Exception {
    NodeList elements = null;
    elements = document.getElementsByTagName("infMDFe");
    org.w3c.dom.Element el = (org.w3c.dom.Element) elements.item(index);
    el.setIdAttribute("Id", true);
    String id = el.getAttribute("Id");

    Reference ref = fac.newReference("#" + id,
            fac.newDigestMethod(DigestMethod.SHA1, null), transformList,
            null, null);

    SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(
            CanonicalizationMethod.INCLUSIVE,
            (C14NMethodParameterSpec) null), fac
            .newSignatureMethod(SignatureMethod.RSA_SHA1, null),
            Collections.singletonList(ref));

    XMLSignature signature = fac.newXMLSignature(si, ki);

    DOMSignContext dsc = new DOMSignContext(privateKey,
            document.getDocumentElement().getElementsByTagName(tipo).item(index));
    signature.sign(dsc);

}

Gera um xml parecido com este:

<enviMDFe xmlns="http://www.portalfiscal.inf.br/mdfe" versao="1.00">
<idLote>000000000000001</idLote>
<MDFe xmlns="http://www.portalfiscal.inf.br/mdfe">
    <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/2000/09/xmldsig#rsa-sha1"/>
            <Reference URI="#MDFe31170922257352000160580010000000011442411352">
                <Transforms>
                    <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
                    <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
                </Transforms>
                <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
                <DigestValue>mUCRQEgobqD4Tx/lvnQF4HPSOWU=</DigestValue>
            </Reference>
        </SignedInfo>
        <SignatureValue>DtPdsuhQgDxNsoM/YYir7+jIFxkkx2HE6LEmRx13LKzcR2g0crOG7J6dR615xczb4+APBzepHTJrp7Or4TFX8NI1BTXMRerjsv+waOlSxvjFnEKHJvsDlYxTYqz23fgK5YylVZcoYRHfbETwO36ejPO3X4xagrpR67BhtCB7NoOZ8NsPv5auQ1FP/lErlKPsb2ohnvmwTgfLPUwyHRwZM8jDpbk3eigm/pr2UxbyMK3rYRviQ1FBapXijfysrhyzKAH8WYOclyze6/UjG7qbVIrPP6JvzAq5hVSeqsNRyeEdmPxwlWi31Ph/3foki/YlVUHlKNdySpTuIoTmrd2GsQ==</SignatureValue>
        <KeyInfo>
            <X509Data>
                <X509Certificate>MIIH9zCCBd+gAwIBAgIIT0iI2i7aUccwDQYJKoZIhvcNAQELBQAwcDELMAkGA1UEBhMCQlIxEzARBgNVBAoTCklDUC1CcmFzaWwxNjA0BgNVBAsTLVNlY3JldGFyaWEgZGEgUmVjZWl0YSBGZWRlcmFsIGRvIEJyYXNpbCAtIFJGQjEUMBIGA1UEAxMLQUMgTElOSyBSRkIwHhcNMTcwNTEwMjAzMzMwWhcNMTgwNTEwMjAzMzMwWjCB5jELMAkGA1UEBhMCQlIxCzAJBgNVBAgTAk1HMRAwDgYDVQQHEwdDTEFVRElPMRMwEQYDVQQKEwpJQ1AtQnJhc2lsMTYwNAYDVQQLEy1TZWNyZXRhcmlhIGRhIFJlY2VpdGEgRmVkZXJhbCBkbyBCcmFzaWwgLSBSRkIxFjAUBgNVBAsTDVJGQiBlLUNOUEogQTExEDAOBgNVBAsTB0FSIExJTksxQTA/BgNVBAMTOE1BUklOQSBERSBQQVVMQSBBTFZFUyBDUEYgMDc4OTIwOTY2IDcxIE1FOjIyMjU3MzUyMDAwMTYwMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiHahGqH6w82HfTo8Dwo+E1sXjaNfvByXweLAti9rNs5histVfU0aci3AZEVt+jYAoSR1qql9oex55jdNQrxMBgA97Yh5UVOa1Ud8B8rxDyoeB+iuLT5NSH48HQQbk05Bfynw5lhrgdHfE8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kGBWBMAQMDoBATDjIyMjU3MzUyMDAwMTYwoDgGBWBMAQMEoC8TLTEyMDUxOTg1MDc4OTIwOTY2NzEwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMKAXBgVgTAEDB6AOEwwwMDAwMDAwMDAwMDAwHQYDVR0lBBYwFAYIKwYBBQUHAwIGCCsGAQUFBwMEMAkGA1UdEwQCMAAwDQYJKoZIhvcNAQELBQADggIBAKyvTM03oTMXPs0KfjI6ILuLh0JE0mzj5J1TJv+z2kEtIDgn5OzkvLazcJZBqPvxs8M6YDZZuke3jrykYXHUSZhqPZpdJFu4bfuA+JMWkcs6QsH/BiNod89O/OSIpt1/GAnxoQfOU8WrPhJtrmJ7T1A6M6aoQekS1NDIEscAjityJP2bxX8krOAsJ7nb3x4nE3rCOIpiHCVVdkhHjILul9ZQRyzeTcJqTp6daOmE3nMe8btqtGnflQHquoJ83e8g3gZ+SRrGzo8Em6xdoPOkafbV7log4OnWaXCNvY2WfYnGkYa03Oeci+pisoixdlHAwdURE3+X1KaW3jauMi5/fmndoGmTQ60H/0LLfVYnoZerXYNkT1fgFwz7k1d6xEdB8Dd7Mbg+35Bf2HvHkR1p3qXhK+FnpqVIft/qUUxfJ7NUJkiIP8wxa3NCOEIiSr69b0xyAnzgoCKcEzwLD54/6pzMrDFXkuLQWDlTo07vX+mjVaT6kNh4frdIUZJ/Bdltdk8iBC9d457TwmekWRnjGtM5w1YUcr44pcSFwWJ5JzzZfljJkqbxJmLlZqpOKKWTcDxxUgxk5OWKvJ/x625CgQ5J/07QSKaOuX+W6R5wAmUdxVYMfnmtf2SwUflzmqmWVND7FaBbN6NDOUuFIMLCx7ahwZur4f1NVQGXX48H01Ng</X509Certificate>
            </X509Data>
        </KeyInfo>
    </Signature>
</MDFe>

"

Só que pego o xml asssinado com este método:

 protected String outputXML(Document doc) throws TransformerException, UnsupportedEncodingException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer trans = tf.newTransformer();
    trans.transform(new DOMSource(doc), new StreamResult(os));
    String xml = os.toString("UTF-8");
    if ((xml != null) && (!"".equals(xml))) {
        xml = xml.replaceAll("\\r\\n", "");
        xml = xml.replaceAll(" standalone=\"no\"", "");
    }
    return xml;
}

Gostaria de pegar como string apenas a tag Signature

    <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/2000/09/xmldsig#rsa-sha1"/>
            <Reference URI="#MDFe31170922257352000160580010000000011442411352">
                <Transforms>
                    <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
                    <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
                </Transforms>
                <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
                <DigestValue>mUCRQEgobqD4Tx/lvnQF4HPSOWU=</DigestValue>
            </Reference>
        </SignedInfo>
        <SignatureValue>DtPdsuhQgDxNsoM/YYir7+jIFxkkx2HE6LEmRx13LKzcR2g0crOG7J6dR615xczb4+APBzepHTJrp7Or4TFX8NI1BTXMRerjsv+waOlSxvjFnEKHJvsDlYxTYqz23fgK5YylVZcoYRHfbETwO36ejPO3X4xagrpR67BhtCB7NoOZ8NsPv5auQ1FP/lErlKPsb2ohnvmwTgfLPUwyHRwZM8jDpbk3eigm/pr2UxbyMK3rYRviQ1FBapXijfysrhyzKAH8WYOclyze6/UjG7qbVIrPP6JvzAq5hVSeqsNRyeEdmPxwlWi31Ph/3foki/YlVUHlKNdySpTuIoTmrd2GsQ==</SignatureValue>
        <KeyInfo>
            <X509Data>
                <X509Certificate>MIIH9zCCBd+gAwIBAgIIT0iI2i7aUccwDQYJKoZIhvcNAQELBQAwcDELMAkGA1UEBhMCQlIxEzARBgNVBAoTCklDUC1CcmFzaWwxNjA0BgNVBAsTLVNlY3JldGFyaWEgZ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eTcJqTp6daOmE3nMe8btqtGnflQHquoJ83e8g3gZ+SRrGzo8Em6xdoPOkafbV7log4OnWaXCNvY2WfYnGkYa03Oeci+pisoixdlHAwdURE3+X1KaW3jauMi5/fmndoGmTQ60H/0LLfVYnoZerXYNkT1fgFwz7k1d6xEdB8Dd7Mbg+35Bf2HvHkR1p3qXhK+FnpqVIft/qUUxfJ7NUJkiIP8wxa3NCOEIiSr69b0xyAnzgoCKcEzwLD54/6pzMrDFXkuLQWDlTo07vX+mjVaT6kNh4frdIUZJ/Bdltdk8iBC9d457TwmekWRnjGtM5w1YUcr44pcSFwWJ5JzzZfljJkqbxJmLlZqpOKKWTcDxxUgxk5OWKvJ/x625CgQ5J/07QSKaOuX+W6R5wAmUdxVYMfnmtf2SwUflzmqmWVND7FaBbN6NDOUuFIMLCx7ahwZur4f1NVQGXX48H01Ng</X509Certificate>
            </X509Data>
        </KeyInfo>
    </Signature>

Seria possível pegar o valor da assinatura em algumas das etapas?

2 Respostas 2

0

Pode obter essa informação utilizando:

Node signature = document.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature").item(0);

O objeto document é o mesmo que está passando como parâmetro no seu método assinarMdfe.

Basicamente, o método getElementsByTagNameNS irá procurar no document por uma TAG Signature que possua um XMLNS igual há http://www.w3.org/2000/09/xmldsig# (representado pela constante: XMLSignature.XMLNS).

0

Resolvi deste jeito

   protected String outputXML(Document doc) throws TransformerException, UnsupportedEncodingException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer trans = tf.newTransformer();
    trans.transform(new DOMSource(doc), new StreamResult(os));
    String xml = os.toString("UTF-8");
    if ((xml != null) && (!"".equals(xml))) {
        xml = xml.replaceAll("\\r\\n", "");
        xml = xml.replaceAll(" standalone=\"no\"", "");
    }
    capturaTagAssinatura(doc);
    return xml;
}

protected String capturaTagAssinatura(Document doc) throws TransformerConfigurationException, TransformerException, UnsupportedEncodingException {
        NodeList elementsByTagNameNS = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
        if (elementsByTagNameNS.getLength() > 0) {
            Node item = elementsByTagNameNS.item(0);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer trans = tf.newTransformer();
            trans.transform(new DOMSource(item), new StreamResult(os));
            assinatura = os.toString("UTF-8");
            if ((assinatura != null) && (!"".equals(assinatura))) {
                assinatura = assinatura.replaceAll("\\r\\n", "");
                assinatura = assinatura.replaceAll(" standalone=\"no\"", "");
                assinatura = assinatura.replaceAll(" standalone=\"no\"", "");
                assinatura = assinatura.replaceAll("\\<\\?xml(.+?)\\?\\>", "").trim();
            }
            return assinatura;
        }
        return assinatura;
    }

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .