ENGLISH 意见建议 网站地图 网站帮助
广泛智力汇聚   高效成果传播   先进机制培育
联盟首页  |  协同开发  |  开放源码库  |  安全告警  |  开源导航  |  文档中心  |  服务支持  |  共创论坛  |  关于联盟


注册会员 网站帮助
    您的位置 »
    今天是: 2010年11月22日    
项目搜索

完全匹配   
开源软件
软件分类表
新发布软件
其它网站镜像
代码片断
协同开发
文档
论坛
寻求协助
热点项目
站点状态
编译工厂

联系我们
关于联盟

代码片段库:
查看代码片段

浏览 | 提交新的代码片段 | 创建代码包

节省资源的MD5算法

类型:
Class
类别:
Other
许可证:
Other
语言:
Java
 
描述:
为一个字节或字符数组或串计算md5值,内部只创建一个数组,节省分配时间,节省内存
完全是static方法,线程安全
注意对返回值的处理,见注释

该代码片段的版本系列:

片段ID 下载版本 提交时间 提交人 删除
48450.12004-05-16 01:30crazybird21

点击"下载版本"来下载该代码片段.


最新版本的代码片段: 0.1


/** @author crazybird21 */

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;
	}
}

		

提交新版本

如果您修改了一个代码片段并且觉得很应该让别人共享,您可以把这作为这个代码片段的最新版本提交上来.


联盟团体会员
合作伙伴
© 共创软件联盟 版权所有
联盟服务条款 | 联盟隐私权规则 | 联系我们
电话: (8610)68313388-5949 | 传真: (8610)88377936
京ICP备05056057号