如果 md5 消息的长度已经正确,那么填充它的目的是什么?
我知道md5中填充的过程,但是在已经是正确长度的消息中添加一个1和几个0的目的是什么?
这是为了安全还是只是一个标记?
I know the process for padding in md5, but what is the purpose of adding a 1 and several 0's to a message that is already the correct length?
Is this for security or just a marker?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
填充过程不得产生冲突。如果您有一条消息 m,它将被填充到 pm 中,其长度是 512 的倍数。现在将 pm 想象为一条消息 m' 本身,即填充位已经添加,就好像它们是消息的一部分一样。如果正如您所建议的那样,填充只是保持 m' 不变,则 m 和 m' 将产生相同的哈希值,即使它们是不同的消息。这将是一次碰撞,也称为“根本不好”。
一般来说,填充过程必须能够明确地删除它:您必须能够查看填充的消息,并毫不犹豫地决定哪些位来自消息本身,哪些位作为填充添加。哈希函数过程中没有任何内容实际上会删除填充,但它在概念上必须是可行的。如果不添加任何位来“填充”长度为 512 倍数的消息,这在数学上是不可能的。
以上对于所有哈希函数都是通用的。 MD5 和同一通用系列的一些函数(包括 SHA-1、SHA-256...),使用 Merkle-Damgård 构造,还需要将输入数据长度编码在填充中(这是实现某些安全证明所必需的)。在 MD5 中,长度被编码为 64 位数字。对于“1”位,任何消息至少有 65 个填充位(最多 511 个)。
The padding procedure must not create collisions. If you have a message m it is padded into pm, which has a length multiple of 512. Now imagine pm as a message m' in itself, i.e. the padding bits already added as if they were part of the message. If padding just keeps m' unchanged, as you suggest, then m and m' would yield the same hash value, even though they are distinct messages. That would be a collision, also known as "not good at all".
Generally speaking, the padding procedure must be such that it could potentially be unambiguously removed: you must be able to look at a padded message, and decide without hesitation which bits are from the message itself, and which were added as padding. Nothing in the course of the hash function actually removes the padding, but it must be conceptually feasible. This is kind of mathematically impossible if messages of length multiple of 512 are "padded" by adding no bit at all.
The above is generic to all hash functions. MD5 and a few functions of the same general family (including SHA-1, SHA-256...), using the Merkle-Damgård construction, also need the input data length to be encoded in the padding (this is necessary to achieve some security proofs). In MD5, the length is encoded as a 64-bit number. With the '1' bit, there are at least 65 padding bits for any message (and at most 511).
消息经过填充,以便长度可被 512 整除。请记住,它是要为其计算哈希值的消息的位表示形式。由于消息需要分成 512 位块,因此添加了额外的位作为填充。有关更多详细信息,请查看 Wiki 上的算法部分。
The message is padded so that the length is divisible by 512. Remember that it is the bit representation of the message for which the hash is being calculated. And since the message needs to be broken into 512-bit chunks, extra bits are added as padding. Check the Algorithm section on the Wiki for more details.