Element headerElement = null; NodeList nodes = doc.getElementsByTagNameNS ("http://schemas.xmlsoap.org/soap/envelope/","Header"); headerElement = (Element)nodes.item(0); // Create an XMLSignature instance Element soapsec = doc.createElementNS("", "SOAP-SEC:Signature"); soapsec.setAttributeNS("", "mustUnderstand", "1"); soapsec.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:SOAP-SEC", "http://www.schemas.xmlsoap.org/soap/security/2000-10"); Transforms transforms = new Transforms(doc); transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE); transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS); XMLSignature sig = new XMLSignature(doc,"",XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1); soapsec.appendChild(sig.getElement()); headerElement.appendChild(soapsec); // Specify the transforms sig.addDocument("#Body", transforms, org.apache.xml.security.utils.Constants.ALGO_ID_DIGEST_SHA1); // Get the certificate and send for signing it will do what you required. sig.sign(privateKey);
headerElement.appendChild(sig.getElement()); Transforms transforms = new Transforms(doc); transforms.addTransform("http://www.w3.org/2000/09/xmldsig#enveloped-signature");
headerElement.appendChild(sig.getElement()); Transforms transforms = new Transforms(doc); transforms.addTransform("http://www.w3.org/2000/09/xmldsig#enveloped-signature");
// Assume "document" is the Document you want to sign, and that you have already have the cert and the key // Construct the signature and add the necessary transforms, etc. XMLSignature signature = new XMLSignature(document, null, XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1); final Transforms transforms = new Transforms(document); transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE); transforms.addTransform(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS); signature.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1); // Now insert the signature as the last child of the outermost node document.getDocumentElement().appendChild(signature.getElement()); // Finally, actually sign the document. signature.addKeyInfo(x509Certificate); signature.addKeyInfo(x509Certificate.getPublicKey()); signature.sign(privateKey);
sig.addKeyInfo(cert.getPublicKey()); sig.sign(privateKey); doc.getDocumentElement().appendChild(sig.getElement()); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); outputStream.write(Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS).canonicalizeSubtree(doc));