sohotcall

md5.fn.js

Aug 4th, 2021
806
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //https://stackoverflow.com/questions/1655769/fastest-md5-implementation-in-javascript
  2. function md5( str ){
  3.     var hc = "0123456789abcdef"
  4.     let rh = n => {
  5.         let j,
  6.             s = ""
  7.         for ( j = 0; j <= 3; j++ )
  8.             s += hc.charAt( ( n >> ( j * 8 + 4 ) ) & 15 ) + hc.charAt( ( n >> ( j * 8 ) ) & 15 )
  9.         return s;
  10.     }
  11.     let ad = ( x, y ) => {
  12.         let l = ( x & 65535 ) + ( y & 65535 )
  13.         let m = ( x >> 16 ) + ( y >> 16 ) + ( l >> 16 )
  14.         return ( m << 16 ) | ( l & 65535 )
  15.     }
  16.     let rl = ( n, c ) => ( n << c ) | ( n >>> ( 32 - c ) )
  17.     let cm = ( q, a, b, x, s, t ) => ad( rl ( ad( ad( a, q ), ad( x, t ) ), s ), b )
  18.     let ff = ( a, b, c, d, x, s, t) => cm( ( b & c ) | ( ( ~ b ) & d ), a, b, x, s, t)
  19.     let gg = ( a, b, c, d, x, s, t) => cm( ( b & d ) | ( c & ( ~ d ) ), a, b, x, s, t)
  20.     let hh = ( a, b, c, d, x, s, t) => cm( b ^ c ^ d, a, b, x, s, t )
  21.     let ii = ( a, b, c, d, x, s, t) => cm( c ^ ( b | ( ~ d ) ), a, b, x, s, t )
  22.     let sb = x => {
  23.         let i
  24.         let nblk = ( ( x.length + 8 ) >> 6 ) + 1
  25.         let blks = new Array( nblk * 16 )
  26.         for ( i = 0; i < nblk * 16; i++ )
  27.             blks[i] = 0
  28.         for ( i = 0; i < x.length; i++)
  29.             blks[ i >> 2 ] |= x.charCodeAt( i ) << ( ( i % 4 ) * 8 )
  30.         blks[ i >> 2 ] |= 0x80 << ( ( i % 4 ) * 8 )
  31.         blks[ nblk * 16 - 2 ] = x.length * 8
  32.         return blks
  33.     }
  34.     let i,
  35.         x = sb( str ),
  36.         a = 1732584193,
  37.         b = -271733879,
  38.         c = -1732584194,
  39.         d = 271733878,
  40.         olda,
  41.         oldb,
  42.         oldc,
  43.         oldd
  44.     for ( i = 0; i < x.length; i += 16 ){
  45.         olda = a
  46.         oldb = b
  47.         oldc = c
  48.         oldd = d
  49.         a = ff( a, b, c, d, x[ i + 0 ], 7, -680876936 )
  50.         d = ff( d, a, b, c, x[ i + 1 ], 12, -389564586 )
  51.         c = ff( c, d, a, b, x[ i + 2 ], 17, 606105819 )
  52.         b = ff( b, c, d, a, x[ i + 3 ], 22, -1044525330 )
  53.         a = ff( a, b, c, d, x[ i + 4 ], 7, -176418897 )
  54.         d = ff( d, a, b, c, x[ i + 5 ], 12, 1200080426 )
  55.         c = ff( c, d, a, b, x[ i + 6 ], 17, -1473231341 )
  56.         b = ff( b, c, d, a, x[ i + 7 ], 22, -45705983 )
  57.         a = ff( a, b, c, d, x[ i + 8 ], 7, 1770035416 )
  58.         d = ff( d, a, b, c, x[ i + 9 ], 12, -1958414417 )
  59.         c = ff( c, d, a, b, x[ i + 10 ], 17, -42063 )
  60.         b = ff( b, c, d, a, x[ i + 11 ], 22, -1990404162 )
  61.         a = ff( a, b, c, d, x[ i + 12 ], 7, 1804603682 )
  62.         d = ff( d, a, b, c, x[ i + 13 ], 12, -40341101 )
  63.         c = ff( c, d, a, b, x[ i + 14 ], 17, -1502002290 )
  64.         b = ff( b, c, d, a, x[ i + 15 ], 22, 1236535329 )
  65.         a = gg( a, b, c, d, x[ i + 1 ], 5, -165796510 )
  66.         d = gg( d, a, b, c, x[ i + 6 ], 9, -1069501632 )
  67.         c = gg( c, d, a, b, x[ i + 11 ], 14, 643717713 )
  68.         b = gg( b, c, d, a, x[ i + 0 ], 20, -373897302 )
  69.         a = gg( a, b, c, d, x[ i + 5 ], 5, -701558691 )
  70.         d = gg( d, a, b, c, x[ i + 10 ], 9, 38016083 )
  71.         c = gg( c, d, a, b, x[ i + 15 ], 14, -660478335 )
  72.         b = gg( b, c, d, a, x[ i + 4 ], 20, -405537848 )
  73.         a = gg( a, b, c, d, x[ i + 9 ], 5, 568446438 )
  74.         d = gg( d, a, b, c, x[ i + 14 ], 9, -1019803690 )
  75.         c = gg( c, d, a, b, x[ i + 3 ], 14, -187363961 )
  76.         b = gg( b, c, d, a, x[ i + 8 ], 20, 1163531501 )
  77.         a = gg( a, b, c, d, x[ i + 13 ], 5, -1444681467 )
  78.         d = gg( d, a, b, c, x[ i + 2 ], 9, -51403784 )
  79.         c = gg( c, d, a, b, x[ i + 7 ], 14, 1735328473 )
  80.         b = gg( b, c, d, a, x[ i + 12 ], 20, -1926607734 )
  81.         a = hh( a, b, c, d, x[ i + 5 ], 4, -378558 )
  82.         d = hh( d, a, b, c, x[ i + 8 ], 11, -2022574463 )
  83.         c = hh( c, d, a, b, x[ i + 11 ], 16, 1839030562 )
  84.         b = hh( b, c, d, a, x[ i + 14 ], 23, -35309556 )
  85.         a = hh( a, b, c, d, x[ i + 1 ], 4, -1530992060 )
  86.         d = hh( d, a, b, c, x[ i + 4 ], 11, 1272893353 )
  87.         c = hh( c, d, a, b, x[ i + 7 ], 16, -155497632 )
  88.         b = hh( b, c, d, a, x[ i + 10 ], 23, -1094730640 )
  89.         a = hh( a, b, c, d, x[ i + 13 ], 4, 681279174 )
  90.         d = hh( d, a, b, c, x[ i + 0 ], 11, -358537222 )
  91.         c = hh( c, d, a, b, x[ i + 3 ], 16, -722521979 )
  92.         b = hh( b, c, d, a, x[ i + 6 ], 23, 76029189 )
  93.         a = hh( a, b, c, d, x[ i + 9 ], 4, -640364487 )
  94.         d = hh( d, a, b, c, x[ i + 12 ], 11, -421815835 )
  95.         c = hh( c, d, a, b, x[ i + 15 ], 16, 530742520 )
  96.         b = hh( b, c, d, a, x[ i + 2 ], 23, -995338651 )
  97.         a = ii( a, b, c, d, x[ i + 0 ], 6, -198630844 )
  98.         d = ii( d, a, b, c, x[ i + 7 ], 10, 1126891415 )
  99.         c = ii( c, d, a, b, x[ i + 14 ], 15, -1416354905 )
  100.         b = ii( b, c, d, a, x[ i + 5 ], 21, -57434055 )
  101.         a = ii( a, b, c, d, x[ i + 12 ], 6, 1700485571 )
  102.         d = ii( d, a, b, c, x[ i + 3 ], 10, -1894986606 )
  103.         c = ii( c, d, a, b, x[ i + 10 ], 15, -1051523 )
  104.         b = ii( b, c, d, a, x[ i + 1 ], 21, -2054922799 )
  105.         a = ii( a, b, c, d, x[ i + 8 ], 6, 1873313359 )
  106.         d = ii( d, a, b, c, x[ i + 15 ], 10, -30611744 )
  107.         c = ii( c, d, a, b, x[ i + 6 ], 15, -1560198380 )
  108.         b = ii( b, c, d, a, x[ i + 13 ], 21, 1309151649 )
  109.         a = ii( a, b, c, d, x[ i + 4 ], 6, -145523070 )
  110.         d = ii( d, a, b, c, x[ i + 11 ], 10, -1120210379 )
  111.         c = ii( c, d, a, b, x[ i + 2 ], 15, 718787259 )
  112.         b = ii( b, c, d, a, x[ i + 9 ], 21, -343485551 )
  113.         a = ad( a, olda )
  114.         b = ad( b, oldb )
  115.         c = ad( c, oldc )
  116.         d = ad( d, oldd )
  117.     }
  118.     return rh( a ) + rh( b ) + rh( c ) + rh( d )
  119. }
  120.  
RAW Paste Data