目录
  • 1.什么是MD5加密
  • 2.为什么要使用MD5加密
  • 3.实现代码

1.什么是MD5加密

2.为什么要使用MD5加密

在存储一些敏感的信息的时候,如果不进行加密则容易出现安全问题。例如:用户存储的密码,如果数据中的密码采用明文,一旦数据库泄露,则用户密码直接泄露。但是使用MD5加密算法之后即使数据泄露,这些密码也无法激活成功教程,保证了数据的安全性。

3.实现代码

/* md5加密转换脚本 */ var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */ var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */ var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */ function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));} function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));} function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));} function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); } function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); } function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); } function md5_vm_test() { return hex_md5("abc") == "cd24fb0d6963f7d28e17f72"; } function core_md5(x, len) { /* append padding */ x[len >> 5] |= 0x80 << ((len) % 32); x[(((len + 64) >>> 9) << 4) + 14] = len; var a = ; var b = -; var c = -; var d = ; for(var i = 0; i < x.length; i += 16) { var olda = a; var oldb = b; var oldc = c; var oldd = d; a = md5_ff(a, b, c, d, x[i+ 0], 7 , -); d = md5_ff(d, a, b, c, x[i+ 1], 12, -); c = md5_ff(c, d, a, b, x[i+ 2], 17, ); b = md5_ff(b, c, d, a, x[i+ 3], 22, -); a = md5_ff(a, b, c, d, x[i+ 4], 7 , -); d = md5_ff(d, a, b, c, x[i+ 5], 12, ); c = md5_ff(c, d, a, b, x[i+ 6], 17, -); b = md5_ff(b, c, d, a, x[i+ 7], 22, -); a = md5_ff(a, b, c, d, x[i+ 8], 7 , ); d = md5_ff(d, a, b, c, x[i+ 9], 12, -); c = md5_ff(c, d, a, b, x[i+10], 17, -42063); b = md5_ff(b, c, d, a, x[i+11], 22, -); a = md5_ff(a, b, c, d, x[i+12], 7 , ); d = md5_ff(d, a, b, c, x[i+13], 12, -); c = md5_ff(c, d, a, b, x[i+14], 17, -); b = md5_ff(b, c, d, a, x[i+15], 22, ); a = md5_gg(a, b, c, d, x[i+ 1], 5 , -); d = md5_gg(d, a, b, c, x[i+ 6], 9 , -); c = md5_gg(c, d, a, b, x[i+11], 14, ); b = md5_gg(b, c, d, a, x[i+ 0], 20, -); a = md5_gg(a, b, c, d, x[i+ 5], 5 , -); d = md5_gg(d, a, b, c, x[i+10], 9 , ); c = md5_gg(c, d, a, b, x[i+15], 14, -); b = md5_gg(b, c, d, a, x[i+ 4], 20, -); a = md5_gg(a, b, c, d, x[i+ 9], 5 , ); d = md5_gg(d, a, b, c, x[i+14], 9 , -); c = md5_gg(c, d, a, b, x[i+ 3], 14, -); b = md5_gg(b, c, d, a, x[i+ 8], 20, ); a = md5_gg(a, b, c, d, x[i+13], 5 , -); d = md5_gg(d, a, b, c, x[i+ 2], 9 , -); c = md5_gg(c, d, a, b, x[i+ 7], 14, ); b = md5_gg(b, c, d, a, x[i+12], 20, -); a = md5_hh(a, b, c, d, x[i+ 5], 4 , -); d = md5_hh(d, a, b, c, x[i+ 8], 11, -); c = md5_hh(c, d, a, b, x[i+11], 16, ); b = md5_hh(b, c, d, a, x[i+14], 23, -); a = md5_hh(a, b, c, d, x[i+ 1], 4 , -); d = md5_hh(d, a, b, c, x[i+ 4], 11, ); c = md5_hh(c, d, a, b, x[i+ 7], 16, -); b = md5_hh(b, c, d, a, x[i+10], 23, -); a = md5_hh(a, b, c, d, x[i+13], 4 , ); d = md5_hh(d, a, b, c, x[i+ 0], 11, -); c = md5_hh(c, d, a, b, x[i+ 3], 16, -); b = md5_hh(b, c, d, a, x[i+ 6], 23, ); a = md5_hh(a, b, c, d, x[i+ 9], 4 , -); d = md5_hh(d, a, b, c, x[i+12], 11, -); c = md5_hh(c, d, a, b, x[i+15], 16, ); b = md5_hh(b, c, d, a, x[i+ 2], 23, -); a = md5_ii(a, b, c, d, x[i+ 0], 6 , -); d = md5_ii(d, a, b, c, x[i+ 7], 10, ); c = md5_ii(c, d, a, b, x[i+14], 15, -); b = md5_ii(b, c, d, a, x[i+ 5], 21, -); a = md5_ii(a, b, c, d, x[i+12], 6 , ); d = md5_ii(d, a, b, c, x[i+ 3], 10, -); c = md5_ii(c, d, a, b, x[i+10], 15, -); b = md5_ii(b, c, d, a, x[i+ 1], 21, -); a = md5_ii(a, b, c, d, x[i+ 8], 6 , ); d = md5_ii(d, a, b, c, x[i+15], 10, -); c = md5_ii(c, d, a, b, x[i+ 6], 15, -); b = md5_ii(b, c, d, a, x[i+13], 21, ); a = md5_ii(a, b, c, d, x[i+ 4], 6 , -); d = md5_ii(d, a, b, c, x[i+11], 10, -); c = md5_ii(c, d, a, b, x[i+ 2], 15, ); b = md5_ii(b, c, d, a, x[i+ 9], 21, -); a = safe_add(a, olda); b = safe_add(b, oldb); c = safe_add(c, oldc); d = safe_add(d, oldd); } return Array(a, b, c, d); } /* * These functions implement the four basic operations the algorithm uses. */ function md5_cmn(q, a, b, x, s, t) { return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b); } function md5_ff(a, b, c, d, x, s, t) { return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t); } function md5_gg(a, b, c, d, x, s, t) { return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t); } function md5_hh(a, b, c, d, x, s, t) { return md5_cmn(b ^ c ^ d, a, b, x, s, t); } function md5_ii(a, b, c, d, x, s, t) { return md5_cmn(c ^ (b | (~d)), a, b, x, s, t); } /* * Calculate the HMAC-MD5, of a key and some data */ function core_hmac_md5(key, data) { var bkey = str2binl(key); if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz); var ipad = Array(16), opad = Array(16); for(var i = 0; i < 16; i++) { ipad[i] = bkey[i] ^ 0x; opad[i] = bkey[i] ^ 0x5C5C5C5C; } var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz); return core_md5(opad.concat(hash), 512 + 128); } /* * Add integers, wrapping at 2^32. This uses 16-bit operations internally * to work around bugs in some JS interpreters. */ function safe_add(x, y) { var lsw = (x & 0xFFFF) + (y & 0xFFFF); var msw = (x >> 16) + (y >> 16) + (lsw >> 16); return (msw << 16) | (lsw & 0xFFFF); } /* * Bitwise rotate a 32-bit number to the left. */ function bit_rol(num, cnt) { return (num << cnt) | (num >>> (32 - cnt)); } /* * Convert a string to an array of little-endian words * If chrsz is ASCII, characters >255 have their hi-byte silently ignored. */ function str2binl(str) { var bin = Array(); var mask = (1 << chrsz) - 1; for(var i = 0; i < str.length * chrsz; i += chrsz) bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32); return bin; } /* * Convert an array of little-endian words to a string */ function binl2str(bin) { var str = ""; var mask = (1 << chrsz) - 1; for(var i = 0; i < bin.length * 32; i += chrsz) str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask); return str; } /* * Convert an array of little-endian words to a hex string. */ function binl2hex(binarray) { var hex_tab = hexcase ? "0ABCDEF" : "0abcdef"; var str = ""; for(var i = 0; i < binarray.length * 4; i++) { str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) + hex_tab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF); } return str; } /* * Convert an array of little-endian words to a base-64 string */ function binl2b64(binarray) { var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0+/"; var str = ""; for(var i = 0; i < binarray.length * 4; i += 3) { var triplet = (((binarray[i >> 2] >> 8 * ( i %4)) & 0xFF) << 16) | (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 ) | ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF); for(var j = 0; j < 4; j++) { if(i * 8 + j * 6 > binarray.length * 32) str += b64pad; else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); } } return str; }

到此这篇关于JavaScript实现简单MD5加密的脚本分享的文章就介绍到这了,更多相关JavaScript MD5加密内容请搜索本网站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本网站!

您可能感兴趣的文章:

  • JS实现的base64加密、md5加密及sha1加密详解
  • javascript md5加密代码
  • 详解易语言调用js实现md5加密方法
  • JS文件上传时如何使用MD5加密
  • JavaScript实现MD5加密的六种方式实例
  • 前端技巧之js-md5的简单使用方法