Home > Web Front-end > JS Tutorial > Javascript MD4_Basics

Javascript MD4_Basics

WBOY
Release: 2016-05-16 19:23:15
Original
1160 people have browsed it
From:http://pajhome.org.uk/crypt/md5/md4src.html
<span class="comment">/*
 * A JavaScript implementation of the RSA Data Security, Inc. MD4 Message
 * Digest Algorithm, as defined in RFC 1320.
 * Version 2.1 Copyright (C) Jerrad Pierce, Paul Johnston 1999 - 2002.
 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 * Distributed under the BSD License
 * See http://pajhome.org.uk/crypt/md5 for more info.
 */</span>

<span class="comment">/*
 * Configurable variables. You may need to tweak these to be compatible with
 * the server-side, but the defaults work in most cases.
 */</span>
<span class="reserved_word">var</span> hexcase <span class="symbol">=</span> <span class="numeric">0</span><span class="symbol">;</span> <span class="comment">/* hex output format. 0 - lowercase; 1 - uppercase    */</span>
<span class="reserved_word">var</span> b64pad <span class="symbol">=</span> <span class="string">""</span><span class="symbol">;</span> <span class="comment">/* base-64 pad character. "=" for strict RFC compliance  */</span>
<span class="reserved_word">var</span> chrsz  <span class="symbol">=</span> <span class="numeric">8</span><span class="symbol">;</span> <span class="comment">/* bits per input character. 8 - ASCII; 16 - Unicode   */</span>

<span class="comment">/*
 * These are the functions you'll usually want to call
 */</span>
<span class="reserved_word">function</span> hex_md4<span class="braces">(</span>s<span class="braces">)</span><span class="braces">{</span> <span class="reserved_word">return</span> binl2hex<span class="braces">(</span>core_md4<span class="braces">(</span>str2binl<span class="braces">(</span>s<span class="braces">)</span>, s.<span class="predefined_identifier">length</span> * chrsz<span class="braces">)</span><span class="braces">)</span><span class="symbol">;</span><span class="braces">}</span>
<span class="reserved_word">function</span> b64_md4<span class="braces">(</span>s<span class="braces">)</span><span class="braces">{</span> <span class="reserved_word">return</span> binl2b64<span class="braces">(</span>core_md4<span class="braces">(</span>str2binl<span class="braces">(</span>s<span class="braces">)</span>, s.<span class="predefined_identifier">length</span> * chrsz<span class="braces">)</span><span class="braces">)</span><span class="symbol">;</span><span class="braces">}</span>
<span class="reserved_word">function</span> str_md4<span class="braces">(</span>s<span class="braces">)</span><span class="braces">{</span> <span class="reserved_word">return</span> binl2str<span class="braces">(</span>core_md4<span class="braces">(</span>str2binl<span class="braces">(</span>s<span class="braces">)</span>, s.<span class="predefined_identifier">length</span> * chrsz<span class="braces">)</span><span class="braces">)</span><span class="symbol">;</span><span class="braces">}</span>
<span class="reserved_word">function</span> hex_hmac_md4<span class="braces">(</span>key, data<span class="braces">)</span> <span class="braces">{</span> <span class="reserved_word">return</span> binl2hex<span class="braces">(</span>core_hmac_md4<span class="braces">(</span>key, data<span class="braces">)</span><span class="braces">)</span><span class="symbol">;</span> <span class="braces">}</span>
<span class="reserved_word">function</span> b64_hmac_md4<span class="braces">(</span>key, data<span class="braces">)</span> <span class="braces">{</span> <span class="reserved_word">return</span> binl2b64<span class="braces">(</span>core_hmac_md4<span class="braces">(</span>key, data<span class="braces">)</span><span class="braces">)</span><span class="symbol">;</span> <span class="braces">}</span>
<span class="reserved_word">function</span> str_hmac_md4<span class="braces">(</span>key, data<span class="braces">)</span> <span class="braces">{</span> <span class="reserved_word">return</span> binl2str<span class="braces">(</span>core_hmac_md4<span class="braces">(</span>key, data<span class="braces">)</span><span class="braces">)</span><span class="symbol">;</span> <span class="braces">}</span>

<span class="comment">/* 
 * Perform a simple self-test to see if the VM is working 
 */</span>
<span class="reserved_word">function</span> md4_vm_test<span class="braces">(</span><span class="braces">)</span>
<span class="braces">{</span>
 <span class="reserved_word">return</span> hex_md4<span class="braces">(</span><span class="string">"abc"</span><span class="braces">)</span> <span class="symbol">=</span><span class="symbol">=</span> <span class="string">"a448017aaf21d8525fc10ae87aa6729d"</span><span class="symbol">;</span>
<span class="braces">}</span>

<span class="comment">/*
 * Calculate the MD4 of an array of little-endian words, and a bit length
 */</span>
