I want to use Sha256 for the checksum of my objects. But it looks like, that amazon uses md5 in the ETag.
Is there any workaround?
I want to use Sha256 for the checksum of my objects. But it looks like, that amazon uses md5 in the ETag.
Is there any workaround?
This is possible as of 2022-02-25. S3 now features a Checksum Retrieval function GetObjectAttributes:
New – Additional Checksum Algorithms for Amazon S3 | AWS News Blog
Checksum Retrieval – The new
GetObjectAttributesfunction returns the checksum for the object and (if applicable) for each part.
This function supports SHA-1, SHA-256, CRC-32, and CRC-32C for checking the integrity of the transmission.
I'm so glad that they now have alternatives to the sad choice of MD5, which is not optimal for anything in particular and was broken for other purposes long ago. See also related discussion of quirks with their MD5 approach at How to get the md5sum of a file on Amazon's S3.
[And while I'm discussing hashes for various purposes, note that a good one for hash-table lookups and other situations which have some basic randomness and security propertiees is HighwayHash: Fast strong hash functions: SipHash/HighwayHash]
Please refer: How to calculate SHA-256 checksum of S3 file content
It can be achieved by following steps in Java:
InputStream inputStream = amazonS3.getObject(bucket, file).getObjectContent();
Use MessageDigest and DigestInputStream classes for the SHA-256 hash
public static String getHash(InputStream inputStream, String algorithm) {
try {
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
DigestInputStream digestInputStream = new DigestInputStream(inputStream, messageDigest);
byte[] buffer = new byte[4096];
int count = 0;
while (digestInputStream.read(buffer) > -1) {
count++;
}
log.info("total read: " + count);
MessageDigest digest = digestInputStream.getMessageDigest();
digestInputStream.close();
byte[] md5 = digest.digest();
StringBuilder sb = new StringBuilder();
for (byte b: md5) {
sb.append(String.format("%02X", b));
}
return sb.toString().toLowerCase();
} catch (Exception e) {
log.error(e);
}
return null;
}