md5算法的具体步骤见RFC1321(http://www.ietf.org/rfc/rfc1321.txt)
这里说一下具体实现时,应该注意的地方:
1.注意数据存储格式
其实,在官方文档中已经提到过这个,但是没注意,现把它摘抄如下;
In this document a "word" is a 32-bit quantity and a "byte" is an
eight-bit quantity. A sequence of bits can be interpreted in a
natural manner as a sequence of bytes, where each consecutive group
of eight bits is interpreted as a byte with the high-order (most
significant) bit of each byte listed first. Similarly, a sequence of
bytes can be interpreted as a sequence of 32-bit words, where each
consecutive group of four bytes is interpreted as a word with the
low-order (least significant) byte given first.
翻译过来,就是说在md5算法中一个字节内是按MSB存储的,在一个字内是按LSB存储的。
例如:00010111 11011001 11100011 10101100(msb)
按照文档中的规定,每个字节是按MSB存储的,则写成十六进制如下:
0x17 d9 e3 ac
而在一个字,即32位里,是按高字节序存储的,所以最终的存储值如下:
0xace3d917
另外,在填充长度时,两个字也是按LSB存储,每个字内是按上面所说的存储,具体原文如下:
A 64-bit representation of b (the length of the message before the
padding bits were added) is appended to the result of the previous
step. In the unlikely event that b is greater than 2^64, then only
the low-order 64 bits of b are used. (These bits are appended as two
32-bit words and appended low-order word first in accordance with the
previous conventions.)
2.迭代时,数据存储格式
虽然官方文档规定了数据该怎样存储,但是在真正实现时,就连官方给的DEMO也没有完全遵守。以至于自己实现md5算法时,算出来的结果总是不对。
md5实现时,分3步:
第一步,分割输入数据,填充数据和长度。
在这一步,原生的数据和填充的数据(0x80 00 00 ...)是按高字节序存储的。而填充的长度须按上面所说的规定存储&#x
这里说一下具体实现时,应该注意的地方:
1.注意数据存储格式
其实,在官方文档中已经提到过这个,但是没注意,现把它摘抄如下;
In this document a "word" is a 32-bit quantity and a "byte" is an
eight-bit quantity. A sequence of bits can be interpreted in a
natural manner as a sequence of bytes, where each consecutive group
of eight bits is interpreted as a byte with the high-order (most
significant) bit of each byte listed first. Similarly, a sequence of
bytes can be interpreted as a sequence of 32-bit words, where each
consecutive group of four bytes is interpreted as a word with the
low-order (least significant) byte given first.
翻译过来,就是说在md5算法中一个字节内是按MSB存储的,在一个字内是按LSB存储的。
例如:00010111 11011001 11100011 10101100(msb)
按照文档中的规定,每个字节是按MSB存储的,则写成十六进制如下:
0x17 d9 e3 ac
而在一个字,即32位里,是按高字节序存储的,所以最终的存储值如下:
0xace3d917
另外,在填充长度时,两个字也是按LSB存储,每个字内是按上面所说的存储,具体原文如下:
A 64-bit representation of b (the length of the message before the
padding bits were added) is appended to the result of the previous
step. In the unlikely event that b is greater than 2^64, then only
the low-order 64 bits of b are used. (These bits are appended as two
32-bit words and appended low-order word first in accordance with the
previous conventions.)
2.迭代时,数据存储格式
虽然官方文档规定了数据该怎样存储,但是在真正实现时,就连官方给的DEMO也没有完全遵守。以至于自己实现md5算法时,算出来的结果总是不对。
md5实现时,分3步:
第一步,分割输入数据,填充数据和长度。
在这一步,原生的数据和填充的数据(0x80 00 00 ...)是按高字节序存储的。而填充的长度须按上面所说的规定存储&#x