搜索
首页web前端js教程Javascript MD4_基础知识

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 class="symbol"> <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 class="symbol"> <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"> 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 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 class="symbol"> <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 class="symbol"> 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 class="symbol"> 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"> 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 class="symbol"> <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 class="symbol"> 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"> 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"> 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"> 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 class="symbol"> <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 class="symbol"> <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 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></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

20+道必知必会的Vue面试题(附答案解析)20+道必知必会的Vue面试题(附答案解析)Apr 06, 2021 am 09:41 AM

本篇文章整理了20+Vue面试题分享给大家,同时附上答案解析。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
1 个月前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用