# SHA256: Padding a 512 bits length message

I am currently implementing the SHA256 hash-algorithm for a "custom-built" embeded-device. Obviously I have a problem with message padding. The routine I wrote does not work with message whose size equals exactly 512 bits.

In that case, how should the message be padded ? i.e.

M = "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPAABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP"


I mean, what should be the hexa-decimal representation of the last padded block?

• You add 512 bits of padding. 0x80 in the first byte and the length is the last 8 bytes. Pretty much the same as for other messages. What's trickier is when less than 9 bytes remain in a block, since then you need to split the padding between two blocks. – CodesInChaos Oct 14 '15 at 20:14
• In the example above, I would add 0x200 for the length of the message so ? – Zyend Oct 14 '15 at 21:05
• Depends on the encoding, but I count 32 characters. For ASCII compatible that would be 0x100, not 0x200 – Maarten Bodewes Oct 15 '15 at 9:08

80 000000000000000000000000000000000000000000000 0000000000000200