JavaScript加密库jQuery.md5.js
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript加密库jQuery.md5.js相关的知识,希望对你有一定的参考价值。
javascript简单的MD5加密库jQuery.md5.js,简单用法如下:
//Create (hex-encoded) MD5 hash of a given string value: var md5 = $.md5(‘value‘); //Create (hex-encoded) HMAC-MD5 hash of a given string value and key: var md5 = $.md5(‘value‘, ‘key‘); //Create raw MD5 hash of a given string value: var md5 = $.md5(‘value‘, null, true); //Create raw HMAC-MD5 hash of a given string value and key: var md5 = $.md5(‘value‘, ‘key‘, true);
源码查看地址 https://github.com/placemarker/jQuery-MD5
1 /* 2 * jQuery MD5 Plugin 1.2.1 3 * https://github.com/blueimp/jQuery-MD5 4 * 5 * Copyright 2010, Sebastian Tschan 6 * https://blueimp.net 7 * 8 * Licensed under the MIT license: 9 * http://creativecommons.org/licenses/MIT/ 10 * 11 * Based on 12 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message 13 * Digest Algorithm, as defined in RFC 1321. 14 * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009 15 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet 16 * Distributed under the BSD License 17 * See http://pajhome.org.uk/crypt/md5 for more info. 18 */ 19 20 /*jslint bitwise: true */ 21 /*global unescape, jQuery */ 22 23 (function ($) { 24 ‘use strict‘; 25 26 /* 27 * Add integers, wrapping at 2^32. This uses 16-bit operations internally 28 * to work around bugs in some JS interpreters. 29 */ 30 function safe_add(x, y) { 31 var lsw = (x & 0xFFFF) + (y & 0xFFFF), 32 msw = (x >> 16) + (y >> 16) + (lsw >> 16); 33 return (msw << 16) | (lsw & 0xFFFF); 34 } 35 36 /* 37 * Bitwise rotate a 32-bit number to the left. 38 */ 39 function bit_rol(num, cnt) { 40 return (num << cnt) | (num >>> (32 - cnt)); 41 } 42 43 /* 44 * These functions implement the four basic operations the algorithm uses. 45 */ 46 function md5_cmn(q, a, b, x, s, t) { 47 return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b); 48 } 49 function md5_ff(a, b, c, d, x, s, t) { 50 return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t); 51 } 52 function md5_gg(a, b, c, d, x, s, t) { 53 return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t); 54 } 55 function md5_hh(a, b, c, d, x, s, t) { 56 return md5_cmn(b ^ c ^ d, a, b, x, s, t); 57 } 58 function md5_ii(a, b, c, d, x, s, t) { 59 return md5_cmn(c ^ (b | (~d)), a, b, x, s, t); 60 } 61 62 /* 63 * Calculate the MD5 of an array of little-endian words, and a bit length. 64 */ 65 function binl_md5(x, len) { 66 /* append padding */ 67 x[len >> 5] |= 0x80 << ((len) % 32); 68 x[(((len + 64) >>> 9) << 4) + 14] = len; 69 70 var i, olda, oldb, oldc, oldd, 71 a = 1732584193, 72 b = -271733879, 73 c = -1732584194, 74 d = 271733878; 75 76 for (i = 0; i < x.length; i += 16) { 77 olda = a; 78 oldb = b; 79 oldc = c; 80 oldd = d; 81 82 a = md5_ff(a, b, c, d, x[i], 7, -680876936); 83 d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586); 84 c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819); 85 b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330); 86 a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897); 87 d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426); 88 c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341); 89 b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983); 90 a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416); 91 d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417); 92 c = md5_ff(c, d, a, b, x[i + 10], 17, -42063); 93 b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162); 94 a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682); 95 d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101); 96 c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290); 97 b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329); 98 99 a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510); 100 d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632); 101 c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713); 102 b = md5_gg(b, c, d, a, x[i], 20, -373897302); 103 a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691); 104 d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083); 105 c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335); 106 b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848); 107 a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438); 108 d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690); 109 c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961); 110 b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501); 111 a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467); 112 d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784); 113 c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473); 114 b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734); 115 116 a = md5_hh(a, b, c, d, x[i + 5], 4, -378558); 117 d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463); 118 c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562); 119 b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556); 120 a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060); 121 d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353); 122 c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632); 123 b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640); 124 a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174); 125 d = md5_hh(d, a, b, c, x[i], 11, -358537222); 126 c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979); 127 b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189); 128 a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487); 129 d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835); 130 c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520); 131 b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651); 132 133 a = md5_ii(a, b, c, d, x[i], 6, -198630844); 134 d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415); 135 c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905); 136 b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055); 137 a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571); 138 d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606); 139 c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523); 140 b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799); 141 a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359); 142 d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744); 143 c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380); 144 b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649); 145 a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070); 146 d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379); 147 c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259); 148 b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551); 149 150 a = safe_add(a, olda); 151 b = safe_add(b, oldb); 152 c = safe_add(c, oldc); 153 d = safe_add(d, oldd); 154 } 155 return [a, b, c, d]; 156 } 157 158 /* 159 * Convert an array of little-endian words to a string 160 */ 161 function binl2rstr(input) { 162 var i, 163 output = ‘‘; 164 for (i = 0; i < input.length * 32; i += 8) { 165 output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xFF); 166 } 167 return output; 168 } 169 170 /* 171 * Convert a raw string to an array of little-endian words 172 * Characters >255 have their high-byte silently ignored. 173 */ 174 function rstr2binl(input) { 175 var i, 176 output = []; 177 output[(input.length >> 2) - 1] = undefined; 178 for (i = 0; i < output.length; i += 1) { 179 output[i] = 0; 180 } 181 for (i = 0; i < input.length * 8; i += 8) { 182 output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (i % 32); 183 } 184 return output; 185 } 186 187 /* 188 * Calculate the MD5 of a raw string 189 */ 190 function rstr_md5(s) { 191 return binl2rstr(binl_md5(rstr2binl(s), s.length * 8)); 192 } 193 194 /* 195 * Calculate the HMAC-MD5, of a key and some data (raw strings) 196 */ 197 function rstr_hmac_md5(key, data) { 198 var i, 199 bkey = rstr2binl(key), 200 ipad = [], 201 opad = [], 202 hash; 203 ipad[15] = opad[15] = undefined; 204 if (bkey.length > 16) { 205 bkey = binl_md5(bkey, key.length * 8); 206 } 207 for (i = 0; i < 16; i += 1) { 208 ipad[i] = bkey[i] ^ 0x36363636; 209 opad[i] = bkey[i] ^ 0x5C5C5C5C; 210 } 211 hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8); 212 return binl2rstr(binl_md5(opad.concat(hash), 512 + 128)); 213 } 214 215 /* 216 * Convert a raw string to a hex string 217 */ 218 function rstr2hex(input) { 219 var hex_tab = ‘0123456789abcdef‘, 220 output = ‘‘, 221 x, 222 i; 223 for (i = 0; i < input.length; i += 1) { 224 x = input.charCodeAt(i); 225 output += hex_tab.charAt((x >>> 4) & 0x0F) + 226 hex_tab.charAt(x & 0x0F); 227 } 228 return output; 229 } 230 231 /* 232 * Encode a string as utf-8 233 */ 234 function str2rstr_utf8(input) { 235 return unescape(encodeURIComponent(input)); 236 } 237 238 /* 239 * Take string arguments and return either raw or hex encoded strings 240 */ 241 function raw_md5(s) { 242 return rstr_md5(str2rstr_utf8(s)); 243 } 244 function hex_md5(s) { 245 return rstr2hex(raw_md5(s)); 246 } 247 function raw_hmac_md5(k, d) { 248 return rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)); 249 } 250 function hex_hmac_md5(k, d) { 251 return rstr2hex(raw_hmac_md5(k, d)); 252 } 253 254 $.md5 = function (string, key, raw) { 255 if (!key) { 256 if (!raw) { 257 return hex_md5(string); 258 } else { 259 return raw_md5(string); 260 } 261 } 262 if (!raw) { 263 return hex_hmac_md5(key, string); 264 } else { 265 return raw_hmac_md5(key, string); 266 } 267 }; 268 269 }(typeof jQuery === ‘function‘ ? jQuery : this));
这里也附上源码,以方便
以上是关于JavaScript加密库jQuery.md5.js的主要内容,如果未能解决你的问题,请参考以下文章