Advertisement
rg443

javascript md5

Jan 19th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* md5.js - MD5 Message-Digest
  2.  * http://www.onicos.com/staff/iz/amuse/javascript/expert/md5.txt
  3.  * Version: 2.0.0
  4.  * LastModified: May 13 2002
  5.  *
  6.  * This program is free software.  You can redistribute it and/or modify
  7.  * it without any warranty.  This library calculates the MD5 based on RFC1321.
  8.  * See RFC1321 for more information and algorism.
  9.  */
  10.  
  11. /* Interface:
  12.  * md5_128bits = MD5_hash(data);
  13.  * md5_hexstr = MD5_hexhash(data);
  14.  */
  15.  
  16. /* ChangeLog
  17.  * 2002/05/13: Version 2.0.0 released
  18.  * NOTICE: API is changed.
  19.  * 2002/04/15: Bug fix about MD5 length.
  20.  */
  21.  
  22.  
  23. //    md5_T[i] = parseInt(Math.abs(Math.sin(i)) * 4294967296.0);
  24. var MD5_T = new Array(0x00000000, 0xd76aa478, 0xe8c7b756, 0x242070db,
  25.               0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613,
  26.               0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1,
  27.               0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e,
  28.               0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51,
  29.               0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681,
  30.               0xe7d3fbc8, 0x21e1cde6, 0xc33707d6, 0xf4d50d87,
  31.               0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9,
  32.               0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122,
  33.               0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60,
  34.               0xbebfbc70, 0x289b7ec6, 0xeaa127fa, 0xd4ef3085,
  35.               0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8,
  36.               0xc4ac5665, 0xf4292244, 0x432aff97, 0xab9423a7,
  37.               0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d,
  38.               0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314,
  39.               0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb,
  40.               0xeb86d391);
  41.  
  42. var MD5_round1 = new Array(new Array( 0, 7, 1), new Array( 1,12, 2),
  43.                new Array( 2,17, 3), new Array( 3,22, 4),
  44.                new Array( 4, 7, 5), new Array( 5,12, 6),
  45.                new Array( 6,17, 7), new Array( 7,22, 8),
  46.                new Array( 8, 7, 9), new Array( 9,12,10),
  47.                new Array(10,17,11), new Array(11,22,12),
  48.                new Array(12, 7,13), new Array(13,12,14),
  49.                new Array(14,17,15), new Array(15,22,16));
  50.  
  51. var MD5_round2 = new Array(new Array( 1, 5,17), new Array( 6, 9,18),
  52.                new Array(11,14,19), new Array( 0,20,20),
  53.                new Array( 5, 5,21), new Array(10, 9,22),
  54.                new Array(15,14,23), new Array( 4,20,24),
  55.                new Array( 9, 5,25), new Array(14, 9,26),
  56.                new Array( 3,14,27), new Array( 8,20,28),
  57.                new Array(13, 5,29), new Array( 2, 9,30),
  58.                new Array( 7,14,31), new Array(12,20,32));
  59.  
  60. var MD5_round3 = new Array(new Array( 5, 4,33), new Array( 8,11,34),
  61.                new Array(11,16,35), new Array(14,23,36),
  62.                new Array( 1, 4,37), new Array( 4,11,38),
  63.                new Array( 7,16,39), new Array(10,23,40),
  64.                new Array(13, 4,41), new Array( 0,11,42),
  65.                new Array( 3,16,43), new Array( 6,23,44),
  66.                new Array( 9, 4,45), new Array(12,11,46),
  67.                new Array(15,16,47), new Array( 2,23,48));
  68.  
  69. var MD5_round4 = new Array(new Array( 0, 6,49), new Array( 7,10,50),
  70.                new Array(14,15,51), new Array( 5,21,52),
  71.                new Array(12, 6,53), new Array( 3,10,54),
  72.                new Array(10,15,55), new Array( 1,21,56),
  73.                new Array( 8, 6,57), new Array(15,10,58),
  74.                new Array( 6,15,59), new Array(13,21,60),
  75.                new Array( 4, 6,61), new Array(11,10,62),
  76.                new Array( 2,15,63), new Array( 9,21,64));
  77.  
  78. function MD5_F(x, y, z) { return (x & y) | (~x & z); }
  79. function MD5_G(x, y, z) { return (x & z) | (y & ~z); }
  80. function MD5_H(x, y, z) { return x ^ y ^ z;          }
  81. function MD5_I(x, y, z) { return y ^ (x | ~z);       }
  82.  
  83. var MD5_round = new Array(new Array(MD5_F, MD5_round1),
  84.               new Array(MD5_G, MD5_round2),
  85.               new Array(MD5_H, MD5_round3),
  86.               new Array(MD5_I, MD5_round4));
  87.  
  88. function MD5_pack(n32) {
  89.   return String.fromCharCode(n32 & 0xff) +
  90.      String.fromCharCode((n32 >>> 8) & 0xff) +
  91.      String.fromCharCode((n32 >>> 16) & 0xff) +
  92.      String.fromCharCode((n32 >>> 24) & 0xff);
  93. }
  94.  
  95. function MD5_unpack(s4) {
  96.   return  s4.charCodeAt(0)        |
  97.      (s4.charCodeAt(1) <<  8) |
  98.      (s4.charCodeAt(2) << 16) |
  99.      (s4.charCodeAt(3) << 24);
  100. }
  101.  
  102. function MD5_number(n) {
  103.   while (n < 0)
  104.     n += 4294967296;
  105.   while (n > 4294967295)
  106.     n -= 4294967296;
  107.   return n;
  108. }
  109.  
  110. function MD5_apply_round(x, s, f, abcd, r) {
  111.   var a, b, c, d;
  112.   var kk, ss, ii;
  113.   var t, u;
  114.  
  115.   a = abcd[0];
  116.   b = abcd[1];
  117.   c = abcd[2];
  118.   d = abcd[3];
  119.   kk = r[0];
  120.   ss = r[1];
  121.   ii = r[2];
  122.  
  123.   u = f(s[b], s[c], s[d]);
  124.   t = s[a] + u + x[kk] + MD5_T[ii];
  125.   t = MD5_number(t);
  126.   t = ((t<<ss) | (t>>>(32-ss)));
  127.   t += s[b];
  128.   s[a] = MD5_number(t);
  129. }
  130.  
  131. function MD5_hash(data) {
  132.   var abcd, x, state, s;
  133.   var len, index, padLen, f, r;
  134.   var i, j, k;
  135.   var tmp;
  136.  
  137.   state = new Array(0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476);
  138.   len = data.length;
  139.   index = len & 0x3f;
  140.   padLen = (index < 56) ? (56 - index) : (120 - index);
  141.   if(padLen > 0) {
  142.     data += "\x80";
  143.     for(i = 0; i < padLen - 1; i++)
  144.       data += "\x00";
  145.   }
  146.   data += MD5_pack(len * 8);
  147.   data += MD5_pack(0);
  148.   len  += padLen + 8;
  149.   abcd = new Array(0, 1, 2, 3);
  150.   x    = new Array(16);
  151.   s    = new Array(4);
  152.  
  153.   for(k = 0; k < len; k += 64) {
  154.     for(i = 0, j = k; i < 16; i++, j += 4) {
  155.       x[i] = data.charCodeAt(j) |
  156.         (data.charCodeAt(j + 1) <<  8) |
  157.         (data.charCodeAt(j + 2) << 16) |
  158.         (data.charCodeAt(j + 3) << 24);
  159.     }
  160.     for(i = 0; i < 4; i++)
  161.       s[i] = state[i];
  162.     for(i = 0; i < 4; i++) {
  163.       f = MD5_round[i][0];
  164.       r = MD5_round[i][1];
  165.       for(j = 0; j < 16; j++) {
  166.     MD5_apply_round(x, s, f, abcd, r[j]);
  167.     tmp = abcd[0];
  168.     abcd[0] = abcd[3];
  169.     abcd[3] = abcd[2];
  170.     abcd[2] = abcd[1];
  171.     abcd[1] = tmp;
  172.       }
  173.     }
  174.  
  175.     for(i = 0; i < 4; i++) {
  176.       state[i] += s[i];
  177.       state[i] = MD5_number(state[i]);
  178.     }
  179.   }
  180.  
  181.   return MD5_pack(state[0]) +
  182.      MD5_pack(state[1]) +
  183.      MD5_pack(state[2]) +
  184.      MD5_pack(state[3]);
  185. }
  186.  
  187. function MD5_hexhash(data) {
  188.     var i, out, c;
  189.     var bit128;
  190.  
  191.     bit128 = MD5_hash(data);
  192.     out = "";
  193.     for(i = 0; i < 16; i++) {
  194.     c = bit128.charCodeAt(i);
  195.     out += "0123456789abcdef".charAt((c>>4) & 0xf);
  196.     out += "0123456789abcdef".charAt(c & 0xf);
  197.     }
  198.     return out;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement