protected void verify(final Path file, final MessageDigest digest, final Checksum checksum) throws ChecksumException {
if(file.getType().contains(Path.Type.encrypted)) {
log.warn(String.format("Skip checksum verification for %s with client side encryption enabled", file));
return;
}
if(null == digest) {
log.debug(String.format("Digest disabled for file %s", file));
return;
}
if(null == checksum) {
log.warn("ETag returned by server is unknown checksum algorithm");
return;
}
if(!checksum.algorithm.equals(HashAlgorithm.md5)) {
log.warn(String.format("ETag %s returned by server is %s but expected MD5", checksum.hash, checksum.algorithm));
return;
}
final String expected = Hex.encodeHexString(digest.digest());
if(!checksum.equals(Checksum.parse(expected))) {
throw new ChecksumException(MessageFormat.format(LocaleFactory.localizedString("Upload {0} failed", "Error"), file.getName()),
MessageFormat.format("Mismatch between MD5 hash {0} of uploaded data and ETag {1} returned by the server",
expected, checksum.hash));
}
}