码迷,mamicode.com
首页 > 编程语言 > 详细

javascript MD5加密

时间:2015-09-09 09:39:54      阅读:505      评论:0      收藏:0      [点我收藏+]

标签:

  1 /*
  2  * Javascript MD5 library - version 0.4
  3  * 
  4  * Coded (2011) by Luigi Galli - LG@4e71.org - http://faultylabs.com
  5  * 
  6  * Thanks to: Roberto Viola
  7  * 
  8  * The below code is PUBLIC DOMAIN - NO WARRANTY!
  9  * 
 10  * Changelog: Version 0.4 - 2011-06-19 + added compact version
 11  * (md5_compact_min.js), this is a slower but smaller version (more than 4KB
 12  * lighter before stripping/minification) + added preliminary support for Typed
 13  * Arrays (see: https://developer.mozilla.org/en/JavaScript_typed_arrays and
 14  * http://www.khronos.org/registry/typedarray/specs/latest/) MD5() now accepts
 15  * input data as ArrayBuffer, Float32Array, Float64Array, Int16Array,
 16  * Int32Array, Int8Array, Uint16Array, Uint32Array or Uint8Array - moved unit
 17  * tests to md5_test.js - minor refactoring
 18  * 
 19  * Version 0.3.* - 2011-06-## - Internal dev versions
 20  * 
 21  * Version 0.2 - 2011-05-22 * FIXED: serious integer overflow problems which
 22  * could cause a wrong MD5 hash being returned
 23  * 
 24  * Version 0.1 - 2011 -Initial version
 25  */
 26 if (typeof faultylabs == ‘undefined‘) {
 27     faultylabs = {}
 28 }
 29 
 30 /*
 31  * MD5() 输出为十六进制大写字符串
 32  */
 33 
 34 faultylabs.MD5 = function(data) {
 35     // convert number to (unsigned) 32 bit hex, zero filled string
 36     function to_zerofilled_hex(n) {
 37         var t1 = (n >>> 0).toString(16)
 38         return "00000000".substr(0, 8 - t1.length) + t1
 39     }
 40 
 41     // convert array of chars to array of bytes
 42     function chars_to_bytes(ac) {
 43         var retval = []
 44         for (var i = 0; i < ac.length; i++) {
 45             retval = retval.concat(str_to_bytes(ac[i]))
 46         }
 47         return retval
 48     }
 49 
 50     // convert a 64 bit unsigned number to array of bytes. Little endian
 51     function int64_to_bytes(num) {
 52         var retval = []
 53         for (var i = 0; i < 8; i++) {
 54             retval.push(num & 0xFF)
 55             num = num >>> 8
 56         }
 57         return retval
 58     }
 59 
 60     // 32 bit left-rotation
 61     function rol(num, places) {
 62         return ((num << places) & 0xFFFFFFFF) | (num >>> (32 - places))
 63     }
 64 
 65     // The 4 MD5 functions
 66     function fF(b, c, d) {
 67         return (b & c) | (~b & d)
 68     }
 69 
 70     function fG(b, c, d) {
 71         return (d & b) | (~d & c)
 72     }
 73 
 74     function fH(b, c, d) {
 75         return b ^ c ^ d
 76     }
 77 
 78     function fI(b, c, d) {
 79         return c ^ (b | ~d)
 80     }
 81 
 82     // pick 4 bytes at specified offset. Little-endian is assumed
 83     function bytes_to_int32(arr, off) {
 84         return (arr[off + 3] << 24) | (arr[off + 2] << 16)
 85                 | (arr[off + 1] << 8) | (arr[off])
 86     }
 87 
 88     /*
 89      * Conver string to array of bytes in UTF-8 encoding See:
 90      * http://www.dangrossman.info/2007/05/25/handling-utf-8-in-javascript-php-and-non-utf8-databases/
 91      * http://stackoverflow.com/questions/1240408/reading-bytes-from-a-javascript-string
 92      * How about a String.getBytes(<ENCODING>) for Javascript!? Isn‘t it time
 93      * to add it?
 94      */
 95     function str_to_bytes(str) {
 96         var retval = []
 97         for (var i = 0; i < str.length; i++)
 98             if (str.charCodeAt(i) <= 0x7F) {
 99                 retval.push(str.charCodeAt(i))
100             } else {
101                 var tmp = encodeURIComponent(str.charAt(i)).substr(1)
102                         .split(‘%‘)
103                 for (var j = 0; j < tmp.length; j++) {
104                     retval.push(parseInt(tmp[j], 0x10))
105                 }
106             }
107         return retval
108     }
109 
110     // convert the 4 32-bit buffers to a 128 bit hex string. (Little-endian is
111     // assumed)
112     function int128le_to_hex(a, b, c, d) {
113         var ra = ""
114         var t = 0
115         var ta = 0
116         for (var i = 3; i >= 0; i--) {
117             ta = arguments[i]
118             t = (ta & 0xFF)
119             ta = ta >>> 8
120             t = t << 8
121             t = t | (ta & 0xFF)
122             ta = ta >>> 8
123             t = t << 8
124             t = t | (ta & 0xFF)
125             ta = ta >>> 8
126             t = t << 8
127             t = t | ta
128             ra = ra + to_zerofilled_hex(t)
129         }
130         return ra
131     }
132 
133     // conversion from typed byte array to plain javascript array
134     function typed_to_plain(tarr) {
135         var retval = new Array(tarr.length)
136         for (var i = 0; i < tarr.length; i++) {
137             retval[i] = tarr[i]
138         }
139         return retval
140     }
141 
142     // check input data type and perform conversions if needed
143     var databytes = null
144     // String
145     var type_mismatch = null
146     if (typeof data == ‘string‘) {
147         // convert string to array bytes
148         databytes = str_to_bytes(data)
149     } else if (data.constructor == Array) {
150         if (data.length === 0) {
151             // if it‘s empty, just assume array of bytes
152             databytes = data
153         } else if (typeof data[0] == ‘string‘) {
154             databytes = chars_to_bytes(data)
155         } else if (typeof data[0] == ‘number‘) {
156             databytes = data
157         } else {
158             type_mismatch = typeof data[0]
159         }
160     } else if (typeof ArrayBuffer != ‘undefined‘) {
161         if (data instanceof ArrayBuffer) {
162             databytes = typed_to_plain(new Uint8Array(data))
163         } else if ((data instanceof Uint8Array) || (data instanceof Int8Array)) {
164             databytes = typed_to_plain(data)
165         } else if ((data instanceof Uint32Array)
166                 || (data instanceof Int32Array)
167                 || (data instanceof Uint16Array)
168                 || (data instanceof Int16Array)
169                 || (data instanceof Float32Array)
170                 || (data instanceof Float64Array)) {
171             databytes = typed_to_plain(new Uint8Array(data.buffer))
172         } else {
173             type_mismatch = typeof data
174         }
175     } else {
176         type_mismatch = typeof data
177     }
178 
179     if (type_mismatch) {
180         alert(‘MD5 type mismatch, cannot process ‘ + type_mismatch)
181     }
182 
183     function _add(n1, n2) {
184         return 0x0FFFFFFFF & (n1 + n2)
185     }
186 
187     return do_digest()
188 
189     function do_digest() {
190 
191         // function update partial state for each run
192         function updateRun(nf, sin32, dw32, b32) {
193             var temp = d
194             d = c
195             c = b
196             // b = b + rol(a + (nf + (sin32 + dw32)), b32)
197             b = _add(b, rol(_add(a, _add(nf, _add(sin32, dw32))), b32))
198             a = temp
199         }
200 
201         // save original length
202         var org_len = databytes.length
203 
204         // first append the "1" + 7x "0"
205         databytes.push(0x80)
206 
207         // determine required amount of padding
208         var tail = databytes.length % 64
209         // no room for msg length?
210         if (tail > 56) {
211             // pad to next 512 bit block
212             for (var i = 0; i < (64 - tail); i++) {
213                 databytes.push(0x0)
214             }
215             tail = databytes.length % 64
216         }
217         for (i = 0; i < (56 - tail); i++) {
218             databytes.push(0x0)
219         }
220         // message length in bits mod 512 should now be 448
221         // append 64 bit, little-endian original msg length (in *bits*!)
222         databytes = databytes.concat(int64_to_bytes(org_len * 8))
223 
224         // initialize 4x32 bit state
225         var h0 = 0x67452301
226         var h1 = 0xEFCDAB89
227         var h2 = 0x98BADCFE
228         var h3 = 0x10325476
229 
230         // temp buffers
231         var a = 0, b = 0, c = 0, d = 0
232 
233         // Digest message
234         for (i = 0; i < databytes.length / 64; i++) {
235             // initialize run
236             a = h0
237             b = h1
238             c = h2
239             d = h3
240 
241             var ptr = i * 64
242 
243             // do 64 runs
244             updateRun(fF(b, c, d), 0xd76aa478, bytes_to_int32(databytes, ptr),
245                     7)
246             updateRun(fF(b, c, d), 0xe8c7b756, bytes_to_int32(databytes, ptr
247                                     + 4), 12)
248             updateRun(fF(b, c, d), 0x242070db, bytes_to_int32(databytes, ptr
249                                     + 8), 17)
250             updateRun(fF(b, c, d), 0xc1bdceee, bytes_to_int32(databytes, ptr
251                                     + 12), 22)
252             updateRun(fF(b, c, d), 0xf57c0faf, bytes_to_int32(databytes, ptr
253                                     + 16), 7)
254             updateRun(fF(b, c, d), 0x4787c62a, bytes_to_int32(databytes, ptr
255                                     + 20), 12)
256             updateRun(fF(b, c, d), 0xa8304613, bytes_to_int32(databytes, ptr
257                                     + 24), 17)
258             updateRun(fF(b, c, d), 0xfd469501, bytes_to_int32(databytes, ptr
259                                     + 28), 22)
260             updateRun(fF(b, c, d), 0x698098d8, bytes_to_int32(databytes, ptr
261                                     + 32), 7)
262             updateRun(fF(b, c, d), 0x8b44f7af, bytes_to_int32(databytes, ptr
263                                     + 36), 12)
264             updateRun(fF(b, c, d), 0xffff5bb1, bytes_to_int32(databytes, ptr
265                                     + 40), 17)
266             updateRun(fF(b, c, d), 0x895cd7be, bytes_to_int32(databytes, ptr
267                                     + 44), 22)
268             updateRun(fF(b, c, d), 0x6b901122, bytes_to_int32(databytes, ptr
269                                     + 48), 7)
270             updateRun(fF(b, c, d), 0xfd987193, bytes_to_int32(databytes, ptr
271                                     + 52), 12)
272             updateRun(fF(b, c, d), 0xa679438e, bytes_to_int32(databytes, ptr
273                                     + 56), 17)
274             updateRun(fF(b, c, d), 0x49b40821, bytes_to_int32(databytes, ptr
275                                     + 60), 22)
276             updateRun(fG(b, c, d), 0xf61e2562, bytes_to_int32(databytes, ptr
277                                     + 4), 5)
278             updateRun(fG(b, c, d), 0xc040b340, bytes_to_int32(databytes, ptr
279                                     + 24), 9)
280             updateRun(fG(b, c, d), 0x265e5a51, bytes_to_int32(databytes, ptr
281                                     + 44), 14)
282             updateRun(fG(b, c, d), 0xe9b6c7aa, bytes_to_int32(databytes, ptr),
283                     20)
284             updateRun(fG(b, c, d), 0xd62f105d, bytes_to_int32(databytes, ptr
285                                     + 20), 5)
286             updateRun(fG(b, c, d), 0x2441453, bytes_to_int32(databytes, ptr
287                                     + 40), 9)
288             updateRun(fG(b, c, d), 0xd8a1e681, bytes_to_int32(databytes, ptr
289                                     + 60), 14)
290             updateRun(fG(b, c, d), 0xe7d3fbc8, bytes_to_int32(databytes, ptr
291                                     + 16), 20)
292             updateRun(fG(b, c, d), 0x21e1cde6, bytes_to_int32(databytes, ptr
293                                     + 36), 5)
294             updateRun(fG(b, c, d), 0xc33707d6, bytes_to_int32(databytes, ptr
295                                     + 56), 9)
296             updateRun(fG(b, c, d), 0xf4d50d87, bytes_to_int32(databytes, ptr
297                                     + 12), 14)
298             updateRun(fG(b, c, d), 0x455a14ed, bytes_to_int32(databytes, ptr
299                                     + 32), 20)
300             updateRun(fG(b, c, d), 0xa9e3e905, bytes_to_int32(databytes, ptr
301                                     + 52), 5)
302             updateRun(fG(b, c, d), 0xfcefa3f8, bytes_to_int32(databytes, ptr
303                                     + 8), 9)
304             updateRun(fG(b, c, d), 0x676f02d9, bytes_to_int32(databytes, ptr
305                                     + 28), 14)
306             updateRun(fG(b, c, d), 0x8d2a4c8a, bytes_to_int32(databytes, ptr
307                                     + 48), 20)
308             updateRun(fH(b, c, d), 0xfffa3942, bytes_to_int32(databytes, ptr
309                                     + 20), 4)
310             updateRun(fH(b, c, d), 0x8771f681, bytes_to_int32(databytes, ptr
311                                     + 32), 11)
312             updateRun(fH(b, c, d), 0x6d9d6122, bytes_to_int32(databytes, ptr
313                                     + 44), 16)
314             updateRun(fH(b, c, d), 0xfde5380c, bytes_to_int32(databytes, ptr
315                                     + 56), 23)
316             updateRun(fH(b, c, d), 0xa4beea44, bytes_to_int32(databytes, ptr
317                                     + 4), 4)
318             updateRun(fH(b, c, d), 0x4bdecfa9, bytes_to_int32(databytes, ptr
319                                     + 16), 11)
320             updateRun(fH(b, c, d), 0xf6bb4b60, bytes_to_int32(databytes, ptr
321                                     + 28), 16)
322             updateRun(fH(b, c, d), 0xbebfbc70, bytes_to_int32(databytes, ptr
323                                     + 40), 23)
324             updateRun(fH(b, c, d), 0x289b7ec6, bytes_to_int32(databytes, ptr
325                                     + 52), 4)
326             updateRun(fH(b, c, d), 0xeaa127fa, bytes_to_int32(databytes, ptr),
327                     11)
328             updateRun(fH(b, c, d), 0xd4ef3085, bytes_to_int32(databytes, ptr
329                                     + 12), 16)
330             updateRun(fH(b, c, d), 0x4881d05, bytes_to_int32(databytes, ptr
331                                     + 24), 23)
332             updateRun(fH(b, c, d), 0xd9d4d039, bytes_to_int32(databytes, ptr
333                                     + 36), 4)
334             updateRun(fH(b, c, d), 0xe6db99e5, bytes_to_int32(databytes, ptr
335                                     + 48), 11)
336             updateRun(fH(b, c, d), 0x1fa27cf8, bytes_to_int32(databytes, ptr
337                                     + 60), 16)
338             updateRun(fH(b, c, d), 0xc4ac5665, bytes_to_int32(databytes, ptr
339                                     + 8), 23)
340             updateRun(fI(b, c, d), 0xf4292244, bytes_to_int32(databytes, ptr),
341                     6)
342             updateRun(fI(b, c, d), 0x432aff97, bytes_to_int32(databytes, ptr
343                                     + 28), 10)
344             updateRun(fI(b, c, d), 0xab9423a7, bytes_to_int32(databytes, ptr
345                                     + 56), 15)
346             updateRun(fI(b, c, d), 0xfc93a039, bytes_to_int32(databytes, ptr
347                                     + 20), 21)
348             updateRun(fI(b, c, d), 0x655b59c3, bytes_to_int32(databytes, ptr
349                                     + 48), 6)
350             updateRun(fI(b, c, d), 0x8f0ccc92, bytes_to_int32(databytes, ptr
351                                     + 12), 10)
352             updateRun(fI(b, c, d), 0xffeff47d, bytes_to_int32(databytes, ptr
353                                     + 40), 15)
354             updateRun(fI(b, c, d), 0x85845dd1, bytes_to_int32(databytes, ptr
355                                     + 4), 21)
356             updateRun(fI(b, c, d), 0x6fa87e4f, bytes_to_int32(databytes, ptr
357                                     + 32), 6)
358             updateRun(fI(b, c, d), 0xfe2ce6e0, bytes_to_int32(databytes, ptr
359                                     + 60), 10)
360             updateRun(fI(b, c, d), 0xa3014314, bytes_to_int32(databytes, ptr
361                                     + 24), 15)
362             updateRun(fI(b, c, d), 0x4e0811a1, bytes_to_int32(databytes, ptr
363                                     + 52), 21)
364             updateRun(fI(b, c, d), 0xf7537e82, bytes_to_int32(databytes, ptr
365                                     + 16), 6)
366             updateRun(fI(b, c, d), 0xbd3af235, bytes_to_int32(databytes, ptr
367                                     + 44), 10)
368             updateRun(fI(b, c, d), 0x2ad7d2bb, bytes_to_int32(databytes, ptr
369                                     + 8), 15)
370             updateRun(fI(b, c, d), 0xeb86d391, bytes_to_int32(databytes, ptr
371                                     + 36), 21)
372 
373             // update buffers
374             h0 = _add(h0, a)
375             h1 = _add(h1, b)
376             h2 = _add(h2, c)
377             h3 = _add(h3, d)
378         }
379         // Done! Convert buffers to 128 bit (LE)
380         return int128le_to_hex(h3, h2, h1, h0).toUpperCase()
381     }
382 
383 }

当时用这个插件是因为在用jquery选择器的时候,id包含了特殊字符和jquery冲突,所以就对ID进行了加密处理...    显然是个笨办法,但也解决了实际问题。 推荐一下...

javascript MD5加密

标签:

原文地址:http://www.cnblogs.com/zhourunbest/p/4793686.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!