Search in sources :

Example 1 with DigesterOutputStream

use of com.sun.org.apache.xml.internal.security.utils.DigesterOutputStream in project jdk8u_jdk by JetBrains.

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 {
    OutputStream os = null;
    try {
        MessageDigestAlgorithm mda = this.getMessageDigestAlgorithm();
        mda.reset();
        DigesterOutputStream diOs = new DigesterOutputStream(mda);
        os = new UnsyncBufferedOutputStream(diOs);
        XMLSignatureInput output = this.dereferenceURIandPerformTransforms(os);
        // C14N11 transform if needed
        if (Reference.useC14N11 && !validating && !output.isOutputStreamSet() && !output.isOctetStream()) {
            if (transforms == null) {
                transforms = new Transforms(this.doc);
                transforms.setSecureValidation(secureValidation);
                this.constructionElement.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("empty", ex);
    } catch (IOException ex) {
        throw new ReferenceNotInitializedException("empty", ex);
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException ex) {
                throw new ReferenceNotInitializedException("empty", ex);
            }
        }
    }
}
Also used : UnsyncBufferedOutputStream(com.sun.org.apache.xml.internal.security.utils.UnsyncBufferedOutputStream) OutputStream(java.io.OutputStream) DigesterOutputStream(com.sun.org.apache.xml.internal.security.utils.DigesterOutputStream) Transforms(com.sun.org.apache.xml.internal.security.transforms.Transforms) MessageDigestAlgorithm(com.sun.org.apache.xml.internal.security.algorithms.MessageDigestAlgorithm) DigesterOutputStream(com.sun.org.apache.xml.internal.security.utils.DigesterOutputStream) IOException(java.io.IOException) UnsyncBufferedOutputStream(com.sun.org.apache.xml.internal.security.utils.UnsyncBufferedOutputStream) XMLSecurityException(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)

Aggregations

MessageDigestAlgorithm (com.sun.org.apache.xml.internal.security.algorithms.MessageDigestAlgorithm)1 XMLSecurityException (com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)1 Transforms (com.sun.org.apache.xml.internal.security.transforms.Transforms)1 DigesterOutputStream (com.sun.org.apache.xml.internal.security.utils.DigesterOutputStream)1 UnsyncBufferedOutputStream (com.sun.org.apache.xml.internal.security.utils.UnsyncBufferedOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1