hashlib中的"block_size"和"digest_size"之间的区别? [英] Difference between `block_size` and `digest_size` in hashlib?

查看:481
本文介绍了hashlib中的"block_size"和"digest_size"之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Python hashlib软件包文档,并希望对两个散列对象属性(即hash.block_sizehash.digest_size)进行一些说明.这是每个属性的定义:

I was going through the Python hashlib package documentation and wanted some clarification on two hash object attributes (namely hash.block_size and hash.digest_size). Here is the definition of each attribute:

hash.digest_size =结果哈希的大小(以字节为单位)."
hash.block_size =哈希算法的内部块大小(以字节为单位)."
来源: https://docs.python.org/2/library/hashlib.html

hash.digest_size= "The size of the resulting hash in bytes."
hash.block_size = "The internal block size of the hash algorithm in bytes."
source: https://docs.python.org/2/library/hashlib.html

因此,我了解到hash.digest_size只是被hash_object散列或消化"后的数据的长度或大小(以字节为单位).例如,从下面的代码中,通过SHA256哈希对象获取字符串"Hello World"的摘要,将返回32字节(或256位)的digest_size.

So I understand that hash.digest_size is simply the length or size (in bytes) of the data once it is hashed or "digested" by the hash_object. For for example from the code below getting the digest of the string 'Hello World' via a SHA256 hash object returns a digest_size of 32 bytes (or 256 bits).

import hashlib
hash_object = hashlib.sha256()
hash_object.update(b'Hello World')
hex_dig = hash_object.hexdigest()

print(hex_dig)
>>>a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
print(hash_object.digest_size)
>>>32
print(hash_object.block_size)
>>>64 
print(len(hex_dig))
>>>64

我不理解的是这个hash.block_size属性.它仅仅是表示散列数据的十六进制表示形式所需的字符长度吗?还有其他事情吗?我不太了解此属性的定义,因此对此做任何澄清都是非常有帮助的.有见地!

What I don't understand is this hash.block_size attribute. Is it simply the length of characters required to represent the hexadecimal representation of the hashed data? Is it something else entirely? I don't quite understand the definition of this attribute so any clarification on this would be very helpful & insightful!

推荐答案

使用输入的任意长度数据计算哈希.大多数散列函数通过使用基于固定数据块更新内部状态的函数来执行此操作,并且您要散列的文件(例如)将以此固定块大小的块进行处理.

The hash is computed with input arbitrary length data. Most hash functions do this by using a function that updates an internal state based on a fixed block of data, and the file (e.g.) you're hashing is processed in chunks of this fixed block size.

因此,大多数散列函数具有固定的初始状态(通常为digest_size,但有时更大),该状态在初始化函数(或具有空输入的散列的构造函数)中进行初始化.对于SHA-256(以及SHA-224),这是32个字节,或者实际上是8个整数.

So most hash functions have a fixed initial state (often of digest_size, but sometimes larger) that gets initialised in an initialise function (or the constructor of an hash with empty input, as well). For SHA-256 (and SHA-224 as well) this is 32 bytes, or actually 8 integers.

然后,它按块处理输入数据(对于SHA-256,这是64个字节,将其转换为16个32位整数,然后对8个状态整数和16个数据整数进行冗长的计算,之后我们得到一个新的状态为8个整数.只要有输入数据,该状态就会持续下去.输入块的大小为block_size.

Then it processes input data in chunks (for SHA-256 this is 64 bytes, which are transformed into 16 32-bit integers, then a longish computation is done on 8 state integers and 16 data integers after which we have a new state of 8 integers. This goes on for as long as there is input data. The size of the input chunk is the block_size.

当我们要计算摘要时(通常在数据的末尾),我们填充最后一个数据(如果最后一个输入块小于块大小),并将到目前为止的总哈希长度放入最终值中64个字节(16个整数),并从最后一个时间开始进行转换.摘要功能然后输出最终状态(的一部分)(SHA-224仅输出其32字节状态的224位= 28字节)作为摘要.最终输出的大小(以字节为单位)为digest_size.

When we want to compute a digest (typically at the end of the data) we pad out the last data (if the last input chunk was smaller than the block size) and also put the total hashed length so far into the final 64 bytes (16 integers) and do the transformation from before one final time. The digest function then outputs the (part of) the final state (SHA-224 only outputs 224 bits=28 bytes of its 32 byte state) as the digest. The size of the final output (in bytes) is digest_size.

这篇关于hashlib中的"block_size"和"digest_size"之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