<span class="reserved_word">function</span> core_md4<span class="braces">(</span>x, len<span class="braces">)</span>
<span class="braces">{</span>
 <span class="comment">/* append padding */</span>
 x[len <span class="symbol">></span><span class="symbol">></span> <span class="numeric">5</span>] <span class="symbol">|</span><span class="symbol">=</span> <span class="numeric">0x80</span> <span class="symbol"><</SPAN><SPAN class=symbol><</SPAN> <SPAN class=braces>(</SPAN>len % <SPAN class=numeric>32</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
 x[<SPAN class=braces>(</SPAN><SPAN class=braces>(</SPAN><SPAN class=braces>(</SPAN>len + <SPAN class=numeric>64</SPAN><SPAN class=braces>)</SPAN> <SPAN class=symbol>></span><span class="symbol">></span><span class="symbol">></span> <span class="numeric">9</span><span class="braces">)</span> <span class="symbol"><</SPAN><SPAN class=symbol><</SPAN> <SPAN class=numeric>4</SPAN><SPAN class=braces>)</SPAN> + <SPAN class=numeric>14</SPAN>] <SPAN class=symbol>=</SPAN> len<SPAN class=symbol>;</SPAN>
 
 <SPAN class=reserved_word>var</SPAN> a <SPAN class=symbol>=</SPAN> <SPAN class=numeric>1732584193</SPAN><SPAN class=symbol>;</SPAN>
 <SPAN class=reserved_word>var</SPAN> b <SPAN class=symbol>=</SPAN> -<SPAN class=numeric>271733879</SPAN><SPAN class=symbol>;</SPAN>
 <SPAN class=reserved_word>var</SPAN> c <SPAN class=symbol>=</SPAN> -<SPAN class=numeric>1732584194</SPAN><SPAN class=symbol>;</SPAN>
 <SPAN class=reserved_word>var</SPAN> d <SPAN class=symbol>=</SPAN> <SPAN class=numeric>271733878</SPAN><SPAN class=symbol>;</SPAN>

 <SPAN class=reserved_word>for</SPAN><SPAN class=braces>(</SPAN><SPAN class=reserved_word>var</SPAN> i <SPAN class=symbol>=</SPAN> <SPAN class=numeric>0</SPAN><SPAN class=symbol>;</SPAN> i <SPAN class=symbol><</SPAN> x.<SPAN class=predefined_identifier>length</SPAN><SPAN class=symbol>;</SPAN> i +<SPAN class=symbol>=</SPAN> <SPAN class=numeric>16</SPAN><SPAN class=braces>)</SPAN>
 <SPAN class=braces>{</SPAN>
  <SPAN class=reserved_word>var</SPAN> olda <SPAN class=symbol>=</SPAN> a<SPAN class=symbol>;</SPAN>
  <SPAN class=reserved_word>var</SPAN> oldb <SPAN class=symbol>=</SPAN> b<SPAN class=symbol>;</SPAN>
  <SPAN class=reserved_word>var</SPAN> oldc <SPAN class=symbol>=</SPAN> c<SPAN class=symbol>;</SPAN>
  <SPAN class=reserved_word>var</SPAN> oldd <SPAN class=symbol>=</SPAN> d<SPAN class=symbol>;</SPAN>

  a <SPAN class=symbol>=</SPAN> md4_ff<SPAN class=braces>(</SPAN>a, b, c, d, x[i+ <SPAN class=numeric>0</SPAN>], <SPAN class=numeric>3</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  d <SPAN class=symbol>=</SPAN> md4_ff<SPAN class=braces>(</SPAN>d, a, b, c, x[i+ <SPAN class=numeric>1</SPAN>], <SPAN class=numeric>7</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  c <SPAN class=symbol>=</SPAN> md4_ff<SPAN class=braces>(</SPAN>c, d, a, b, x[i+ <SPAN class=numeric>2</SPAN>], <SPAN class=numeric>11</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  b <SPAN class=symbol>=</SPAN> md4_ff<SPAN class=braces>(</SPAN>b, c, d, a, x[i+ <SPAN class=numeric>3</SPAN>], <SPAN class=numeric>19</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  a <SPAN class=symbol>=</SPAN> md4_ff<SPAN class=braces>(</SPAN>a, b, c, d, x[i+ <SPAN class=numeric>4</SPAN>], <SPAN class=numeric>3</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  d <SPAN class=symbol>=</SPAN> md4_ff<SPAN class=braces>(</SPAN>d, a, b, c, x[i+ <SPAN class=numeric>5</SPAN>], <SPAN class=numeric>7</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  c <SPAN class=symbol>=</SPAN> md4_ff<SPAN class=braces>(</SPAN>c, d, a, b, x[i+ <SPAN class=numeric>6</SPAN>], <SPAN class=numeric>11</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  b <SPAN class=symbol>=</SPAN> md4_ff<SPAN class=braces>(</SPAN>b, c, d, a, x[i+ <SPAN class=numeric>7</SPAN>], <SPAN class=numeric>19</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  a <SPAN class=symbol>=</SPAN> md4_ff<SPAN class=braces>(</SPAN>a, b, c, d, x[i+ <SPAN class=numeric>8</SPAN>], <SPAN class=numeric>3</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  d <SPAN class=symbol>=</SPAN> md4_ff<SPAN class=braces>(</SPAN>d, a, b, c, x[i+ <SPAN class=numeric>9</SPAN>], <SPAN class=numeric>7</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  c <SPAN class=symbol>=</SPAN> md4_ff<SPAN class=braces>(</SPAN>c, d, a, b, x[i+<SPAN class=numeric>10</SPAN>], <SPAN class=numeric>11</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  b <SPAN class=symbol>=</SPAN> md4_ff<SPAN class=braces>(</SPAN>b, c, d, a, x[i+<SPAN class=numeric>11</SPAN>], <SPAN class=numeric>19</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  a <SPAN class=symbol>=</SPAN> md4_ff<SPAN class=braces>(</SPAN>a, b, c, d, x[i+<SPAN class=numeric>12</SPAN>], <SPAN class=numeric>3</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  d <SPAN class=symbol>=</SPAN> md4_ff<SPAN class=braces>(</SPAN>d, a, b, c, x[i+<SPAN class=numeric>13</SPAN>], <SPAN class=numeric>7</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  c <SPAN class=symbol>=</SPAN> md4_ff<SPAN class=braces>(</SPAN>c, d, a, b, x[i+<SPAN class=numeric>14</SPAN>], <SPAN class=numeric>11</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  b <SPAN class=symbol>=</SPAN> md4_ff<SPAN class=braces>(</SPAN>b, c, d, a, x[i+<SPAN class=numeric>15</SPAN>], <SPAN class=numeric>19</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>

  a <SPAN class=symbol>=</SPAN> md4_gg<SPAN class=braces>(</SPAN>a, b, c, d, x[i+ <SPAN class=numeric>0</SPAN>], <SPAN class=numeric>3</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  d <SPAN class=symbol>=</SPAN> md4_gg<SPAN class=braces>(</SPAN>d, a, b, c, x[i+ <SPAN class=numeric>4</SPAN>], <SPAN class=numeric>5</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  c <SPAN class=symbol>=</SPAN> md4_gg<SPAN class=braces>(</SPAN>c, d, a, b, x[i+ <SPAN class=numeric>8</SPAN>], <SPAN class=numeric>9</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  b <SPAN class=symbol>=</SPAN> md4_gg<SPAN class=braces>(</SPAN>b, c, d, a, x[i+<SPAN class=numeric>12</SPAN>], <SPAN class=numeric>13</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  a <SPAN class=symbol>=</SPAN> md4_gg<SPAN class=braces>(</SPAN>a, b, c, d, x[i+ <SPAN class=numeric>1</SPAN>], <SPAN class=numeric>3</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  d <SPAN class=symbol>=</SPAN> md4_gg<SPAN class=braces>(</SPAN>d, a, b, c, x[i+ <SPAN class=numeric>5</SPAN>], <SPAN class=numeric>5</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  c <SPAN class=symbol>=</SPAN> md4_gg<SPAN class=braces>(</SPAN>c, d, a, b, x[i+ <SPAN class=numeric>9</SPAN>], <SPAN class=numeric>9</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  b <SPAN class=symbol>=</SPAN> md4_gg<SPAN class=braces>(</SPAN>b, c, d, a, x[i+<SPAN class=numeric>13</SPAN>], <SPAN class=numeric>13</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  a <SPAN class=symbol>=</SPAN> md4_gg<SPAN class=braces>(</SPAN>a, b, c, d, x[i+ <SPAN class=numeric>2</SPAN>], <SPAN class=numeric>3</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  d <SPAN class=symbol>=</SPAN> md4_gg<SPAN class=braces>(</SPAN>d, a, b, c, x[i+ <SPAN class=numeric>6</SPAN>], <SPAN class=numeric>5</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  c <SPAN class=symbol>=</SPAN> md4_gg<SPAN class=braces>(</SPAN>c, d, a, b, x[i+<SPAN class=numeric>10</SPAN>], <SPAN class=numeric>9</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  b <SPAN class=symbol>=</SPAN> md4_gg<SPAN class=braces>(</SPAN>b, c, d, a, x[i+<SPAN class=numeric>14</SPAN>], <SPAN class=numeric>13</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  a <SPAN class=symbol>=</SPAN> md4_gg<SPAN class=braces>(</SPAN>a, b, c, d, x[i+ <SPAN class=numeric>3</SPAN>], <SPAN class=numeric>3</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  d <SPAN class=symbol>=</SPAN> md4_gg<SPAN class=braces>(</SPAN>d, a, b, c, x[i+ <SPAN class=numeric>7</SPAN>], <SPAN class=numeric>5</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  c <SPAN class=symbol>=</SPAN> md4_gg<SPAN class=braces>(</SPAN>c, d, a, b, x[i+<SPAN class=numeric>11</SPAN>], <SPAN class=numeric>9</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  b <SPAN class=symbol>=</SPAN> md4_gg<SPAN class=braces>(</SPAN>b, c, d, a, x[i+<SPAN class=numeric>15</SPAN>], <SPAN class=numeric>13</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>

  a <SPAN class=symbol>=</SPAN> md4_hh<SPAN class=braces>(</SPAN>a, b, c, d, x[i+ <SPAN class=numeric>0</SPAN>], <SPAN class=numeric>3</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  d <SPAN class=symbol>=</SPAN> md4_hh<SPAN class=braces>(</SPAN>d, a, b, c, x[i+ <SPAN class=numeric>8</SPAN>], <SPAN class=numeric>9</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  c <SPAN class=symbol>=</SPAN> md4_hh<SPAN class=braces>(</SPAN>c, d, a, b, x[i+ <SPAN class=numeric>4</SPAN>], <SPAN class=numeric>11</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  b <SPAN class=symbol>=</SPAN> md4_hh<SPAN class=braces>(</SPAN>b, c, d, a, x[i+<SPAN class=numeric>12</SPAN>], <SPAN class=numeric>15</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  a <SPAN class=symbol>=</SPAN> md4_hh<SPAN class=braces>(</SPAN>a, b, c, d, x[i+ <SPAN class=numeric>2</SPAN>], <SPAN class=numeric>3</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  d <SPAN class=symbol>=</SPAN> md4_hh<SPAN class=braces>(</SPAN>d, a, b, c, x[i+<SPAN class=numeric>10</SPAN>], <SPAN class=numeric>9</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  c <SPAN class=symbol>=</SPAN> md4_hh<SPAN class=braces>(</SPAN>c, d, a, b, x[i+ <SPAN class=numeric>6</SPAN>], <SPAN class=numeric>11</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  b <SPAN class=symbol>=</SPAN> md4_hh<SPAN class=braces>(</SPAN>b, c, d, a, x[i+<SPAN class=numeric>14</SPAN>], <SPAN class=numeric>15</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  a <SPAN class=symbol>=</SPAN> md4_hh<SPAN class=braces>(</SPAN>a, b, c, d, x[i+ <SPAN class=numeric>1</SPAN>], <SPAN class=numeric>3</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  d <SPAN class=symbol>=</SPAN> md4_hh<SPAN class=braces>(</SPAN>d, a, b, c, x[i+ <SPAN class=numeric>9</SPAN>], <SPAN class=numeric>9</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  c <SPAN class=symbol>=</SPAN> md4_hh<SPAN class=braces>(</SPAN>c, d, a, b, x[i+ <SPAN class=numeric>5</SPAN>], <SPAN class=numeric>11</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  b <SPAN class=symbol>=</SPAN> md4_hh<SPAN class=braces>(</SPAN>b, c, d, a, x[i+<SPAN class=numeric>13</SPAN>], <SPAN class=numeric>15</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  a <SPAN class=symbol>=</SPAN> md4_hh<SPAN class=braces>(</SPAN>a, b, c, d, x[i+ <SPAN class=numeric>3</SPAN>], <SPAN class=numeric>3</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  d <SPAN class=symbol>=</SPAN> md4_hh<SPAN class=braces>(</SPAN>d, a, b, c, x[i+<SPAN class=numeric>11</SPAN>], <SPAN class=numeric>9</SPAN> <SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  c <SPAN class=symbol>=</SPAN> md4_hh<SPAN class=braces>(</SPAN>c, d, a, b, x[i+ <SPAN class=numeric>7</SPAN>], <SPAN class=numeric>11</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  b <SPAN class=symbol>=</SPAN> md4_hh<SPAN class=braces>(</SPAN>b, c, d, a, x[i+<SPAN class=numeric>15</SPAN>], <SPAN class=numeric>15</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>

  a <SPAN class=symbol>=</SPAN> safe_add<SPAN class=braces>(</SPAN>a, olda<SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  b <SPAN class=symbol>=</SPAN> safe_add<SPAN class=braces>(</SPAN>b, oldb<SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  c <SPAN class=symbol>=</SPAN> safe_add<SPAN class=braces>(</SPAN>c, oldc<SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
  d <SPAN class=symbol>=</SPAN> safe_add<SPAN class=braces>(</SPAN>d, oldd<SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>

 <SPAN class=braces>}</SPAN>
 <SPAN class=reserved_word>return</SPAN> <SPAN class=predefined_type>Array</SPAN><SPAN class=braces>(</SPAN>a, b, c, d<SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>

<SPAN class=braces>}</SPAN>

<SPAN class=comment>/*
 * These functions implement the basic operation for each round of the
 * algorithm.
 */</SPAN>
<SPAN class=reserved_word>function</SPAN> md4_cmn<SPAN class=braces>(</SPAN>q, a, b, x, s, t<SPAN class=braces>)</SPAN>
<SPAN class=braces>{</SPAN>
 <SPAN class=reserved_word>return</SPAN> safe_add<SPAN class=braces>(</SPAN>rol<SPAN class=braces>(</SPAN>safe_add<SPAN class=braces>(</SPAN>safe_add<SPAN class=braces>(</SPAN>a, q<SPAN class=braces>)</SPAN>, safe_add<SPAN class=braces>(</SPAN>x, t<SPAN class=braces>)</SPAN><SPAN class=braces>)</SPAN>, s<SPAN class=braces>)</SPAN>, b<SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
<SPAN class=braces>}</SPAN>
<SPAN class=reserved_word>function</SPAN> md4_ff<SPAN class=braces>(</SPAN>a, b, c, d, x, s<SPAN class=braces>)</SPAN>
<SPAN class=braces>{</SPAN>
 <SPAN class=reserved_word>return</SPAN> md4_cmn<SPAN class=braces>(</SPAN><SPAN class=braces>(</SPAN>b <SPAN class=symbol>&</SPAN> c<SPAN class=braces>)</SPAN> <SPAN class=symbol>|</SPAN> <SPAN class=braces>(</SPAN><SPAN class=braces>(</SPAN>~b<SPAN class=braces>)</SPAN> <SPAN class=symbol>&</SPAN> d<SPAN class=braces>)</SPAN>, a, <SPAN class=numeric>0</SPAN>, x, s, <SPAN class=numeric>0</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
<SPAN class=braces>}</SPAN>
<SPAN class=reserved_word>function</SPAN> md4_gg<SPAN class=braces>(</SPAN>a, b, c, d, x, s<SPAN class=braces>)</SPAN>
<SPAN class=braces>{</SPAN>
 <SPAN class=reserved_word>return</SPAN> md4_cmn<SPAN class=braces>(</SPAN><SPAN class=braces>(</SPAN>b <SPAN class=symbol>&</SPAN> c<SPAN class=braces>)</SPAN> <SPAN class=symbol>|</SPAN> <SPAN class=braces>(</SPAN>b <SPAN class=symbol>&</SPAN> d<SPAN class=braces>)</SPAN> <SPAN class=symbol>|</SPAN> <SPAN class=braces>(</SPAN>c <SPAN class=symbol>&</SPAN> d<SPAN class=braces>)</SPAN>, a, <SPAN class=numeric>0</SPAN>, x, s, <SPAN class=numeric>1518500249</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
<SPAN class=braces>}</SPAN>
<SPAN class=reserved_word>function</SPAN> md4_hh<SPAN class=braces>(</SPAN>a, b, c, d, x, s<SPAN class=braces>)</SPAN>
<SPAN class=braces>{</SPAN>
 <SPAN class=reserved_word>return</SPAN> md4_cmn<SPAN class=braces>(</SPAN>b ^ c ^ d, a, <SPAN class=numeric>0</SPAN>, x, s, <SPAN class=numeric>1859775393</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
<SPAN class=braces>}</SPAN>

<SPAN class=comment>/*
 * Calculate the HMAC-MD4, of a key and some data
 */</SPAN>
<SPAN class=reserved_word>function</SPAN> core_hmac_md4<SPAN class=braces>(</SPAN>key, data<SPAN class=braces>)</SPAN>
<SPAN class=braces>{</SPAN>
 <SPAN class=reserved_word>var</SPAN> bkey <SPAN class=symbol>=</SPAN> str2binl<SPAN class=braces>(</SPAN>key<SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
 <SPAN class=reserved_word>if</SPAN><SPAN class=braces>(</SPAN>bkey.<SPAN class=predefined_identifier>length</SPAN> <SPAN class=symbol>></span> <span class="numeric">16</span><span class="braces">)</span> bkey <span class="symbol">=</span> core_md4<span class="braces">(</span>bkey, key.<span class="predefined_identifier">length</span> * chrsz<span class="braces">)</span><span class="symbol">;</span>

 <span class="reserved_word">var</span> ipad <span class="symbol">=</span> <span class="predefined_type">Array</span><span class="braces">(</span><span class="numeric">16</span><span class="braces">)</span>, opad <span class="symbol">=</span> <span class="predefined_type">Array</span><span class="braces">(</span><span class="numeric">16</span><span class="braces">)</span><span class="symbol">;</span>
 <span class="reserved_word">for</span><span class="braces">(</span><span class="reserved_word">var</span> i <span class="symbol">=</span> <span class="numeric">0</span><span class="symbol">;</span> i <span class="symbol"><</SPAN> <SPAN class=numeric>16</SPAN><SPAN class=symbol>;</SPAN> i++<SPAN class=braces>)</SPAN> 
 <SPAN class=braces>{</SPAN>
  ipad[i] <SPAN class=symbol>=</SPAN> bkey[i] ^ <SPAN class=numeric>0x36363636</SPAN><SPAN class=symbol>;</SPAN>
  opad[i] <SPAN class=symbol>=</SPAN> bkey[i] ^ <SPAN class=numeric>0x5C5C5C5C</SPAN><SPAN class=symbol>;</SPAN>
 <SPAN class=braces>}</SPAN>

 <SPAN class=reserved_word>var</SPAN> <SPAN class=predefined_identifier>hash</SPAN> <SPAN class=symbol>=</SPAN> core_md4<SPAN class=braces>(</SPAN>ipad.concat<SPAN class=braces>(</SPAN>str2binl<SPAN class=braces>(</SPAN>data<SPAN class=braces>)</SPAN><SPAN class=braces>)</SPAN>, <SPAN class=numeric>512</SPAN> + data.<SPAN class=predefined_identifier>length</SPAN> * chrsz<SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
 <SPAN class=reserved_word>return</SPAN> core_md4<SPAN class=braces>(</SPAN>opad.concat<SPAN class=braces>(</SPAN><SPAN class=predefined_identifier>hash</SPAN><SPAN class=braces>)</SPAN>, <SPAN class=numeric>512</SPAN> + <SPAN class=numeric>128</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
<SPAN class=braces>}</SPAN>

<SPAN class=comment>/*
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
 * to work around bugs in some JS interpreters.
 */</SPAN>
<SPAN class=reserved_word>function</SPAN> safe_add<SPAN class=braces>(</SPAN>x, y<SPAN class=braces>)</SPAN>
<SPAN class=braces>{</SPAN>
 <SPAN class=reserved_word>var</SPAN> lsw <SPAN class=symbol>=</SPAN> <SPAN class=braces>(</SPAN>x <SPAN class=symbol>&</SPAN> <SPAN class=numeric>0xFFFF</SPAN><SPAN class=braces>)</SPAN> + <SPAN class=braces>(</SPAN>y <SPAN class=symbol>&</SPAN> <SPAN class=numeric>0xFFFF</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
 <SPAN class=reserved_word>var</SPAN> msw <SPAN class=symbol>=</SPAN> <SPAN class=braces>(</SPAN>x <SPAN class=symbol>></span><span class="symbol">></span> <span class="numeric">16</span><span class="braces">)</span> + <span class="braces">(</span>y <span class="symbol">></span><span class="symbol">></span> <span class="numeric">16</span><span class="braces">)</span> + <span class="braces">(</span>lsw <span class="symbol">></span><span class="symbol">></span> <span class="numeric">16</span><span class="braces">)</span><span class="symbol">;</span>
 <span class="reserved_word">return</span> <span class="braces">(</span>msw <span class="symbol"><</SPAN><SPAN class=symbol><</SPAN> <SPAN class=numeric>16</SPAN><SPAN class=braces>)</SPAN> <SPAN class=symbol>|</SPAN> <SPAN class=braces>(</SPAN>lsw <SPAN class=symbol>&</SPAN> <SPAN class=numeric>0xFFFF</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
<SPAN class=braces>}</SPAN>

<SPAN class=comment>/*
 * Bitwise rotate a 32-bit number to the left.
 */</SPAN>
<SPAN class=reserved_word>function</SPAN> rol<SPAN class=braces>(</SPAN>num, cnt<SPAN class=braces>)</SPAN>
<SPAN class=braces>{</SPAN>
 <SPAN class=reserved_word>return</SPAN> <SPAN class=braces>(</SPAN>num <SPAN class=symbol><</SPAN><SPAN class=symbol><</SPAN> cnt<SPAN class=braces>)</SPAN> <SPAN class=symbol>|</SPAN> <SPAN class=braces>(</SPAN>num <SPAN class=symbol>></span><span class="symbol">></span><span class="symbol">></span> <span class="braces">(</span><span class="numeric">32</span> - cnt<span class="braces">)</span><span class="braces">)</span><span class="symbol">;</span>
<span class="braces">}</span>

<span class="comment">/*
 * Convert a string to an array of little-endian words
 * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
 */</span>
<span class="reserved_word">function</span> str2binl<span class="braces">(</span>str<span class="braces">)</span>
<span class="braces">{</span>
 <span class="reserved_word">var</span> bin <span class="symbol">=</span> <span class="predefined_type">Array</span><span class="braces">(</span><span class="braces">)</span><span class="symbol">;</span>
 <span class="reserved_word">var</span> mask <span class="symbol">=</span> <span class="braces">(</span><span class="numeric">1</span> <span class="symbol"><</SPAN><SPAN class=symbol><</SPAN> chrsz<SPAN class=braces>)</SPAN> - <SPAN class=numeric>1</SPAN><SPAN class=symbol>;</SPAN>
 <SPAN class=reserved_word>for</SPAN><SPAN class=braces>(</SPAN><SPAN class=reserved_word>var</SPAN> i <SPAN class=symbol>=</SPAN> <SPAN class=numeric>0</SPAN><SPAN class=symbol>;</SPAN> i <SPAN class=symbol><</SPAN> str.<SPAN class=predefined_identifier>length</SPAN> * chrsz<SPAN class=symbol>;</SPAN> i +<SPAN class=symbol>=</SPAN> chrsz<SPAN class=braces>)</SPAN>
  bin[i<SPAN class=symbol>></span><span class="symbol">></span><span class="numeric">5</span>] <span class="symbol">|</span><span class="symbol">=</span> <span class="braces">(</span>str.charCodeAt<span class="braces">(</span>i <span class="symbol">/</span> chrsz<span class="braces">)</span> <span class="symbol">&</span> mask<span class="braces">)</span> <span class="symbol"><</SPAN><SPAN class=symbol><</SPAN> <SPAN class=braces>(</SPAN>i%<SPAN class=numeric>32</SPAN><SPAN class=braces>)</SPAN><SPAN class=symbol>;</SPAN>
 <SPAN class=reserved_word>return</SPAN> bin<SPAN class=symbol>;</SPAN>
<SPAN class=braces>}</SPAN>

<SPAN class=comment>/*
 * Convert an array of little-endian words to a string
 */</SPAN>
<SPAN class=reserved_word>function</SPAN> binl2str<SPAN class=braces>(</SPAN>bin<SPAN class=braces>)</SPAN>
<SPAN class=braces>{</SPAN>
 <SPAN class=reserved_word>var</SPAN> str <SPAN class=symbol>=</SPAN> <SPAN class=string>""</SPAN><SPAN class=symbol>;</SPAN>
 <SPAN class=reserved_word>var</SPAN> mask <SPAN class=symbol>=</SPAN> <SPAN class=braces>(</SPAN><SPAN class=numeric>1</SPAN> <SPAN class=symbol><</SPAN><SPAN class=symbol><</SPAN> chrsz<SPAN class=braces>)</SPAN> - <SPAN class=numeric>1</SPAN><SPAN class=symbol>;</SPAN>
 <SPAN class=reserved_word>for</SPAN><SPAN class=braces>(</SPAN><SPAN class=reserved_word>var</SPAN> i <SPAN class=symbol>=</SPAN> <SPAN class=numeric>0</SPAN><SPAN class=symbol>;</SPAN> i <SPAN class=symbol><</SPAN> bin.<SPAN class=predefined_identifier>length</SPAN> * <SPAN class=numeric>32</SPAN><SPAN class=symbol>;</SPAN> i +<SPAN class=symbol>=</SPAN> chrsz<SPAN class=braces>)</SPAN>
  str +<SPAN class=symbol>=</SPAN> String.fromCharCode<SPAN class=braces>(</SPAN><SPAN class=braces>(</SPAN>bin[i<SPAN class=symbol>></span><span class="symbol">></span><span class="numeric">5</span>] <span class="symbol">></span><span class="symbol">></span><span class="symbol">></span> <span class="braces">(</span>i % <span class="numeric">32</span><span class="braces">)</span><span class="braces">)</span> <span class="symbol">&</span> mask<span class="braces">)</span><span class="symbol">;</span>
 <span class="reserved_word">return</span> str<span class="symbol">;</span>
<span class="braces">}</span>

<span class="comment">/*
 * Convert an array of little-endian words to a hex string.
 */</span>
<span class="reserved_word">function</span> binl2hex<span class="braces">(</span>binarray<span class="braces">)</span>
<span class="braces">{</span>
 <span class="reserved_word">var</span> hex_tab <span class="symbol">=</span> hexcase ? <span class="string">"0123456789ABCDEF"</span> : <span class="string">"0123456789abcdef"</span><span class="symbol">;</span>
 <span class="reserved_word">var</span> str <span class="symbol">=</span> <span class="string">""</span><span class="symbol">;</span>
 <span class="reserved_word">for</span><span class="braces">(</span><span class="reserved_word">var</span> i <span class="symbol">=</span> <span class="numeric">0</span><span class="symbol">;</span> i <span class="symbol"><</SPAN> binarray.<SPAN class=predefined_identifier>length</SPAN> * <SPAN class=numeric>4</SPAN><SPAN class=symbol>;</SPAN> i++<SPAN class=braces>)</SPAN>
 <SPAN class=braces>{</SPAN>
  str +<SPAN class=symbol>=</SPAN> hex_tab.<SPAN class=library_function>charAt</SPAN><SPAN class=braces>(</SPAN><SPAN class=braces>(</SPAN>binarray[i<SPAN class=symbol>></span><span class="symbol">></span><span class="numeric">2</span>] <span class="symbol">></span><span class="symbol">></span> <span class="braces">(</span><span class="braces">(</span>i%<span class="numeric">4</span><span class="braces">)</span>*<span class="numeric">8</span>+<span class="numeric">4</span><span class="braces">)</span><span class="braces">)</span> <span class="symbol">&</span> <span class="numeric">0xF</span><span class="braces">)</span> +
      hex_tab.<span class="library_function">charAt</span><span class="braces">(</span><span class="braces">(</span>binarray[i<span class="symbol">></span><span class="symbol">></span><span class="numeric">2</span>] <span class="symbol">></span><span class="symbol">></span> <span class="braces">(</span><span class="braces">(</span>i%<span class="numeric">4</span><span class="braces">)</span>*<span class="numeric">8</span> <span class="braces">)</span><span class="braces">)</span> <span class="symbol">&</span> <span class="numeric">0xF</span><span class="braces">)</span><span class="symbol">;</span>
 <span class="braces">}</span>
 <span class="reserved_word">return</span> str<span class="symbol">;</span>
<span class="braces">}</span>

<span class="comment">/*
 * Convert an array of little-endian words to a base-64 string
 */</span>
<span class="reserved_word">function</span> binl2b64<span class="braces">(</span>binarray<span class="braces">)</span>
<span class="braces">{</span>
 <span class="reserved_word">var</span> tab <span class="symbol">=</span> <span class="string">"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"</span><span class="symbol">;</span>
 <span class="reserved_word">var</span> str <span class="symbol">=</span> <span class="string">""</span><span class="symbol">;</span>
 <span class="reserved_word">for</span><span class="braces">(</span><span class="reserved_word">var</span> i <span class="symbol">=</span> <span class="numeric">0</span><span class="symbol">;</span> i <span class="symbol"><</SPAN> binarray.<SPAN class=predefined_identifier>length</SPAN> * <SPAN class=numeric>4</SPAN><SPAN class=symbol>;</SPAN> i +<SPAN class=symbol>=</SPAN> <SPAN class=numeric>3</SPAN><SPAN class=braces>)</SPAN>
 <SPAN class=braces>{</SPAN>
  <SPAN class=reserved_word>var</SPAN> triplet <SPAN class=symbol>=</SPAN> <SPAN class=braces>(</SPAN><SPAN class=braces>(</SPAN><SPAN class=braces>(</SPAN>binarray[i  <SPAN class=symbol>></span><span class="symbol">></span> <span class="numeric">2</span>] <span class="symbol">></span><span class="symbol">></span> <span class="numeric">8</span> * <span class="braces">(</span> i  %<span class="numeric">4</span><span class="braces">)</span><span class="braces">)</span> <span class="symbol">&</span> <span class="numeric">0xFF</span><span class="braces">)</span> <span class="symbol"><</SPAN><SPAN class=symbol><</SPAN> <SPAN class=numeric>16</SPAN><SPAN class=braces>)</SPAN>
        <SPAN class=symbol>|</SPAN> <SPAN class=braces>(</SPAN><SPAN class=braces>(</SPAN><SPAN class=braces>(</SPAN>binarray[i+<SPAN class=numeric>1</SPAN> <SPAN class=symbol>></span><span class="symbol">></span> <span class="numeric">2</span>] <span class="symbol">></span><span class="symbol">></span> <span class="numeric">8</span> * <span class="braces">(</span><span class="braces">(</span>i+<span class="numeric">1</span><span class="braces">)</span>%<span class="numeric">4</span><span class="braces">)</span><span class="braces">)</span> <span class="symbol">&</span> <span class="numeric">0xFF</span><span class="braces">)</span> <span class="symbol"><</SPAN><SPAN class=symbol><</SPAN> <SPAN class=numeric>8</SPAN> <SPAN class=braces>)</SPAN>
        <SPAN class=symbol>|</SPAN> <SPAN class=braces>(</SPAN><SPAN class=braces>(</SPAN>binarray[i+<SPAN class=numeric>2</SPAN> <SPAN class=symbol>></span><span class="symbol">></span> <span class="numeric">2</span>] <span class="symbol">></span><span class="symbol">></span> <span class="numeric">8</span> * <span class="braces">(</span><span class="braces">(</span>i+<span class="numeric">2</span><span class="braces">)</span>%<span class="numeric">4</span><span class="braces">)</span><span class="braces">)</span> <span class="symbol">&</span> <span class="numeric">0xFF</span><span class="braces">)</span><span class="symbol">;</span>
  <span class="reserved_word">for</span><span class="braces">(</span><span class="reserved_word">var</span> j <span class="symbol">=</span> <span class="numeric">0</span><span class="symbol">;</span> j <span class="symbol"><</SPAN> <SPAN class=numeric>4</SPAN><SPAN class=symbol>;</SPAN> j++<SPAN class=braces>)</SPAN>
  <SPAN class=braces>{</SPAN>
   <SPAN class=reserved_word>if</SPAN><SPAN class=braces>(</SPAN>i * <SPAN class=numeric>8</SPAN> + j * <SPAN class=numeric>6</SPAN> <SPAN class=symbol>></span> binarray.<span class="predefined_identifier">length</span> * <span class="numeric">32</span><span class="braces">)</span> str +<span class="symbol">=</span> b64pad<span class="symbol">;</span>
   <span class="reserved_word">else</span> str +<span class="symbol">=</span> tab.<span class="library_function">charAt</span><span class="braces">(</span><span class="braces">(</span>triplet <span class="symbol">></span><span class="symbol">></span> <span class="numeric">6</span>*<span class="braces">(</span><span class="numeric">3</span>-j<span class="braces">)</span><span class="braces">)</span> <span class="symbol">&</span> <span class="numeric">0x3F</span><span class="braces">)</span><span class="symbol">;</span>
  <span class="braces">}</span>
 <span class="braces">}</span>
 <span class="reserved_word">return</span> str<span class="symbol">;</span>
<span class="braces">}</span>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template