Search in sources :

Example 1 with DigesterOutputStream

use of org.apache.xml.security.utils.DigesterOutputStream 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)

Aggregations

IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 MessageDigestAlgorithm (org.apache.xml.security.algorithms.MessageDigestAlgorithm)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