4. <SignedData> 5. .............. 6. </SignedData> 7. </Signature> 8. ..............
在 Enveloping 格式的数字签名中,待签名的 XML 内容需要通过 URI 或者 Transform 进行引用。
3.2.1 生成签名
1. // Create XMLObject refering to Simon's payment info 2. DocumentBuilderFactory dbf = 3. DocumentBuilderFactory.newInstance(); 4. dbf.setNamespaceAware(true); 5. Document origDoc = 6. dbf.newDocumentBuilder().parse(new FileInputStream(inputFile)); 7. Element docEle = origDoc.getDocumentElement(); 8. Node simonPayment = 9. docEle.getElementsByTagName("PaymentInfo").item(0); 10. XMLStructure content = new DOMStructure(simonPayment); 11. XMLObject xmlObj = 12. fac.newXMLObject(Collections.singletonList(content), 13. "SimonPayment", null, null); 14. 15. // Create the reference to element to be signed 16. Reference ref = fac.newReference("#SimonPayment", fac.newDigestMethod(DigestMethod.SHA1, null));
...... ......
17. XMLSignature signature = fac.newXMLSignature(si, ki, Collections.singletonList(xmlObj), null, null);
为了引用待签名的内容,首先查找到 Simon 的支付记录对应的 DOM 元素,使用 XMLStructure 对其进行包装,然后生成 XMLObject ,并为其指定 id 为 "SimonPayment"。随后在创建 Referenc 的时候,同样指定引用 id 为 "SimonPayment"。在创建 XMLSignature 对象的时候将待签名的 XMLObject 作为参数,这些 XMLObject 包含的 XML 内容将会成为 <Signature> 元素的子元素,从而创建出 Enveloping 格式的签名。程序的其他部分与生成 Enveloped 格式的数字签名相同。
签名之后生成的 <Signature> 元素如下:
1. <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> 2. <SignedInfo> 3. <CanonicalizationMethod 4. Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"/> 5. <SignatureMethod
上一篇:监控Proxool连接池的活动连接变化情况
下一篇:探索Java应用程序的安全需求
|