http://www.w3.org/TR/2001/REC-xml-c14n-20010315
Libraries used :
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<AppendHere id="here">
</AppendHere>
<Comments>The price is $30</Comments>
</Root>
<?xml version="1.0" encoding="UTF-8"?><Root>
<AppendHere id="here">
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#hmac-sha1"/>
<ds:Reference URI="#xpointer(/)">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>Q4KJ3D+IGMUVeNj/OA10MWlNBgA=</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>xwZLw4/qSMGyht8/SALD40tmP+s=</ds:SignatureValue>
</ds:Signature></AppendHere>
<Comments>The price is $30</Comments>
</Root>
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
factory.setValidating(false);
factory.setIgnoringElementContentWhitespace(false);
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(inStream);
is.setEncoding("UTF-8");
Document document = builder.parse(is);
org.apache.xml.security.Init.init();
Canonicalizer c14n = Canonicalizer.getInstance("http://www.w3.org/TR/2001/REC-xml-c14n-20010315");
byte outputBytes[] = c14n.canonicalizeSubtree(document);
document= builder.parse(new ByteArrayInputStream(outputBytes));
final Charset charSet = Charset.forName("ASCII");
final SecretKeySpec signingKey = new javax.crypto.spec.SecretKeySpec(charSet.encode(password).array(), "HmacSHA1");
org.apache.xml.security.signature.XMLSignature sig = new org.apache.xml.security.signature.XMLSignature(document,"",org.apache.xml.security.signature.XMLSignature.ALGO_ID_MAC_HMAC_SHA1);
document.getElementsByTagName("AppendHere").item(0).appendChild(sig.getElement());
Transforms transforms = new Transforms(document);
transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
sig.addDocument("#xpointer(/)", transforms, org.apache.xml.security.utils.Constants.ALGO_ID_DIGEST_SHA1);
sig.sign(signingKey);
<AppendHere>
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"></ds:CanonicalizationMethod>
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#hmac-sha1"></ds:SignatureMethod>
<ds:Reference URI="#xpointer(/)">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"></ds:Transform>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></ds:DigestMethod>
<ds:DigestValue>LB/n8Ir8iJlrMyJkL1Ue5TWhJwk=</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>inmGAJ4Wzej+Ir/f835EIeMAPgk=</ds:SignatureValue>
</ds:Signature>
</AppendHere>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
7 | |
5 | |
5 | |
5 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 |