本文整理汇总了Java中org.apache.xml.security.c14n.Canonicalizer.canonicalizeSubtree方法的典型用法代码示例。如果您正苦于以下问题:Java Canonicalizer.canonicalizeSubtree方法的具体用法?Java Canonicalizer.canonicalizeSubtree怎么用?Java Canonicalizer.canonicalizeSubtree使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.xml.security.c14n.Canonicalizer
的用法示例。
在下文中一共展示了Canonicalizer.canonicalizeSubtree方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getCanonicalizedOctetStream
import org.apache.xml.security.c14n.Canonicalizer; //导入方法依赖的package包/类
/**
* Returns getCanonicalizedOctetStream
*
* @return the canonicalization result octet stream of <code>SignedInfo</code> element
* @throws CanonicalizationException
* @throws InvalidCanonicalizerException
* @throws XMLSecurityException
*/
public byte[] getCanonicalizedOctetStream()
throws CanonicalizationException, InvalidCanonicalizerException, XMLSecurityException {
if (this.c14nizedBytes == null) {
Canonicalizer c14nizer =
Canonicalizer.getInstance(this.getCanonicalizationMethodURI());
c14nizer.setSecureValidation(isSecureValidation());
String inclusiveNamespaces = this.getInclusiveNamespaces();
if (inclusiveNamespaces == null) {
this.c14nizedBytes = c14nizer.canonicalizeSubtree(getElement());
} else {
this.c14nizedBytes = c14nizer.canonicalizeSubtree(getElement(), inclusiveNamespaces);
}
}
// make defensive copy
return this.c14nizedBytes.clone();
}
示例2: signInOctetStream
import org.apache.xml.security.c14n.Canonicalizer; //导入方法依赖的package包/类
/**
* Output the C14n stream to the given OutputStream.
* @param os
* @throws CanonicalizationException
* @throws InvalidCanonicalizerException
* @throws XMLSecurityException
*/
public void signInOctetStream(OutputStream os)
throws CanonicalizationException, InvalidCanonicalizerException, XMLSecurityException {
if (this.c14nizedBytes == null) {
Canonicalizer c14nizer =
Canonicalizer.getInstance(this.getCanonicalizationMethodURI());
c14nizer.setSecureValidation(isSecureValidation());
c14nizer.setWriter(os);
String inclusiveNamespaces = this.getInclusiveNamespaces();
if (inclusiveNamespaces == null) {
c14nizer.canonicalizeSubtree(getElement());
} else {
c14nizer.canonicalizeSubtree(getElement(), inclusiveNamespaces);
}
} else {
try {
os.write(this.c14nizedBytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
示例3: testC14n11Base
import org.apache.xml.security.c14n.Canonicalizer; //导入方法依赖的package包/类
@org.junit.Test
public void testC14n11Base() throws Exception {
DocumentBuilder documentBuilder = XMLUtils.createDocumentBuilder(true);
documentBuilder.setErrorHandler(new org.apache.xml.security.utils.IgnoreAllErrorHandler());
byte inputBytes[] = input.getBytes();
Document doc =
documentBuilder.parse(new ByteArrayInputStream(inputBytes));
Canonicalizer c14n =
Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N11_OMIT_COMMENTS);
XPathFactory xpf = XPathFactory.newInstance();
XPath xPath = xpf.newXPath();
xPath.setNamespaceContext(new DSNamespaceContext());
Node signedInfo =
(Node) xPath.evaluate("//ds:SignedInfo[1]", doc, XPathConstants.NODE);
byte[] output = c14n.canonicalizeSubtree(signedInfo);
assertEquals( new String(output, "UTF-8"), expectedResult);
}
示例4: main
import org.apache.xml.security.c14n.Canonicalizer; //导入方法依赖的package包/类
/**
* Method main
*
* @param args
* @throws Exception
*/
public static void main(String args[]) throws Exception {
org.apache.xml.security.Init.init();
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
dfactory.setNamespaceAware(true);
dfactory.setValidating(true);
DocumentBuilder documentBuilder = dfactory.newDocumentBuilder();
// this is to throw away all validation warnings
documentBuilder.setErrorHandler(new org.apache.xml.security.utils.IgnoreAllErrorHandler());
byte inputBytes[] = input.getBytes();
Document doc =
documentBuilder.parse(new ByteArrayInputStream(inputBytes));
// after playing around, we have our document now
Canonicalizer c14n = Canonicalizer.getInstance(
"http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments");
byte outputBytes[] = c14n.canonicalizeSubtree(doc);
System.out.println(new String(outputBytes));
}
示例5: toString
import org.apache.xml.security.c14n.Canonicalizer; //导入方法依赖的package包/类
private String toString (Node n) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Canonicalizer c14n = Canonicalizer.getInstance
(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
byte[] serBytes = c14n.canonicalizeSubtree(n);
baos.write(serBytes);
baos.close();
return baos.toString("UTF-8");
}
示例6: testRelativeNSbehaviour
import org.apache.xml.security.c14n.Canonicalizer; //导入方法依赖的package包/类
/**
* Note: This specification supports the recent XML plenary decision to
* deprecate relative namespace URIs as follows: implementations of XML
* canonicalization MUST report an operation failure on documents containing
* relative namespace URIs. XML canonicalization MUST NOT be implemented
* with an XML parser that converts relative URIs to absolute URIs.
*
* Implementations MUST report an operation failure on documents containing
* relative namespace URIs.
*
* @throws CanonicalizationException
* @throws FileNotFoundException
* @throws IOException
* @throws InvalidCanonicalizerException
* @throws ParserConfigurationException
* @throws SAXException
* @throws TransformerException
*/
@org.junit.Test
public void testRelativeNSbehaviour()
throws IOException, FileNotFoundException, SAXException,
ParserConfigurationException, CanonicalizationException,
InvalidCanonicalizerException, TransformerException {
//J-
String inputStr = ""
+ "<absolute:correct xmlns:absolute='http://www.absolute.org/#likeVodka'>"
+ "<relative:incorrect xmlns:relative='../cheating#away'>"
+ "</relative:incorrect>"
+ "</absolute:correct>"
+ "\n"
+ "";
//J+
DocumentBuilder db = XMLUtils.createDocumentBuilder(false);
Document doc = db.parse(new ByteArrayInputStream(inputStr.getBytes()));
boolean weCatchedTheRelativeNS = false;
try {
Canonicalizer c14n =
Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
c14n.canonicalizeSubtree(doc);
} catch (CanonicalizationException cex) {
// if we reach this point - good.
log.debug("We catched the C14nEx, that's good: " + cex.getMessage());
weCatchedTheRelativeNS = true;
}
assertTrue("We did not catch the relative namespace", weCatchedTheRelativeNS);
}
示例7: main
import org.apache.xml.security.c14n.Canonicalizer; //导入方法依赖的package包/类
/**
* Method main
*
* @param args
* @throws Exception
*/
public static void main(String args[]) throws Exception {
org.apache.xml.security.Init.init();
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
dfactory.setNamespaceAware(true);
dfactory.setValidating(true);
DocumentBuilder documentBuilder = dfactory.newDocumentBuilder();
// this is to throw away all validation warnings
documentBuilder.setErrorHandler(new org.apache.xml.security.utils.IgnoreAllErrorHandler());
byte inputBytes[] = input.getBytes();
Document doc =
documentBuilder.parse(new ByteArrayInputStream(inputBytes));
Canonicalizer c14n =
Canonicalizer
.getInstance("http://www.w3.org/TR/2001/REC-xml-c14n-20010315");
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
xpath.setNamespaceContext(new DSNamespaceContext());
String expression = "//ds:SignedInfo[1]";
Element signedInfo =
(Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
byte outputBytes[] = c14n.canonicalizeSubtree(signedInfo);
if (outputBytes != null) {
System.out.println(new String(outputBytes));
}
}
示例8: canonicalizeSubtree
import org.apache.xml.security.c14n.Canonicalizer; //导入方法依赖的package包/类
/**
* This method canonicalizes the given {@code Node}.
*
* @param canonicalizationMethod
* canonicalization method
* @param node
* {@code Node} to canonicalize
* @return array of canonicalized bytes
*/
public static byte[] canonicalizeSubtree(final String canonicalizationMethod, final Node node) {
try {
final Canonicalizer c14n = Canonicalizer.getInstance(canonicalizationMethod);
final byte[] canonicalized = c14n.canonicalizeSubtree(node);
return canonicalized;
} catch (Exception e) {
throw new DSSException(e);
}
}
示例9: onDocumentSigned
import org.apache.xml.security.c14n.Canonicalizer; //导入方法依赖的package包/类
@Override
protected void onDocumentSigned(byte[] byteArray) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new ByteArrayInputStream(byteArray));
XPathFactory f = XPathFactory.newInstance();
XPath xPath = f.newXPath();
xPath.setNamespaceContext(new Name());
Node node = (Node) xPath.evaluate("root/data[@id='data1']", doc, XPathConstants.NODE);
Init.init();
Canonicalizer c14n = Canonicalizer.getInstance("http://www.w3.org/2001/10/xml-exc-c14n#");
byte c14nBytes[] = c14n.canonicalizeSubtree(node);
Assert.assertEquals("AdGdZ+/VQVVvC9yzL4Yj8iRK33cQBiRW2UpKGMswdZQ=", Base64.encode(MessageDigest.getInstance("SHA-256").digest(c14nBytes)));
node = (Node) xPath.evaluate("root/data[@id='data2']", doc, XPathConstants.NODE);
Init.init();
c14n = Canonicalizer.getInstance("http://www.w3.org/2001/10/xml-exc-c14n#");
c14nBytes = c14n.canonicalizeSubtree(node);
Assert.assertEquals("R69a3Im5463c09SuOrn9Sfly9h9LxVxSqg/0CVumJjA=", Base64.encode(MessageDigest.getInstance("SHA-256").digest(c14nBytes)));
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
示例10: testSerializedData
import org.apache.xml.security.c14n.Canonicalizer; //导入方法依赖的package包/类
@org.junit.Test
public void testSerializedData() throws Exception {
if (!haveISOPadding) {
log.warn("Test testSerializedData skipped as necessary algorithms not available");
return;
}
byte[] bits128 = {
(byte) 0x10, (byte) 0x11, (byte) 0x12, (byte) 0x13,
(byte) 0x14, (byte) 0x15, (byte) 0x16, (byte) 0x17,
(byte) 0x18, (byte) 0x19, (byte) 0x1A, (byte) 0x1B,
(byte) 0x1C, (byte) 0x1D, (byte) 0x1E, (byte) 0x1F};
Key key = new SecretKeySpec(bits128, "AES");
Document d = document(); // source
Element e = (Element) d.getElementsByTagName(element()).item(index());
// encrypt
cipher = XMLCipher.getInstance(XMLCipher.AES_128);
cipher.init(XMLCipher.ENCRYPT_MODE, key);
// serialize element ...
Canonicalizer canon =
Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
canon.setWriter(baos);
canon.notReset();
canon.canonicalizeSubtree(e);
baos.close();
String before = baos.toString("UTF-8");
byte[] serialized = baos.toByteArray();
EncryptedData encryptedData =
cipher.encryptData(
d, EncryptionConstants.TYPE_ELEMENT, new ByteArrayInputStream(serialized)
);
//decrypt
XMLCipher dcipher = XMLCipher.getInstance(XMLCipher.AES_128);
dcipher.init(XMLCipher.DECRYPT_MODE, key);
String algorithm = encryptedData.getEncryptionMethod().getAlgorithm();
assertEquals(XMLCipher.AES_128, algorithm);
byte[] bytes = dcipher.decryptToByteArray(dcipher.martial(encryptedData));
String after = new String(bytes, "UTF-8");
assertEquals(before, after);
// test with null type
encryptedData = cipher.encryptData(d, null, new ByteArrayInputStream(serialized));
}
示例11: c14nAndCompare
import org.apache.xml.security.c14n.Canonicalizer; //导入方法依赖的package包/类
private boolean c14nAndCompare(
String fileIn,
String fileRef,
String fileOut,
String c14nURI,
boolean validating,
String xpath,
Map<String, String> namespaces
) throws IOException, FileNotFoundException, SAXException,
ParserConfigurationException, CanonicalizationException,
InvalidCanonicalizerException, TransformerException, XPathExpressionException {
DocumentBuilder documentBuilder = XMLUtils.createDocumentBuilder(validating, false);
// throw away all warnings and errors
documentBuilder.setErrorHandler(new IgnoreAllErrorHandler());
// org.xml.sax.EntityResolver resolver = new TestVectorResolver();
// documentBuilder.setEntityResolver(resolver);
// Document doc = documentBuilder.parse(resolver.resolveEntity(null, fileIn));
Document doc = documentBuilder.parse(fileIn);
Canonicalizer c14n = Canonicalizer.getInstance(c14nURI);
byte c14nBytes[] = null;
if (xpath == null) {
c14nBytes = c14n.canonicalizeSubtree(doc);
} else {
NodeList nl = null;
XPathFactory xpf = XPathFactory.newInstance();
XPath xPath = xpf.newXPath();
DSNamespaceContext namespaceContext =
new DSNamespaceContext(namespaces);
xPath.setNamespaceContext(namespaceContext);
nl = (NodeList)xPath.evaluate(xpath, doc, XPathConstants.NODESET);
c14nBytes = c14n.canonicalizeXPathNodeSet(nl);
}
// org.xml.sax.InputSource refIs = resolver.resolveEntity(null, fileRef);
// byte refBytes[] = JavaUtils.getBytesFromStream(refIs.getByteStream());
byte refBytes[] = JavaUtils.getBytesFromFile(fileRef);
// if everything is OK, result is true; we do a binary compare, byte by byte
boolean result = java.security.MessageDigest.isEqual(refBytes, c14nBytes);
if (!result) {
File f = new File(fileOut);
if (!f.exists()) {
File parent = new File(f.getParent());
parent.mkdirs();
f.createNewFile();
}
FileOutputStream fos = new FileOutputStream(f);
fos.write(c14nBytes);
log.debug("Wrote erroneous result to file " + f.toURI().toURL().toString());
assertEquals(new String(refBytes),new String(c14nBytes));
fos.close();
}
return result;
}
示例12: c14nAndCompare
import org.apache.xml.security.c14n.Canonicalizer; //导入方法依赖的package包/类
private boolean c14nAndCompare(
String fileIn,
String fileRef,
String fileOut,
String c14nURI,
boolean validating,
String xpath,
Map<String, String> namespaces
) throws IOException, FileNotFoundException, SAXException,
ParserConfigurationException, CanonicalizationException,
InvalidCanonicalizerException, TransformerException, XPathExpressionException {
DocumentBuilder documentBuilder = XMLUtils.createDocumentBuilder(validating, false);
// throw away all warnings and errors
documentBuilder.setErrorHandler(new IgnoreAllErrorHandler());
// org.xml.sax.EntityResolver resolver = new TestVectorResolver();
// documentBuilder.setEntityResolver(resolver);
// Document doc = documentBuilder.parse(resolver.resolveEntity(null, fileIn));
Document doc = documentBuilder.parse(fileIn);
Canonicalizer c14n = Canonicalizer.getInstance(c14nURI);
byte c14nBytes[] = null;
if (xpath == null) {
c14nBytes = c14n.canonicalizeSubtree(doc);
} else {
NodeList nl = null;
XPathFactory xpf = XPathFactory.newInstance();
XPath xPath = xpf.newXPath();
DSNamespaceContext namespaceContext =
new DSNamespaceContext(namespaces);
xPath.setNamespaceContext(namespaceContext);
nl = (NodeList)xPath.evaluate(xpath, doc, XPathConstants.NODESET);
c14nBytes = c14n.canonicalizeXPathNodeSet(nl);
}
// org.xml.sax.InputSource refIs = resolver.resolveEntity(null, fileRef);
// byte refBytes[] = JavaUtils.getBytesFromStream(refIs.getByteStream());
byte refBytes[] = JavaUtils.getBytesFromFile(fileRef);
// if everything is OK, result is true; we do a binary compare, byte by byte
boolean result = java.security.MessageDigest.isEqual(refBytes, c14nBytes);
if (!result) {
File f = new File(fileOut);
if (!f.exists()) {
File parent = new File(f.getParent());
parent.mkdirs();
f.createNewFile();
}
FileOutputStream fos = new FileOutputStream(f);
fos.write(c14nBytes);
log.debug("Wrote erroneous result to file " + f.toURI().toURL().toString());
assertEquals(new String(refBytes), new String(c14nBytes));
fos.close();
}
return result;
}