w35l3y

md5

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