Padding the message in SHA256

落花浮王杯 提交于 2019-12-11 12:16:45

问题


I am trying to understand SHA256. On the Wikipedia page it says:

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)

So if my message is 01100001 01100010 01100011 I would first add a 1 to get

01100001 01100010 01100011 1

Then you would fill in 0s so that the total length is 448 mod 512:

01100001 01100010 01100011 10000000 0000 ... 0000

(So in this example, one would add 448 - 25 0s)

My question is: What does the last part mean? I would like to see an example.


回答1:


It means the message length, padded to 64 bits, with the bytes appearing in order of significance. So if the message length is 37113, that's 90 f9 in hex; two bytes. There are two basic(*) ways to represent this as a 64-bit integer,

00 00 00 00 00 00 90 f9  # big endian

and

f9 90 00 00 00 00 00 00  # little endian

The former convention follows the way numbers are usually written out in decimal: one hundred and two is written 102, with the most significant part (the "big end") being written first, the least significant ("little end") last. The reason that this is specified explicitly is that both conventions are used in practice; internet protocols use big endian, Intel-compatible processors use little endian, so if they were decimal machines, they'd write one hundred and two as 201.

(*) Actually there are 8! = 40320 ways to represent a 64-bit integer if 8-bit bytes are the smallest units to be permuted, but two are in actual use.



来源:https://stackoverflow.com/questions/23163770/padding-the-message-in-sha256

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!