At Which Position MD5-PADDING starts? <p>I'm trying to implement MD5 File-Hash in and I think it will be finished in the near future. Currently I need to append the PADDING but i don't understand how this works.</p> <p>Example: I have a file with 109 Bytes of data.</p> <pre><code>// 512 Bit 360 Bit // ------------- ------------- // |xxxxxxxxxxx| |xxxxxx | // ------------- ------------- </code></pre> <p>When appending the PADDING to the data, does it have to look like this:</p> <pre><code>// 512 Bit 360 Bit 512 Bit // ------------- ------------- ------------- // |xxxxxxxxxxx| |xxxxxx|1|00| |00000000|64| // ------------- ------------- ------------- </code></pre> <p>or this</p> <pre><code>// 512 Bit 512 Bit // ------------- ------------- // |xxxxxxxxxxx| |xxx|1|00|64| // ------------- ------------- </code></pre> <p>or this?</p> <pre><code>// 512 Bit 360 Bit 512 Bit // ------------- ------------- ------------- // |xxxxxxxxxxx| |xxxxxx | |10000000|64| // ------------- ------------- ------------- </code></pre> <p>I'm confused. After Consulting the RFC1321 Text I think to use the Padding in the SAME Block if it is possible to pad inside a block. And if this doesn't fit then use a new block. </p> <p>Am I right?</p> <p>EDIT: Need more detail.</p> <p>Where will the padding be appended? after the last data bit or the last data byte? or is this the same?</p> <p>Like this?</p> <pre><code> 512 Bit-Block -------------------------------------------------- |1|1|0|X0000000| |0|0|0|0|0|0|0|1| |0|0|0|00000000| |0|0|0|0|0|0|0|0| |0|0|4|00000000| |0|0|0|0|0|0|0|1| |0|0|6|00000000| ... |0|0|0|0|0|0|0|1| |1|1|8|00000000| |0|0|0|0|0|0|0|1| |1|1|0|00000000| |0|0|0|0|0|0|0|1| |0|1|0|00000000| |0|0|0|0|0|0|0|1| |1|1|0|00000000| |0|0|0|0|0|0|0|0| -------------------------------------------------- |DATA |PADDING | 64 Bit counter| </code></pre> <p>or like this?</p> <pre><code> 512 Bit-Block -------------------------------------------------- |1|1|0|00000000| |0|0|0|0|0|0|0|1| |0|0|0|00000000| |0|0|0|0|0|0|0|0| |0|0|4|00000000| |0|0|0|0|0|0|0|1| |0|0|6|00000000| ... |0|0|0|0|0|0|0|1| |1|1|8|00000000| |0|0|0|0|0|0|0|1| |1|1|X|00000000| |0|0|0|0|0|0|0|1| |0|1|0|00000000| |0|0|0|0|0|0|0|1| |1|1|0|00000000| |0|0|0|0|0|0|0|0| -------------------------------------------------- |DATA |PADDING | 64 Bit counter| </code></pre>