簡體   English   中英

為什么我的 sha256 校驗和與 aws glacier 校驗和響應不兼容?

[英]Why is my sha256 checksum incompatible with aws glacier checksum response?

我在 ubuntu 服務器中有一個存檔文件。 我使用aws cli將此文件上傳到 AWS glacier 中。 最后,AWS 給了我一個這樣的校驗和:

{"checksum": "6c126443c882b8b0be912c91617a5765050d7c99dc43b9d30e47c42635ab02d5"}

但是當我像這樣在自己的服務器中檢查校驗和時:

sunny@server:~/sha256sum backup.zip

返回此校驗和:

5ba29292a350c4a8f194c78dd0ef537ec21ca075f1fe649ae6296c7100b25ba8

為什么校驗和之間有差異?

雖然 Glacier 返回的校驗和使用 SHA-256,但它並不是對整個 object 進行簡單的 SHA-256 求和。而是對每兆字節數據計算哈希值,並為每對哈希值計算一個 hash,並重復該過程直到剩下一個 hash。 有關詳細信息,請參閱文檔

這是 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))

您可以像這樣在單個文件上調用它:

$ ./glacier_checksum.py backup.zip

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM