Heim >Web-Frontend >js-Tutorial >Javascript SHA-1:Secure Hash Algorithm_基础知识

Javascript SHA-1:Secure Hash Algorithm_基础知识

WBOY
WBOYOriginal
2016-05-16 19:23:101178Durchsuche
From:http://pajhome.org.uk/crypt/md5/sha1src.html
<span class="comment">/*
 * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
 * in FIPS PUB 180-1
 * Version 2.1a Copyright Paul Johnston 2000 - 2002.
 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 * Distributed under the BSD License
 * See http://pajhome.org.uk/crypt/md5 for details.
 */</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
 * They take string arguments and return either hex or base-64 encoded strings
 */</span>
<span class="reserved_word">function</span> hex_sha1<span class="braces">(</span>s<span class="braces">)</span><span class="braces">{</span><span class="reserved_word">return</span> binb2hex<span class="braces">(</span>core_sha1<span class="braces">(</span>str2binb<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_sha1<span class="braces">(</span>s<span class="braces">)</span><span class="braces">{</span><span class="reserved_word">return</span> binb2b64<span class="braces">(</span>core_sha1<span class="braces">(</span>str2binb<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_sha1<span class="braces">(</span>s<span class="braces">)</span><span class="braces">{</span><span class="reserved_word">return</span> binb2str<span class="braces">(</span>core_sha1<span class="braces">(</span>str2binb<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_sha1<span class="braces">(</span>key, data<span class="braces">)</span><span class="braces">{</span> <span class="reserved_word">return</span> binb2hex<span class="braces">(</span>core_hmac_sha1<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_sha1<span class="braces">(</span>key, data<span class="braces">)</span><span class="braces">{</span> <span class="reserved_word">return</span> binb2b64<span class="braces">(</span>core_hmac_sha1<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_sha1<span class="braces">(</span>key, data<span class="braces">)</span><span class="braces">{</span> <span class="reserved_word">return</span> binb2str<span class="braces">(</span>core_hmac_sha1<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> sha1_vm_test<span class="braces">(</span><span class="braces">)</span>
<span class="braces">{</span>
 <span class="reserved_word">return</span> hex_sha1<span class="braces">(</span><span class="string">"abc"</span><span class="braces">)</span> <span class="symbol">=</span><span class="symbol">=</span> <span class="string">"a9993e364706816aba3e25717850c26c9cd0d89d"</span><span class="symbol">;</span>
<span class="braces">}</span>

<span class="comment">/*
 * Calculate the SHA-1 of an array of big-endian words, and a bit length
 */</span>
<span class="reserved_word">function</span> core_sha1<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><span class="numeric">24</span> - len % <span class="numeric">32</span><span class="braces">)</span><span class="symbol">;</span>
 x[<span class="braces">(</span><span class="braces">(</span>len + <span class="numeric">64</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">15</span>] <span class="symbol">=</span> len<span class="symbol">;</span>

 <span class="reserved_word">var</span> w <span class="symbol">=</span> <span class="predefined_type">Array</span><span class="braces">(</span><span class="numeric">80</span><span class="braces">)</span><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">var</span> e <span class="symbol">=</span> -<span class="numeric">1009589776</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>
  <span class="reserved_word">var</span> olde <span class="symbol">=</span> e<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">80</span><span class="symbol">;</span> j++<span class="braces">)</span>
  <span class="braces">{</span>
   <span class="reserved_word">if</span><span class="braces">(</span>j <span class="symbol"> <span class="numeric">16</span><span class="braces">)</span> w[j] <span class="symbol">=</span> x[i + j]<span class="symbol">;</span>
   <span class="reserved_word">else</span> w[j] <span class="symbol">=</span> rol<span class="braces">(</span>w[j-<span class="numeric">3</span>] ^ w[j-<span class="numeric">8</span>] ^ w[j-<span class="numeric">14</span>] ^ w[j-<span class="numeric">16</span>], <span class="numeric">1</span><span class="braces">)</span><span class="symbol">;</span>
   <span class="reserved_word">var</span> t <span class="symbol">=</span> safe_add<span class="braces">(</span>safe_add<span class="braces">(</span>rol<span class="braces">(</span>a, <span class="numeric">5</span><span class="braces">)</span>, sha1_ft<span class="braces">(</span>j, b, c, d<span class="braces">)</span><span class="braces">)</span>,
            safe_add<span class="braces">(</span>safe_add<span class="braces">(</span>e, w[j]<span class="braces">)</span>, sha1_kt<span class="braces">(</span>j<span class="braces">)</span><span class="braces">)</span><span class="braces">)</span><span class="symbol">;</span>
   e <span class="symbol">=</span> d<span class="symbol">;</span>
   d <span class="symbol">=</span> c<span class="symbol">;</span>
   c <span class="symbol">=</span> rol<span class="braces">(</span>b, <span class="numeric">30</span><span class="braces">)</span><span class="symbol">;</span>
   b <span class="symbol">=</span> a<span class="symbol">;</span>
   a <span class="symbol">=</span> t<span class="symbol">;</span>
  <span class="braces">}</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>
  e <span class="symbol">=</span> safe_add<span class="braces">(</span>e, olde<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, e<span class="braces">)</span><span class="symbol">;</span>

<span class="braces">}</span>

<span class="comment">/*
 * Perform the appropriate triplet combination function for the current
 * iteration
 */</span>
<span class="reserved_word">function</span> sha1_ft<span class="braces">(</span>t, b, c, d<span class="braces">)</span>
<span class="braces">{</span>
 <span class="reserved_word">if</span><span class="braces">(</span>t <span class="symbol"> <span class="numeric">20</span><span class="braces">)</span> <span class="reserved_word">return</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><span class="symbol">;</span>
 <span class="reserved_word">if</span><span class="braces">(</span>t <span class="symbol"> <span class="numeric">40</span><span class="braces">)</span> <span class="reserved_word">return</span> b ^ c ^ d<span class="symbol">;</span>
 <span class="reserved_word">if</span><span class="braces">(</span>t <span class="symbol"> <span class="numeric">60</span><span class="braces">)</span> <span class="reserved_word">return</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><span class="symbol">;</span>
 <span class="reserved_word">return</span> b ^ c ^ d<span class="symbol">;</span>
<span class="braces">}</span>

<span class="comment">/*
 * Determine the appropriate additive constant for the current iteration
 */</span>
<span class="reserved_word">function</span> sha1_kt<span class="braces">(</span>t<span class="braces">)</span>
<span class="braces">{</span>
 <span class="reserved_word">return</span> <span class="braces">(</span>t <span class="symbol"> <span class="numeric">20</span><span class="braces">)</span> ? <span class="numeric">1518500249</span> : <span class="braces">(</span>t <span class="symbol"> <span class="numeric">40</span><span class="braces">)</span> ? <span class="numeric">1859775393</span> :
     <span class="braces">(</span>t <span class="symbol"> <span class="numeric">60</span><span class="braces">)</span> ? -<span class="numeric">1894007588</span> : -<span class="numeric">899497514</span><span class="symbol">;</span>
<span class="braces">}</span>

<span class="comment">/*
 * Calculate the HMAC-SHA1 of a key and some data
 */</span>
<span class="reserved_word">function</span> core_hmac_sha1<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> str2binb<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_sha1<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_sha1<span class="braces">(</span>ipad.concat<span class="braces">(</span>str2binb<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_sha1<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">160</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 an 8-bit or 16-bit string to an array of big-endian words
 * In 8-bit function, characters >255 have their hi-byte silently ignored.
 */</span>
<span class="reserved_word">function</span> str2binb<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><span class="numeric">32</span> - chrsz - 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 big-endian words to a string
 */</span>
<span class="reserved_word">function</span> binb2str<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><span class="numeric">32</span> - chrsz - 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 big-endian words to a hex string.
 */</span>
<span class="reserved_word">function</span> binb2hex<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><span class="numeric">3</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><span class="numeric">3</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 big-endian words to a base-64 string
 */</span>
<span class="reserved_word">function</span> binb2b64<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><span class="numeric">3</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="numeric">3</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="numeric">3</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></span></span></span></span></span></span></span></span>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn