我正在尝试了解 SHA256。关于Wikipedia page它说:
append the bit '1' to the message
append k bits '0', where k is the minimum number >= 0 such that the resulting message length (modulo 512 in bits) is 448.
append length of message (without the '1' bit or padding), in bits, as 64-bit big-endian integer (this will make the entire post-processed length a multiple of 512 bits)
所以如果我的消息是 01100001 01100010 01100011
我会先添加一个 1
来获取
01100001 01100010 01100011 1
然后您将填写 0
,这样总长度就是 448 mod 512:
01100001 01100010 01100011 10000000 0000 ... 0000
(所以在这个例子中,一个人会添加 448 - 25 0
s)
我的问题是:最后一部分是什么意思?我想看一个例子。
最佳答案
表示消息长度,填充为 64 位,字节按重要性顺序出现。因此,如果消息长度为 37113,则十六进制为 90 f9;两个字节。有两种基本 (*) 方法可以将其表示为 64 位整数,
00 00 00 00 00 00 90 f9 # big endian
和
f9 90 00 00 00 00 00 00 # little endian
以前的约定遵循数字通常以十进制表示的方式:102 写成 102,首先写出最高有效部分(“大端”),然后是最低有效部分(“小端”)最后的。明确指定的原因是这两种约定在实践中都被使用;互联网协议(protocol)使用大端,Intel 兼容处理器使用小端,所以如果它们是十进制机器,它们会将 102 写成 201。
(*) 其实有8个! = 40320 种表示 64 位整数的方法,如果 8 位字节是要排列的最小单位,但实际使用了两种。
关于algorithm - 在 SHA256 中填充消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23163770/