如何计算S3文件内容的SHA-256校验和 [英] How to calculate SHA-256 checksum of S3 file content
本文介绍了如何计算S3文件内容的SHA-256校验和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
开箱即用的S3提供S3对象内容的MD5校验和.但是我需要计算文件内容的SHA-256校验和.该文件可能足够大,所以我不想将文件加载到内存中并计算校验和,相反,我需要一种解决方案来计算校验和而不将整个文件加载到内存中.
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.
推荐答案
可以通过Java中的以下步骤来实现:
It can be achieved by following steps in Java:
- 获取S3对象的InputStream
- 将MessageDigest和DigestInputStream类用于SHA-256哈希(或SHA-1或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;
}
这篇关于如何计算S3文件内容的SHA-256校验和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文