How can chunked reads yield the same digest as reading the dataset at once?
Successive update calls feed bytes to a hash in order, equivalent to processing the concatenated chunks in one call.
Python example and interpretation
Splitting abc into a and bc preserves its digest. Text-mode newline conversion can change the bytes and therefore the result.
import hashlib
h=hashlib.sha256()
h.update(b'a')
h.update(b'bc')
h.digest()==hashlib.sha256(b'abc').digest()Result True
中文
分块读取大型数据集时,怎样验证内容摘要与整文件一致?
哈希对象多次 update 按顺序吸收字节,其结果等于一次输入所有分块的连接。
Python 算例与适用条件
将 abc 拆为 a 与 bc 后摘要一致。若读取文本时换行被转换,参与哈希的字节就不再相同。
import hashlib
h=hashlib.sha256()
h.update(b'a')
h.update(b'bc')
h.digest()==hashlib.sha256(b'abc').digest()结果 True