javascript实现md5算法(支持微信小程序),可分多次计算

概述:

本人前端需要实现md5计算的功能,最好是能做到分多次计算。

本文所写的代码在现有md5的C++代码,反复测试对比计算过程参数,成功改造成md5的javascript代码,并成功验证好分多次计算md5数据

测试平台:

已经在如下环境中测试通过,其他平台(浏览器)应该也不会有问题:

1、nodejs中node.exe运行

2、微信小程序

功能代码:

javascript 复制代码
function strMd5CtxParams() {
	this.md5count;
	this.state;
	this.buffer;
};

function md5_init() {
	var md5CtxParams = new strMd5CtxParams();
	
	md5CtxParams.md5count = new Array(2);
	md5CtxParams.state = new Array(4);
	md5CtxParams.buffer = new Uint8Array(64);
	
	md5CtxParams.md5count[0] = 0;
	md5CtxParams.md5count[1] = 0;
	md5CtxParams.state[0] = 0x67452301;
	md5CtxParams.state[1] = 0xEFCDAB89;
	md5CtxParams.state[2] = 0x98BADCFE;
	md5CtxParams.state[3] = 0x10325476;
	
	return md5CtxParams;
}

function md5_update(md5CtxParams, data, datalen) {
	//unsigned int i = 0;
	//unsigned int index = 0;
	//unsigned int partlen = 0;
	var index = 0;
	var i = 0;
  var j = 0;
  var partlen = 0;

	//index = (m_Md5Count[0] >> 3) & 0x3F;
	index = (md5CtxParams.md5count[0] >>> 3) & 0x3F;
	
	partlen = 64 - index;
	//m_Md5Count[0] += (inputlen << 3);
	md5CtxParams.md5count[0] = bn_u32_add(md5CtxParams.md5count[0], datalen << 3);
	//if(m_Md5Count[0] < (inputlen << 3)) {
	if (bn_u32_compare(md5CtxParams.md5count[0], datalen << 3) < 0) {
		//m_Md5Count[1]++;
		md5CtxParams.md5count[1] = bn_u32_add(md5CtxParams.md5count[1], 1);
	}
	//m_Md5Count[1] += (inputlen >> 29);
	md5CtxParams.md5count[1] = bn_u32_add(md5CtxParams.md5count[1], datalen >>> 29);

	if(datalen >= partlen) {
		//memcpy(&m_Buffer[index],input,partlen);
		for (j = 0; j < partlen; j++) {
			md5CtxParams.buffer[index + j] = data[j];
		}
		//Transform(m_State,m_Buffer);
		Transform(md5CtxParams.state, md5CtxParams.buffer, 0);
		for(i = partlen;i+64 <= datalen;i+=64) {
			//Transform(m_State, (unsigned char*)&input[i]);
			Transform(md5CtxParams.state, data, i);
		}
		index = 0;        
	}  else {
		i = 0;
		//console.log("iii");
	}
	//memcpy(&m_Buffer[index],&input[i],inputlen-i);
	for (j = 0; j < datalen-i; j++) {
		md5CtxParams.buffer[index + j] = data[i + j];
	}
}

function md5_final(md5CtxParams) {
	var PADDING=[0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
	var index = 0,padlen = 0;
	var result = new Uint8Array(16);
	var bits = new Uint8Array(8);
	var i = 0;
	
	//index = (m_Md5Count[0] >> 3) & 0x3F;
	index = (md5CtxParams.md5count[0] >> 3) & 0x3F;
	padlen = (index < 56) ? (56-index) : (120-index);
	Encode(bits, md5CtxParams.md5count, 0, 8);
	md5_update(md5CtxParams, PADDING, padlen);
	md5_update(md5CtxParams, bits, 8);
	
	//console.log("md5_final = %d %d %d %d", md5CtxParams.state[0], md5CtxParams.state[1], md5CtxParams.state[2], md5CtxParams.state[3]);
	Encode(result, md5CtxParams.state, 0, 16);
	
	return result;
}

function md5_str_to_array(strIn) {
	var bytesArray = new Uint8Array(strIn.length);
	var i = 0;
	
	for (i = 0; i < strIn.length; i++) {
		bytesArray[i] = strIn.charCodeAt(i);
	}
	
	return bytesArray;
}


function md5_hex_to_array(hexStrIn) {
  var i = 0;
  var cnt = 0;
  var ele = 0;
  var bytesArray = null;

  cnt = 0;
  for (i = 0; i < hexStrIn.length; i++) {
    ele = hexStrIn.charCodeAt(i);
    if (ele >= 48 && ele < 48 + 10) {
      cnt++;
    }
    if (ele >= 65 && ele < 65 + 6) {
      cnt++;
    }
    if (ele >= 97 && ele < 97 + 6) {
      cnt++;
    }
  }
  bytesArray = new Uint8Array(parseInt((cnt + 1) / 2));
  cnt = 0;
  for (i = 0; i < hexStrIn.length; i++) {
    ele = hexStrIn.charCodeAt(i);
    if (ele >= 48 && ele < 48 + 10) {
      ele -= 48;
      cnt++;
    } else if (ele >= 65 && ele < 65 + 6) {
      ele = ele - 65 + 10;
      cnt++;
    } else if (ele >= 97 && ele < 97 + 6) {
      ele = ele - 97 + 10;
      cnt++;
    } else {
      continue;
    }
    if ((cnt % 2) == 1) {
      bytesArray[parseInt((cnt - 1) / 2)] = (ele << 4) & 0xF0;
    } else {
      bytesArray[parseInt((cnt - 1) / 2)] |= ele;
    }
  }

  return bytesArray;
}

function md5_encode_hex(result, len) {
	var sha256_hex_digits = "0123456789abcdef";
	var output = new String();
	var i = 0;

	for (i = 0; i < len; i++) {
		output += sha256_hex_digits.charAt((result[i] >>> 4) & 0x0f);
		output += sha256_hex_digits.charAt((result[i]) & 0x0f);
	}
	
	return output;
}

function F(x,y,z) {
	return ((x & y) | (~x & z));
}

function G(x,y,z) {
	return ((x & z) | (y & ~z));
}

function H(x,y,z) {
	return (x^y^z);
}

function I(x,y,z) {
	return (y ^ (x | ~z));
}

function ROTATE_LEFT(x, n) {
	x = x & 0xffffffff;
	return ((x << n) | (x >>> (32-n)));
}

function FF(a, b, c, d, x, s, ac) {
	//a += F(b,c,d) + x + ac;
	a = bn_u32_add(a, F(b,c,d));
	a = bn_u32_add(a, x);
	a = bn_u32_add(a, ac);
	
	a = ROTATE_LEFT(a,s);
	//a += b;
	a = bn_u32_add(a, b);
	return a;
}

function GG(a,b,c,d,x,s,ac) { 
	//a += G(b,c,d) + x + ac; 
	a = bn_u32_add(a, G(b,c,d));
	a = bn_u32_add(a, x);
	a = bn_u32_add(a, ac);
	a = ROTATE_LEFT(a,s); 
	//a += b; 
	a = bn_u32_add(a, b);
	
	return a;
}

function HH(a,b,c,d,x,s,ac) { 
	//a += H(b,c,d) + x + ac; 
	a = bn_u32_add(a, H(b,c,d));
	a = bn_u32_add(a, x);
	a = bn_u32_add(a, ac);
	
	a = ROTATE_LEFT(a,s); 
	//a += b; 
	a = bn_u32_add(a, b);
	
	return a;
}

function II(a,b,c,d,x,s,ac) { 
	//a += H(b,c,d) + x + ac; 
	a = bn_u32_add(a, I(b,c,d));
	a = bn_u32_add(a, x);
	a = bn_u32_add(a, ac);
	
	a = ROTATE_LEFT(a,s); 
	//a += b; 
	a = bn_u32_add(a, b);
	
	return a;
}

function Transform(state, block, blockindex) {
	var x = new Array(64);//Uint8Array(64);
	var a = state[0];
	var b = state[1];
	var c = state[2];
	var d = state[3];
	//var i = 0;
	
	Decode(x, block, blockindex, 64);
	
	//printfIntsIndex("x", x, 0, 64);
	a = FF(a, b, c, d, x[ 0], 7, 0xd76aa478); /* 1 */
	d = FF(d, a, b, c, x[ 1], 12, 0xe8c7b756); /* 2 */
	c = FF(c, d, a, b, x[ 2], 17, 0x242070db); /* 3 */
	b = FF(b, c, d, a, x[ 3], 22, 0xc1bdceee); /* 4 */
	a = FF(a, b, c, d, x[ 4], 7, 0xf57c0faf); /* 5 */
	d = FF(d, a, b, c, x[ 5], 12, 0x4787c62a); /* 6 */
	c = FF(c, d, a, b, x[ 6], 17, 0xa8304613); /* 7 */
	b = FF(b, c, d, a, x[ 7], 22, 0xfd469501); /* 8 */
	a = FF(a, b, c, d, x[ 8], 7, 0x698098d8); /* 9 */
	d = FF(d, a, b, c, x[ 9], 12, 0x8b44f7af); /* 10 */
	c = FF(c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */
	b = FF(b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */
	a = FF(a, b, c, d, x[12], 7, 0x6b901122); /* 13 */
	d = FF(d, a, b, c, x[13], 12, 0xfd987193); /* 14 */
	c = FF(c, d, a, b, x[14], 17, 0xa679438e); /* 15 */
	b = FF(b, c, d, a, x[15], 22, 0x49b40821); /* 16 */
	//console.log("a = %d b = %d c = %d d = %d", a, b, c, d);
	//console.log("3a = %d b = %d c = %d d = %d", a, b, c, d);
	
	/* Round 2 */
	a = GG(a, b, c, d, x[ 1], 5, 0xf61e2562); /* 17 */
	//console.log("4a = %d b = %d c = %d d = %d", a, b, c, d);
	d = GG(d, a, b, c, x[ 6], 9, 0xc040b340); /* 18 */
	//console.log("5a = %d b = %d c = %d d = %d", a, b, c, d);
	c = GG(c, d, a, b, x[11], 14, 0x265e5a51); /* 19 */
	//console.log("6a = %d b = %d c = %d d = %d", a, b, c, d);
	b = GG(b, c, d, a, x[ 0], 20, 0xe9b6c7aa); /* 20 */
	//console.log("7a = %d b = %d c = %d d = %d", a, b, c, d);
	a = GG(a, b, c, d, x[ 5], 5, 0xd62f105d); /* 21 */
	//console.log("8a = %d b = %d c = %d d = %d", a, b, c, d);
	d = GG(d, a, b, c, x[10], 9,  0x2441453); /* 22 */
	//console.log("9a = %d b = %d c = %d d = %d", a, b, c, d);
	c = GG(c, d, a, b, x[15], 14, 0xd8a1e681); /* 23 */
	//console.log("10a = %d b = %d c = %d d = %d", a, b, c, d);
	b = GG(b, c, d, a, x[ 4], 20, 0xe7d3fbc8); /* 24 */
	//console.log("11a = %d b = %d c = %d d = %d", a, b, c, d);
	a = GG(a, b, c, d, x[ 9], 5, 0x21e1cde6); /* 25 */
	//console.log("12a = %d b = %d c = %d d = %d", a, b, c, d);
	d = GG(d, a, b, c, x[14], 9, 0xc33707d6); /* 26 */
	//console.log("13a = %d b = %d c = %d d = %d", a, b, c, d);
	c = GG(c, d, a, b, x[ 3], 14, 0xf4d50d87); /* 27 */
	//console.log("14a = %d b = %d c = %d d = %d", a, b, c, d);
	b = GG(b, c, d, a, x[ 8], 20, 0x455a14ed); /* 28 */
	//console.log("15a = %d b = %d c = %d d = %d", a, b, c, d);
	a = GG(a, b, c, d, x[13], 5, 0xa9e3e905); /* 29 */
	//console.log("16a = %d b = %d c = %d d = %d", a, b, c, d);
	d = GG(d, a, b, c, x[ 2], 9, 0xfcefa3f8); /* 30 */
	//console.log("17a = %d b = %d c = %d d = %d", a, b, c, d);
	c = GG(c, d, a, b, x[ 7], 14, 0x676f02d9); /* 31 */
	//console.log("18a = %d b = %d c = %d d = %d", a, b, c, d);
	b = GG(b, c, d, a, x[12], 20, 0x8d2a4c8a); /* 32 */
	//console.log("2a = %d b = %d c = %d d = %d", a, b, c, d);

	/* Round 3 */
	a = HH(a, b, c, d, x[ 5], 4, 0xfffa3942); /* 33 */
	d = HH(d, a, b, c, x[ 8], 11, 0x8771f681); /* 34 */
	c = HH(c, d, a, b, x[11], 16, 0x6d9d6122); /* 35 */
	b = HH(b, c, d, a, x[14], 23, 0xfde5380c); /* 36 */
	a = HH(a, b, c, d, x[ 1], 4, 0xa4beea44); /* 37 */
	d = HH(d, a, b, c, x[ 4], 11, 0x4bdecfa9); /* 38 */
	c = HH(c, d, a, b, x[ 7], 16, 0xf6bb4b60); /* 39 */
	b = HH(b, c, d, a, x[10], 23, 0xbebfbc70); /* 40 */
	a = HH(a, b, c, d, x[13], 4, 0x289b7ec6); /* 41 */
	d = HH(d, a, b, c, x[ 0], 11, 0xeaa127fa); /* 42 */
	c = HH(c, d, a, b, x[ 3], 16, 0xd4ef3085); /* 43 */
	b = HH(b, c, d, a, x[ 6], 23,  0x4881d05); /* 44 */
	a = HH(a, b, c, d, x[ 9], 4, 0xd9d4d039); /* 45 */
	d = HH(d, a, b, c, x[12], 11, 0xe6db99e5); /* 46 */
	c = HH(c, d, a, b, x[15], 16, 0x1fa27cf8); /* 47 */
	b = HH(b, c, d, a, x[ 2], 23, 0xc4ac5665); /* 48 */
	//console.log("1a = %d b = %d c = %d d = %d", a, b, c, d);
	
	/* Round 4 */
	a = II(a, b, c, d, x[ 0], 6, 0xf4292244); /* 49 */
	d = II(d, a, b, c, x[ 7], 10, 0x432aff97); /* 50 */
	c = II(c, d, a, b, x[14], 15, 0xab9423a7); /* 51 */
	b = II(b, c, d, a, x[ 5], 21, 0xfc93a039); /* 52 */
	a = II(a, b, c, d, x[12], 6, 0x655b59c3); /* 53 */
	d = II(d, a, b, c, x[ 3], 10, 0x8f0ccc92); /* 54 */
	c = II(c, d, a, b, x[10], 15, 0xffeff47d); /* 55 */
	b = II(b, c, d, a, x[ 1], 21, 0x85845dd1); /* 56 */
	a = II(a, b, c, d, x[ 8], 6, 0x6fa87e4f); /* 57 */
	d = II(d, a, b, c, x[15], 10, 0xfe2ce6e0); /* 58 */
	c = II(c, d, a, b, x[ 6], 15, 0xa3014314); /* 59 */
	b = II(b, c, d, a, x[13], 21, 0x4e0811a1); /* 60 */
	a = II(a, b, c, d, x[ 4], 6, 0xf7537e82); /* 61 */
	d = II(d, a, b, c, x[11], 10, 0xbd3af235); /* 62 */
	c = II(c, d, a, b, x[ 2], 15, 0x2ad7d2bb); /* 63 */
	b = II(b, c, d, a, x[ 9], 21, 0xeb86d391); /* 64 */

	//console.log("0a = %d b = %d c = %d d = %d", a, b, c, d);
	//console.log("pre state = %d %d %d %d", state[0], state[1], state[2], state[3]);
	
	//state[0] += a;
	state[0] = bn_u32_add(state[0], a);
	//state[1] += b;
	state[1] = bn_u32_add(state[1], b);
	//state[2] += c;
	state[2] = bn_u32_add(state[2], c);
	//state[3] += d;
	state[3] = bn_u32_add(state[3], d);
	
	//throw Error("11");
	//console.log("state = %d %d %d %d", state[0], state[1], state[2], state[3]);
	//throw Error("11");
}

function Decode(output, input, inputindex, len)
{
	var i = 0;
	var j = 0;

	while(j < len) {
		output[i] = ((input[j +inputindex]) & 0xff) |
			((input[j+1 + inputindex] & 0xff) << 8) |
			((input[j+2 + inputindex] & 0xff) << 16) |
			((input[j+3 + inputindex] & 0xff) << 24);
		i++;
		j+=4; 
	}
}

function Encode(output, input, inputindex, len)
{
	//unsigned int i = 0;
	//unsigned int j = 0;
	var i = 0;
	var j = 0;

	while(j < len) {
		output[j] = input[i + inputindex] & 0xFF;  
		output[j+1] = (input[i + inputindex] >>> 8) & 0xFF;
		output[j+2] = (input[i + inputindex] >>> 16) & 0xFF;
		output[j+3] = (input[i + inputindex] >>> 24) & 0xFF;
		i++;
		j+=4;
	}
}

/*
function printfInts(title, data, len) {
	printfIntsIndex(title, data, 0, len);
}

function printfIntsIndex(title, data, index, len) {
	var i = 0;
	
	for (i = 0; i < len; i++) {
		console.log("%d %s %d", i, title == null ? "":title, data[i + index]);
	}
}*/

function bn_u32_compare(uint1, uint2) {
	uint1 &= 0xffffffff;
	uint2 &= 0xffffffff;
	
	if ((uint1 & 0x80000000) == 0 && (uint2 & 0x80000000) == 0) {
		return uint1 > uint2 ? 1 : (uint1 == uint2 ? 0 : (-1));
	}
	if ((uint1 & 0x80000000) != 0 && (uint2 & 0x80000000) != 0) {
		uint1 &= 0x7fffffff;
		uint2 &= 0x7fffffff;
		return uint1 > uint2 ? 1 : (uint1 == uint2 ? 0 : (-1));
	}
	if ((uint1 & 0x80000000) != 0) {
		return 1;
	} else {
		return -1;
	}
}

function bn_u32_add(add1, add2) {
	var i = 0;
	var tmp = 0;
	var result = 0;
	
	//add1 &= 0xffffffff;
	//add2 &= 0xffffffff;
	
	for (i = 0; i < 4; i++) {
		tmp = ((add1 >>> (i * 8)) & 0xff) + ((add2 >>> (i * 8)) & 0xff) + tmp;
		result |= ((tmp & 0xff) << (i * 8));
		tmp = tmp >>> 8;
	}
	
	return result & 0xffffffff;
}
/*
function bn_u32_minus(minuend, subtractor) {
	var a = (minuend & 0xffffffff) >>> 0;
	var b = (subtractor & 0xffffffff) >>> 0;
	
	return (a - b) & 0xffffffff;
}
*/

module.exports = {
  md5_init: md5_init,
  md5_update: md5_update,
  md5_final: md5_final,
  md5_encode_hex: md5_encode_hex,
  md5_str_to_array: md5_str_to_array,
  md5_hex_to_array: md5_hex_to_array,
}

/*
var data1 = "Decrypt";
var data0 = "1234567890abcdef9999oplk8563plmh6003EBFF27AEDE040312E8D8159A91AE0149CDC9EE6775DE602739DB660D241935E8AA847EA015309664C1EBA947E0F043B8CD71240DE448F1D0B758A21E80009BA39A2799515469338E75B810C7550D2B0144DFDCEE981E3250B70F094A2AC98183DFF1B889B8249006ADAEA39504F5F4E8D3581E606D97029880DF721764FF94CD8E66BC1C924DE4451921BB95AF20DA4A9510FDDCCAF0AF88C520D5F6C942446D39079B072429FCA222250F88B753BEA4061E02364528BFEC8F66F4F67CA82CA33DEA0EDFE8FD6E4BF9226B87DB28C0DA0A776CFBD2E4977C0F1E738A443D9D4BE434B9A9F1D9B24D2ACBD7510928A8D4767E8EA8DEC66FF6587237956309";
var data2 = "96325";
//char buf[128];

//EZERO_ARRAY(buf);
var md5ctx = null;
var result = null;

// 字符串 
md5ctx = md5_init();
md5_update(md5ctx, md5_str_to_array(data0), data0.length);
md5_update(md5ctx, md5_str_to_array(data1), data1.length);
md5_update(md5ctx, md5_str_to_array(data2), data2.length);
result = md5_final(md5ctx);
console.log("result:" + md5_encode_hex(result, 16));

// hex 
md5ctx = md5_init();
md5_update(md5ctx, md5_hex_to_array("001122334455"), 6);
md5_update(md5ctx, md5_hex_to_array("AABBCCDDEEFF"), 6);
md5_update(md5ctx, md5_hex_to_array("88"), 1);
result = md5_final(md5ctx);
console.log("result:" + md5_encode_hex(result, 16));
*/

测试代码:

如下在微信小程序中测试验证

javascript 复制代码
var js_md5 = require('../../crypto/md5.js');
testMd5() {
    var data1 = "Decrypt";
    var data0 = "1234567890abcdef9999oplk8563plmh6003EBFF27AEDE040312E8D8159A91AE0149CDC9EE6775DE602739DB660D241935E8AA847EA015309664C1EBA947E0F043B8CD71240DE448F1D0B758A21E80009BA39A2799515469338E75B810C7550D2B0144DFDCEE981E3250B70F094A2AC98183DFF1B889B8249006ADAEA39504F5F4E8D3581E606D97029880DF721764FF94CD8E66BC1C924DE4451921BB95AF20DA4A9510FDDCCAF0AF88C520D5F6C942446D39079B072429FCA222250F88B753BEA4061E02364528BFEC8F66F4F67CA82CA33DEA0EDFE8FD6E4BF9226B87DB28C0DA0A776CFBD2E4977C0F1E738A443D9D4BE434B9A9F1D9B24D2ACBD7510928A8D4767E8EA8DEC66FF6587237956309";
    var data2 = "96325";
    //char buf[128];

    //EZERO_ARRAY(buf);
    var md5ctx = null;
    var result = null;

    // 字符串 
    md5ctx = js_md5.md5_init();
    js_md5.md5_update(md5ctx, js_md5.md5_str_to_array(data0), data0.length);
    js_md5.md5_update(md5ctx, js_md5.md5_str_to_array(data1), data1.length);
    js_md5.md5_update(md5ctx, js_md5.md5_str_to_array(data2), data2.length);
    result = js_md5.md5_final(md5ctx);
    console.log("result:" + js_md5.md5_encode_hex(result, 16));

    // hex 
    md5ctx = js_md5.md5_init();
    js_md5.md5_update(md5ctx, js_md5.md5_hex_to_array("001122334455"), 6);
    js_md5.md5_update(md5ctx, js_md5.md5_hex_to_array("AABBCCDDEEFF"), 6);
    js_md5.md5_update(md5ctx, js_md5.md5_hex_to_array("88"), 1);
    result = js_md5.md5_final(md5ctx);
    console.log("result:" + js_md5.md5_encode_hex(result, 16));
  },

测试结果:

result:8aeec07868ee5cd179812324d28d49d6

result:89be57c106c2fa152be29148da87ea31

相关推荐
格里菲斯858几秒前
算法练习记录
算法
薛一半1 分钟前
PC端查看历史消息,鼠标向上滚动加载数据时页面停留在上次查看的位置
前端·javascript·vue.js
过期的H2O219 分钟前
【H2O2|全栈】JS进阶知识(四)Ajax
开发语言·javascript·ajax
MarcoPage19 分钟前
第十九课 Vue组件中的方法
前端·javascript·vue.js
你好龙卷风!!!1 小时前
vue3 怎么判断数据列是否包某一列名
前端·javascript·vue.js
幼儿园园霸柒柒1 小时前
第七章: 7.3求一个3*3的整型矩阵对角线元素之和
c语言·c++·算法·矩阵·c#·1024程序员节
忘梓.1 小时前
排序的秘密(1)——排序简介以及插入排序
数据结构·c++·算法·排序算法
福大大架构师每日一题1 小时前
文心一言 VS 讯飞星火 VS chatgpt (384)-- 算法导论24.5 4题
算法·文心一言
云卓科技1 小时前
无人车之路径规划篇
人工智能·嵌入式硬件·算法·自动驾驶
摆烂小白敲代码2 小时前
背包九讲——背包问题求方案数
c语言·c++·算法·背包问题·背包问题求方案数