前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >节省资源的MD5算法

节省资源的MD5算法

作者头像
阿敏总司令
发布2019-02-28 14:55:56
4330
发布2019-02-28 14:55:56
举报
文章被收录于专栏:简单就是美!

阅读更多

public final class Md5

{

/** return BytesMd5 (bytes, 0, Integer.MAX_VALUE) */

public static int[] BytesMd5(byte[] bytes)

{

return BytesMd5(bytes, 0, Integer.MAX_VALUE);

}

/**

* give the bytes and get the md result

* null bytes means from and end1 is 0

* return value is locally allocated array

* return[0] is highest 32 bits, return[3] is lowest 32 bits

* (((long)return[0]) << 32) | (return[1] & 0xFFFFFFFFL) is high 64 bits

* (((long)return[2]) << 32) | (return[3] & 0xFFFFFFFFL) is low 64 bits

* thread-safe without synchronized

*/

public static int[] BytesMd5(byte[] bytes, int from, int end1)

{

// bytes = Util.MaskNull(bytes);

// from = Util.Bound(from, 0, bytes.length);

// end1 = Util.Bound(end1, from, bytes.length);

/** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

add code for checking the arguments here

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/

int[] x = new int[20]; // x[16-19] means the md result in little-endian

x[16] = 0x67452301;

x[17] = 0xEFCDAB89;

x[18] = 0x98BADCFE;

x[19] = 0x10325476;

int n;

int d;

for (n = 0, d = from; d < end1 - 3; d += 4)

{

x[n++] = (bytes[d] & 0xFF) | ((bytes[d + 1] & 0xFF) << 8) //

| ((bytes[d + 2] & 0xFF) << 16) | ((bytes[d + 3] & 0xFF) << 24);

if (n == 16)

{

Transform(x);

n = 0;

}

}

if (d == end1)

x[n++] = 0x80;

else if (d == end1 - 1)

x[n++] = (bytes[d] & 0xFF) | 0x8000;

else if (d == end1 - 2)

x[n++] = (bytes[d] & 0xFF) | ((bytes[d + 1] & 0xFF) << 8) | 0x800000;

else /* d == len - 3 */

x[n++] = (bytes[d] & 0xFF) | ((bytes[d + 1] & 0xFF) << 8) //

| ((bytes[d + 2] & 0xFF) << 16) | 0x80000000;

if (n == 15)

x[n++] = 0;

if (n == 16)

{

Transform(x);

n = 0;

}

if (n < 14)

for ( ; n < 14; n++)

x[n] = 0;

x[14] = (end1 - from) << 3;

x[15] = (end1 - from) >> 29;

Transform(x);

x[0] = x[19];

x[1] = x[18];

x[2] = x[17];

x[3] = x[16];

return x;

}

/** return UnicodeMd5 (chars, 0, Integer.MAX_VALUE) */

public static int[] UnicodeMd5(char[] chars)

{

return UnicodeMd5(chars, 0, Integer.MAX_VALUE);

}

/**

* give the unicode chars and get the md result

* null chars means from and end1 is 0

* each unicode char is treated as two bytes in big-endian

* return value is locally allocated array

* return[0] is highest 32 bits, return[3] is lowest 32 bits

* (((long)return[0]) << 32) | (return[1] & 0xFFFFFFFFL) is high 64 bits

* (((long)return[2]) << 32) | (return[3] & 0xFFFFFFFFL) is low 64 bits

* thread-safe without synchronized

*/

public static int[] UnicodeMd5(char[] chars, int from, int end1)

{

// chars = Util.MaskNull(chars);

// from = Util.Bound(from, 0, chars.length);

// end1 = Util.Bound(end1, from, chars.length);

/** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

add code for checking the arguments here

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/

int[] x = new int[20]; // x[16-19] means the md result in little-endian

x[16] = 0x67452301;

x[17] = 0xEFCDAB89;

x[18] = 0x98BADCFE;

x[19] = 0x10325476;

int n;

int d;

for (n = 0, d = from; d < end1 - 1; d += 2)

{

x[n++] = (chars[d] >>> 8) | ((chars[d] & 0xFF) << 8) //

| ((chars[d + 1] >>> 8) << 16) | ((chars[d + 1] & 0xFF) << 24);

if (n == 16)

{

Transform(x);

n = 0;

}

}

if (d == end1)

x[n++] = 0x80;

else /* d == end1 - 1 */

x[n++] = (chars[d] >>> 8) | ((chars[d] & 0xFF) << 8) | 0x800000;

if (n == 15)

x[n++] = 0;

if (n == 16)

{

Transform(x);

n = 0;

}

if (n < 14)

for ( ; n < 14; n++)

x[n] = 0;

x[14] = (end1 - from) << 4;

x[15] = (end1 - from) >> 28;

Transform(x);

x[0] = x[19];

x[1] = x[18];

x[2] = x[17];

x[3] = x[16];

return x;

}

/** return UnicodeMd5(s, 0, Integer.MAX_VALUE) */

public static int[] UnicodeMd5(String s)

{

return UnicodeMd5(s, 0, Integer.MAX_VALUE);

}

/**

* give the unicode str and get the md result

* null str means from and end1 is 0

* each unicode char is treated as two bytes in big-endian

* return value is locally allocated array

* return[0] is highest 32 bits, return[3] is lowest 32 bits

* (((long)return[0]) << 32) | (return[1] & 0xFFFFFFFFL) is high 64 bits

* (((long)return[2]) << 32) | (return[3] & 0xFFFFFFFFL) is low 64 bits

* thread-safe without synchronized

*/

public static int[] UnicodeMd5(String str, int from, int end1)

{

// str = Util.MaskNull(str);

// from = Util.Bound(from, 0, str.length());

// end1 = Util.Bound(end1, from, str.length());

/** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

add code for checking the arguments here

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/

int[] x = new int[20]; // x[16-19] means the md result in little-endian

x[16] = 0x67452301;

x[17] = 0xEFCDAB89;

x[18] = 0x98BADCFE;

x[19] = 0x10325476;

int n;

int d;

for (n = 0, d = from; d < end1 - 1; d += 2)

{

x[n++] = (str.charAt(d) >>> 8) | ((str.charAt(d) & 0xFF) << 8) //

| ((str.charAt(d + 1) >>> 8) << 16) | ((str.charAt(d + 1) & 0xFF) << 24);

if (n == 16)

{

Transform(x);

n = 0;

}

}

if (d == end1)

x[n++] = 0x80;

else /* d == end1 - 1 */

x[n++] = (str.charAt(d) >>> 8) | ((str.charAt(d) & 0xFF) << 8) | 0x800000;

if (n == 15)

x[n++] = 0;

if (n == 16)

{

Transform(x);

n = 0;

}

if (n < 14)

for ( ; n < 14; n++)

x[n] = 0;

x[14] = (end1 - from) << 4;

x[15] = (end1 - from) >> 28;

Transform(x);

x[0] = x[19];

x[1] = x[18];

x[2] = x[17];

x[3] = x[16];

return x;

}

private static int F(int x, int y, int z)

{

return (x & y) | ((~x) & z);

}

private static int G(int x, int y, int z)

{

return (x & z) | (y & (~z));

}

private static int H(int x, int y, int z)

{

return x ^ y ^ z;

}

private static int I(int x, int y, int z)

{

return y ^ (x | (~z));

}

private static int FF(int a, int b, int c, int d, int x, int s, int ac)

{

a += F(b, c, d) + x + ac;

return ((a << s) | (a >>> (32 - s))) + b;

}

private static int GG(int a, int b, int c, int d, int x, int s, int ac)

{

a += G(b, c, d) + x + ac;

return ((a << s) | (a >>> (32 - s))) + b;

}

private static int HH(int a, int b, int c, int d, int x, int s, int ac)

{

a += H(b, c, d) + x + ac;

return ((a << s) | (a >>> (32 - s))) + b;

}

private static int II(int a, int b, int c, int d, int x, int s, int ac)

{

a += I(b, c, d) + x + ac;

return ((a << s) | (a >>> (32 - s))) + b;

}

private static void Transform(int[] x)

{

int a = x[16], b = x[17], c = x[18], d = x[19];

//

// Round 1

a = FF(a, b, c, d, x[0], 7, 0xD76AA478);

d = FF(d, a, b, c, x[1], 12, 0xE8C7B756);

c = FF(c, d, a, b, x[2], 17, 0x242070DB);

b = FF(b, c, d, a, x[3], 22, 0xC1BDCEEE);

//

a = FF(a, b, c, d, x[4], 7, 0xF57C0FAF);

d = FF(d, a, b, c, x[5], 12, 0x4787C62A);

c = FF(c, d, a, b, x[6], 17, 0xA8304613);

b = FF(b, c, d, a, x[7], 22, 0xFD469501);

//

a = FF(a, b, c, d, x[8], 7, 0x698098D8);

d = FF(d, a, b, c, x[9], 12, 0x8B44F7AF);

c = FF(c, d, a, b, x[10], 17, 0xFFFF5BB1);

b = FF(b, c, d, a, x[11], 22, 0x895CD7BE);

//

a = FF(a, b, c, d, x[12], 7, 0x6B901122);

d = FF(d, a, b, c, x[13], 12, 0xFD987193);

c = FF(c, d, a, b, x[14], 17, 0xA679438E);

b = FF(b, c, d, a, x[15], 22, 0x49B40821);

//

// Round 2

a = GG(a, b, c, d, x[1], 5, 0xF61E2562);

d = GG(d, a, b, c, x[6], 9, 0xC040B340);

c = GG(c, d, a, b, x[11], 14, 0x265E5A51);

b = GG(b, c, d, a, x[0], 20, 0xE9B6C7AA);

//

a = GG(a, b, c, d, x[5], 5, 0xD62F105D);

d = GG(d, a, b, c, x[10], 9, 0x2441453);

c = GG(c, d, a, b, x[15], 14, 0xD8A1E681);

b = GG(b, c, d, a, x[4], 20, 0xE7D3FBC8);

//

a = GG(a, b, c, d, x[9], 5, 0x21E1CDE6);

d = GG(d, a, b, c, x[14], 9, 0xC33707D6);

c = GG(c, d, a, b, x[3], 14, 0xF4D50D87);

b = GG(b, c, d, a, x[8], 20, 0x455A14ED);

//

a = GG(a, b, c, d, x[13], 5, 0xA9E3E905);

d = GG(d, a, b, c, x[2], 9, 0xFCEFA3F8);

c = GG(c, d, a, b, x[7], 14, 0x676F02D9);

b = GG(b, c, d, a, x[12], 20, 0x8D2A4C8A);

//

// Round 3

a = HH(a, b, c, d, x[5], 4, 0xFFFA3942);

d = HH(d, a, b, c, x[8], 11, 0x8771F681);

c = HH(c, d, a, b, x[11], 16, 0x6D9D6122);

b = HH(b, c, d, a, x[14], 23, 0xFDE5380C);

//

a = HH(a, b, c, d, x[1], 4, 0xA4BEEA44);

d = HH(d, a, b, c, x[4], 11, 0x4BDECFA9);

c = HH(c, d, a, b, x[7], 16, 0xF6BB4B60);

b = HH(b, c, d, a, x[10], 23, 0xBEBFBC70);

//

a = HH(a, b, c, d, x[13], 4, 0x289B7EC6);

d = HH(d, a, b, c, x[0], 11, 0xEAA127FA);

c = HH(c, d, a, b, x[3], 16, 0xD4EF3085);

b = HH(b, c, d, a, x[6], 23, 0x4881D05);

//

a = HH(a, b, c, d, x[9], 4, 0xD9D4D039);

d = HH(d, a, b, c, x[12], 11, 0xE6DB99E5);

c = HH(c, d, a, b, x[15], 16, 0x1FA27CF8);

b = HH(b, c, d, a, x[2], 23, 0xC4AC5665);

//

// Round 4

a = II(a, b, c, d, x[0], 6, 0xF4292244);

d = II(d, a, b, c, x[7], 10, 0x432AFF97);

c = II(c, d, a, b, x[14], 15, 0xAB9423A7);

b = II(b, c, d, a, x[5], 21, 0xFC93A039);

//

a = II(a, b, c, d, x[12], 6, 0x655B59C3);

d = II(d, a, b, c, x[3], 10, 0x8F0CCC92);

c = II(c, d, a, b, x[10], 15, 0xFFEFF47D);

b = II(b, c, d, a, x[1], 21, 0x85845DD1);

//

a = II(a, b, c, d, x[8], 6, 0x6FA87E4F);

d = II(d, a, b, c, x[15], 10, 0xFE2CE6E0);

c = II(c, d, a, b, x[6], 15, 0xA3014314);

b = II(b, c, d, a, x[13], 21, 0x4E0811A1);

//

a = II(a, b, c, d, x[4], 6, 0xF7537E82);

d = II(d, a, b, c, x[11], 10, 0xBD3AF235);

c = II(c, d, a, b, x[2], 15, 0x2AD7D2BB);

b = II(b, c, d, a, x[9], 21, 0xEB86D391);

//

//

x[16] += a;

x[17] += b;

x[18] += c;

x[19] += d;

}

}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2005-04-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档