简体   繁体   English

为什么我的 sha256 校验和与 aws glacier 校验和响应不兼容?

[英]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.

相关问题 Boto3 文件上传到 S3 Bucket 未通过 SHA256 校验和检查 - Boto3 file upload to S3 Bucket is failing SHA256 checksum check 为什么 SHA1 和 SHA256 与我的应用名称 package 不匹配? - Why SHA1 and SHA256 are not matching with my app package name? AWS S3 - Etag Sha256 而不是 Md5 - AWS S3 - Etag Sha256 instead of Md5 Ballerina HMAC SHA256 未生成预期结果 - Ballerina HMAC SHA256 not generating expected results 用于签名 AWS 请求的 HMAC SHA-256 - HMAC SHA-256 for a signed AWS request 如何从椭圆曲线 secp256k1 导出以太坊地址 - SHA256 摘要 - How to derive ethereum address from Elliptic Curve secp256k1 - SHA256 Digest 在空手道框架中,如何检索由 SHA256 加密库生成的 APIGEE 令牌? - In Karate framework, How to retrieve APIGEE token generated out of SHA256 encrypted libraries? 如何验证在 C# 中使用 google KMS(非对称符号,椭圆曲线 P-256 密钥 SHA256 摘要)签名的签名真实性 - How to verify signature authenticity that was signed using google KMS (Asymmetric sign, Elliptic Curve P-256 key SHA256 Digest) in C# AWS 冰川删除作业 - AWS glacier delete job 不支持您提供的授权机制。 请使用 AWS4-HMAC-SHA256 - The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM