简体   繁体   中英

How to calculate SHA-256 checksum of S3 file content

S3 out of the box provides the MD5 checksum of the S3 object content. But I need to calculate the SHA-256 checksum of the file content. The file could be large enough so I do not want to load the file in memory and calculate the checksum, instead I need a solution to calculate the checksum without loading the whole file in memory.

It can be achieved by following steps in Java:

  1. Get InputStream of the S3 Object
  2. Use MessageDigest and DigestInputStream classes for the SHA-256 hash(or SHA-1 or MD5)

Following is the snippet on how to do it:

String getS3FileHash(AmazonS3 amazonS3, String s3bucket, String filePath) {
    try {
        InputStream inputStream = amazonS3.getObject(s3bucket, filePath).getObjectContent();
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
        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; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM