use of org.apache.xml.security.algorithms.MessageDigestAlgorithm in project santuario-java by apache.
the class DigestAlgorithmTest method testMD5.
@org.junit.Test
public void testMD5() throws Exception {
Document doc = XMLUtils.createDocumentBuilder(false).newDocument();
MessageDigestAlgorithm digestAlgorithm = MessageDigestAlgorithm.getInstance(doc, MessageDigestAlgorithm.ALGO_ID_DIGEST_NOT_RECOMMENDED_MD5);
assertEquals(MessageDigestAlgorithm.ALGO_ID_DIGEST_NOT_RECOMMENDED_MD5, digestAlgorithm.getAlgorithmURI());
byte[] digest = digestAlgorithm.digest("test-string".getBytes());
assertNotNull(digest);
assertTrue(digest.length > 0);
// Now compare against a JDK MessageDigest Object
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] digest2 = md.digest("test-string".getBytes());
assertTrue(Arrays.equals(digest, digest2));
}
use of org.apache.xml.security.algorithms.MessageDigestAlgorithm in project santuario-java by apache.
the class DigestAlgorithmTest method testSHA3_256.
@org.junit.Test
public void testSHA3_256() throws Exception {
org.junit.Assume.assumeTrue(bcInstalled);
Document doc = XMLUtils.createDocumentBuilder(false).newDocument();
MessageDigestAlgorithm digestAlgorithm = MessageDigestAlgorithm.getInstance(doc, MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA3_256);
assertEquals(MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA3_256, digestAlgorithm.getAlgorithmURI());
byte[] digest = digestAlgorithm.digest("test-string".getBytes());
assertNotNull(digest);
assertTrue(digest.length > 0);
// Now compare against a JDK MessageDigest Object
MessageDigest md = MessageDigest.getInstance("SHA3-256");
byte[] digest2 = md.digest("test-string".getBytes());
assertTrue(Arrays.equals(digest, digest2));
}
use of org.apache.xml.security.algorithms.MessageDigestAlgorithm in project santuario-java by apache.
the class DigestAlgorithmTest method testSHA256.
@org.junit.Test
public void testSHA256() throws Exception {
Document doc = XMLUtils.createDocumentBuilder(false).newDocument();
MessageDigestAlgorithm digestAlgorithm = MessageDigestAlgorithm.getInstance(doc, MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA256);
assertEquals(MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA256, digestAlgorithm.getAlgorithmURI());
byte[] digest = digestAlgorithm.digest("test-string".getBytes());
assertNotNull(digest);
assertTrue(digest.length > 0);
// Now compare against a JDK MessageDigest Object
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] digest2 = md.digest("test-string".getBytes());
assertTrue(Arrays.equals(digest, digest2));
}
use of org.apache.xml.security.algorithms.MessageDigestAlgorithm in project santuario-java by apache.
the class Reference method calculateDigest.
/**
* Method calculateDigest
*
* @param validating true if validating the reference
* @return reference Calculate the digest of this reference.
* @throws ReferenceNotInitializedException
* @throws XMLSignatureException
*/
private byte[] calculateDigest(boolean validating) throws ReferenceNotInitializedException, XMLSignatureException {
XMLSignatureInput input = this.getContentsBeforeTransformation();
if (input.isPreCalculatedDigest()) {
return getPreCalculatedDigest(input);
}
cacheDereferencedElement(input);
MessageDigestAlgorithm mda = this.getMessageDigestAlgorithm();
mda.reset();
try (DigesterOutputStream diOs = new DigesterOutputStream(mda);
OutputStream os = new UnsyncBufferedOutputStream(diOs)) {
XMLSignatureInput output = this.getContentsAfterTransformation(input, os);
this.transformsOutput = output;
// C14N11 transform if needed
if (Reference.useC14N11 && !validating && !output.isOutputStreamSet() && !output.isOctetStream()) {
if (transforms == null) {
transforms = new Transforms(getDocument());
transforms.setSecureValidation(secureValidation);
getElement().insertBefore(transforms.getElement(), digestMethodElem);
}
transforms.addTransform(Transforms.TRANSFORM_C14N11_OMIT_COMMENTS);
output.updateOutputStream(os, true);
} else {
output.updateOutputStream(os);
}
os.flush();
if (output.getOctetStreamReal() != null) {
output.getOctetStreamReal().close();
}
return diOs.getDigestValue();
} catch (XMLSecurityException ex) {
throw new ReferenceNotInitializedException(ex);
} catch (IOException ex) {
throw new ReferenceNotInitializedException(ex);
}
}
use of org.apache.xml.security.algorithms.MessageDigestAlgorithm in project santuario-java by apache.
the class DigestAlgorithmTest method testSHA384.
@org.junit.Test
public void testSHA384() throws Exception {
Document doc = XMLUtils.createDocumentBuilder(false).newDocument();
MessageDigestAlgorithm digestAlgorithm = MessageDigestAlgorithm.getInstance(doc, MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA384);
assertEquals(MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA384, digestAlgorithm.getAlgorithmURI());
byte[] digest = digestAlgorithm.digest("test-string".getBytes());
assertNotNull(digest);
assertTrue(digest.length > 0);
// Now compare against a JDK MessageDigest Object
MessageDigest md = MessageDigest.getInstance("SHA-384");
byte[] digest2 = md.digest("test-string".getBytes());
assertTrue(Arrays.equals(digest, digest2));
}
Aggregations