Search in sources :

Example 1 with MessageDigestAlgorithm

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));
}
Also used : MessageDigestAlgorithm(org.apache.xml.security.algorithms.MessageDigestAlgorithm) Document(org.w3c.dom.Document) MessageDigest(java.security.MessageDigest)

Example 2 with MessageDigestAlgorithm

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));
}
Also used : MessageDigestAlgorithm(org.apache.xml.security.algorithms.MessageDigestAlgorithm) Document(org.w3c.dom.Document) MessageDigest(java.security.MessageDigest)

Example 3 with MessageDigestAlgorithm

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));
}
Also used : MessageDigestAlgorithm(org.apache.xml.security.algorithms.MessageDigestAlgorithm) Document(org.w3c.dom.Document) MessageDigest(java.security.MessageDigest)

Example 4 with MessageDigestAlgorithm

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);
    }
}
Also used : DigesterOutputStream(org.apache.xml.security.utils.DigesterOutputStream) OutputStream(java.io.OutputStream) UnsyncBufferedOutputStream(org.apache.xml.security.utils.UnsyncBufferedOutputStream) Transforms(org.apache.xml.security.transforms.Transforms) MessageDigestAlgorithm(org.apache.xml.security.algorithms.MessageDigestAlgorithm) DigesterOutputStream(org.apache.xml.security.utils.DigesterOutputStream) IOException(java.io.IOException) UnsyncBufferedOutputStream(org.apache.xml.security.utils.UnsyncBufferedOutputStream) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException)

Example 5 with MessageDigestAlgorithm

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));
}
Also used : MessageDigestAlgorithm(org.apache.xml.security.algorithms.MessageDigestAlgorithm) Document(org.w3c.dom.Document) MessageDigest(java.security.MessageDigest)

Aggregations

MessageDigestAlgorithm (org.apache.xml.security.algorithms.MessageDigestAlgorithm)13 MessageDigest (java.security.MessageDigest)12 Document (org.w3c.dom.Document)12 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)1 Transforms (org.apache.xml.security.transforms.Transforms)1 DigesterOutputStream (org.apache.xml.security.utils.DigesterOutputStream)1 UnsyncBufferedOutputStream (org.apache.xml.security.utils.UnsyncBufferedOutputStream)1