[英]Why is my sha256 checksum incompatible with aws glacier checksum response?
I have an archive file in ubuntu server.我在 ubuntu 服务器中有一个存档文件。 I uploaded this file in AWS glacier using
aws cli
.我使用
aws cli
将此文件上传到 AWS glacier 中。 at the finishing, AWS gave me a checksum like this:最后,AWS 给了我一个这样的校验和:
{"checksum": "6c126443c882b8b0be912c91617a5765050d7c99dc43b9d30e47c42635ab02d5"}
but when i checked the checksum in own server like this:但是当我像这样在自己的服务器中检查校验和时:
sunny@server:~/sha256sum backup.zip
return this checksum:返回此校验和:
5ba29292a350c4a8f194c78dd0ef537ec21ca075f1fe649ae6296c7100b25ba8
why between checksums has a difference?为什么校验和之间有差异?
While the checksum returned by Glacier uses SHA-256, it is not a simple SHA-256 sum over the entire object. Rather, it calculates hashes for each megabyte of data, and calculates a hash for each pair of hashes, and repeats the process till one hash remains.虽然 Glacier 返回的校验和使用 SHA-256,但它并不是对整个 object 进行简单的 SHA-256 求和。而是对每兆字节数据计算哈希值,并为每对哈希值计算一个 hash,并重复该过程直到剩下一个 hash。 For more information, see the documentation .
有关详细信息,请参阅文档。
Here's is a simple implementation in Python这是 Python 中的一个简单实现
#!/usr/bin/env python3
import hashlib
import sys
import binascii
# Given a file object (opened in binary mode), calculate the checksum used by glacier
def calc_hash_tree(fileobj):
chunk_size = 1048576
# Calculate a list of hashes for each chunk in the fileobj
chunks = []
while True:
chunk = f.read(chunk_size)
if len(chunk) == 0:
break
chunks.append(hashlib.sha256(chunk).digest())
# Now calculate each level of the tree till one digest remains
while len(chunks) > 1:
next_chunks = []
while len(chunks) > 1:
next_chunks.append(hashlib.sha256(chunks.pop(0) + chunks.pop(0)).digest())
if len(chunks) > 0:
next_chunks.append(chunks.pop(0))
chunks = next_chunks
# The final remaining hash is the root of the tree:
return binascii.hexlify(chunks[0]).decode("utf-8")
if __name__ == "__main__":
with open(sys.argv[1], "rb") as f:
print(calc_hash_tree(f))
You can call it on a single file like this:您可以像这样在单个文件上调用它:
$ ./glacier_checksum.py backup.zip
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.