signaturePartDef.setDigestAlgo(securePart.getDigestMethod());
if (signaturePartDef.getDigestAlgo() == null) {
signaturePartDef.setDigestAlgo(getSecurityProperties().getSignatureDigestAlgorithm());
}
DigestOutputStream digestOutputStream = createMessageDigestOutputStream(signaturePartDef.getDigestAlgo());
InputStream inputStream = attachment.getSourceStream();
if (!inputStream.markSupported()) {
inputStream = new BufferedInputStream(inputStream);
}
inputStream.mark(Integer.MAX_VALUE); //we can process at maximum 2G with the standard jdk streams
try {
Transformer transformer = buildTransformerChain(digestOutputStream, signaturePartDef, null);
Map<String, Object> transformerProperties = new HashMap<String, Object>(2);
transformerProperties.put(
AttachmentContentSignatureTransform.ATTACHMENT, attachment);
transformer.setProperties(transformerProperties);
transformer.transform(inputStream);
transformer.doFinal();
digestOutputStream.close();
//reset the inputStream to be able to reuse it
inputStream.reset();
} catch (IOException e) {
throw new WSSecurityException(WSSecurityException.ErrorCode.FAILED_SIGNATURE, e);
} catch (XMLStreamException e) {
throw new WSSecurityException(WSSecurityException.ErrorCode.FAILED_SIGNATURE, e);
}
String calculatedDigest = new String(Base64.encode(digestOutputStream.getDigestValue()));
if (LOG.isDebugEnabled()) {
LOG.debug("Calculated Digest: " + calculatedDigest);
}
signaturePartDef.setDigestValue(calculatedDigest);