|
发表于 2007-1-1 21:28:31
|
显示全部楼层
555,现在不管这个老。
白银大人帮偶看偶的代码。研究老一天老。
现在眼晕老。其实基本过程好像就这样老。
很有可能是那4个0x01234567。我见RFC上的是67452301。
还有一个可能就是我的I2B。I2B。B2L有问题。
再有的话就是填充方法。
或者最后输出那四个32位整数有问题。
再或者。
我把他们相加本来就有问题。
5555。总之帮偶看一下吧。
- package test;
- public class MyMD5 {
- // private static final int[][] S = { { 7, 12, 17, 22 }, { 5, 9, 14, 20 },
- // { 4, 11, 16, 23 }, { 6, 10, 15, 21 } };
- private static final int S11 = 7;
- private static final int S12 = 12;
- private static final int S13 = 17;
- private static final int S14 = 22;
- private static final int S21 = 5;
- private static final int S22 = 9;
- private static final int S23 = 14;
- private static final int S24 = 20;
- private static final int S31 = 4;
- private static final int S32 = 11;
- private static final int S33 = 16;
- private static final int S34 = 23;
- private static final int S41 = 6;
- private static final int S42 = 10;
- private static final int S43 = 15;
- private static final int S44 = 21;
- private static final int[] STATE = {
- 0x01234567,
- 0x89abcdef,
- 0xfedcba98,
- 0x76543210 };
- /**
- * 缓存的512位信息数组(64个byte)
- */
- private byte[] cache;
- /**
- * 当前缓存的长度
- */
- private byte cacheLength;
- /**
- * 已经计算的长度
- */
- private long coreLength;
- /**
- * MD5中的第一个32位分组
- */
- private int a;
- /**
- * MD5中的第二个32位分组
- */
- private int b;
- /**
- * MD5中的第三个32位分组
- */
- private int c;
- /**
- * MD5中的第四个32位分组
- */
- private int d;
- /**
- * 把一个字节数组转换为整数
- *
- * @param b
- * 字节数组
- * @return 转换后的整数
- */
- public int B2I(byte[] b) {
- return B2I(b, 0, b.length);
- }
- public int B2I(byte[] b, int off, int len) {
- len = (len <= 4) ? len : 4;
- int result = 0;
- // 开始计算
- for (int i = 0; i < len; i++) {
- int temp = b[(len - i - 1) + off] & 0xFF;
- result += temp * StrictMath.pow(0x100, i);
- }
- return result;
- }
- /**
- * 将一个整数转换为一个字节数组
- *
- * @param i
- * 一个整数
- * @return 转换后的字节数组
- */
- public void I2B(int i, byte[] b, int off) {
- if (off + 4 > b.length) {
- return;
- }
- b[0 + off] = (byte) (i / 0x1000000);
- b[1 + off] = (byte) (i / 0x10000);
- b[2 + off] = (byte) (i / 0x100);
- b[3 + off] = (byte) (i);
- }
-
- public void L2B(long l, byte[] b, int off){
- if (off + 8 > b.length) {
- return;
- }
- b[0 + off] = (byte) (l / 0x100000000000000l);
- b[1 + off] = (byte) (l / 0x1000000000000l);
- b[2 + off] = (byte) (l / 0x10000000000l);
- b[3 + off] = (byte) (l / 0x100000000l);
- b[4 + off] = (byte) (l / 0x1000000);
- b[5 + off] = (byte) (l / 0x10000);
- b[6 + off] = (byte) (l / 0x100);
- b[7 + off] = (byte) (l);
- }
-
- public MyMD5(){
- init();
- }
- /**
- * 初始化当前计算
- *
- */
- private void init() {
- cache = new byte[64];
- cacheLength = 0;
- coreLength = 0;
- a = 0;
- b = 0;
- c = 0;
- d = 0;
- }
- private int F(int x, int y, int z) {
- return ((x & y) | (~x) & z);
- }
- private int G(int x, int y, int z) {
- return ((x & z) | (y & (~z)));
- }
- private int H(int x, int y, int z) {
- return (x ^ y ^ z);
- }
- private int I(int x, int y, int z) {
- return (y ^ (x | (~z)));
- }
- private int ROTATE_LEFT(int x, int n) {
- return (((x) << (n)) | ((x) >> (32 - (n))));
- }
- /**
- * MD5中的第一轮计算函数
- *
- * @return
- */
- private int FF(int a, int b, int c, int d, int x, int s, int ac) {
- a += F(b, c, d) + x + ac;
- //a = ROTATE_LEFT(a, s);
- a += b;
- return a;
- }
- /**
- * MD5中的第二轮计算函数
- *
- * @return
- */
- private int GG(int a, int b, int c, int d, int x, int s, int ac) {
- a += G(b, c, d) + x + ac;
- //a = ROTATE_LEFT(a, s);
- a += b;
- return a;
- }
- /**
- * MD5中的第三轮计算函数
- *
- * @return
- */
- private int HH(int a, int b, int c, int d, int x, int s, int ac) {
- a += H(b, c, d) + x + ac;
- //a = ROTATE_LEFT(a, s);
- a += b;
- return a;
- }
- /**
- * MD5中的第四轮计算函数
- *
- * @return
- */
- private int II(int a, int b, int c, int d, int x, int s, int ac) {
- a += I(b, c, d) + x + ac;
- //a = ROTATE_LEFT(a, s);
- a += b;
- return a;
- }
- /**
- * 计算当前缓存的结果
- *
- */
- private void calculate() {
- /**
- * 检查是否缓冲完成
- */
- if (cacheLength != 64) {
- return;
- }
- int[] x = new int[16];
- for (int i = 0; i < x.length; i++) {
- x[i] = B2I(cache, i * 4, 4);
- }
- int a = STATE[0];
- int b = STATE[1];
- int c = STATE[2];
- int d = STATE[3];
- /**
- * 第一轮
- */
- a += FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
- d += FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
- c += FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */
- b += FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
- a += FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
- d += FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
- c += FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
- b += FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
- a += FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
- d += FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
- c += FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
- b += FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
- a += FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
- d += FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
- c += FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
- b += FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
- /**
- * 第二轮
- */
- a += GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
- d += GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
- c += GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
- b += GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
- a += GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
- d += GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
- c += GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
- b += GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
- a += GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
- d += GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
- c += GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
- b += GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
- a += GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
- d += GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
- c += GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
- b += GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
- /**
- * 第三轮
- */
- a += HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
- d += HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
- c += HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
- b += HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
- a += HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
- d += HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
- c += HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
- b += HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
- a += HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
- d += HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
- c += HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
- b += HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */
- a += HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
- d += HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
- c += HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
- b += HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
- /**
- * 第四轮
- */
- a += II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
- d += II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
- c += II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
- b += II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
- a += II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
- d += II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
- c += II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
- b += II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
- a += II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
- d += II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
- c += II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
- b += II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
- a += II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
- d += II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
- c += II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
- b += II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
- /**
- * 处理结果
- */
- this.a += a;
- this.b += b;
- this.c += c;
- this.d += d;
- cacheLength = 0;
- coreLength += 64;
- }
- /**
- * 更新当前计算结果
- *
- * @param b
- */
- public void update(byte[] b) {
- int off = 0;
- int len = 0;
- int bLen = b.length;
- while (off < bLen) {
- len = Math.min(cache.length - (cacheLength), bLen - off);
- System.arraycopy(b, off, cache, cacheLength, len);
- cacheLength += len;
- off += len;
- calculate();
- }
- }
- /**
- * 填充最后的字段
- *
- */
- private void fill() {
- long msgLength = coreLength+(cacheLength*8);
- int fillCount = 56 - cacheLength;
-
- /**
- * 如果填充位置不够,则再加多512位。
- */
- if(fillCount < 0){
- fillCount = 64 - fillCount;
- }
-
- /**
- * 如果需要填充,则生成填充字节。
- */
- if(fillCount >0){
- byte[] b = new byte[fillCount];
- b[0] = -128;
- this.update(b);
- }
- L2B(msgLength,cache,cacheLength);
- cacheLength+=8;
- calculate();
- }
- /**
- * 完成最终的计算
- *
- * @return 计算后的128位散列
- */
- public byte[] digest() {
- byte[] result = new byte[16];
- // 填充
- fill();
- // 计算结果
- I2B(a, result, 0);
- I2B(b, result, 4);
- I2B(c, result, 8);
- I2B(d, result, 12);
- // 计算完成以后重置
- init();
- return result;
- }
- }
复制代码 |
|