github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/clients/chrome/clip-it-good/SHA1.js (about)

     1  // From http://code.google.com/p/crypto-js/
     2  // License: http://www.opensource.org/licenses/bsd-license.php
     3  //
     4  // Copyright (c) 2009, Jeff Mott. All rights reserved.
     5  // 
     6  // Redistribution and use in source and binary forms, with or without
     7  // modification, are permitted provided that the following conditions are met:
     8  // 
     9  // Redistributions of source code must retain the above copyright notice, this
    10  // list of conditions and the following disclaimer. Redistributions in binary
    11  // form must reproduce the above copyright notice, this list of conditions and
    12  // the following disclaimer in the documentation and/or other materials provided
    13  // with the distribution. Neither the name Crypto-JS nor the names of its
    14  // contributors may be used to endorse or promote products derived from this
    15  // software without specific prior written permission. THIS SOFTWARE IS PROVIDED
    16  // BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
    17  // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    18  // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
    19  // EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    20  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    21  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    22  // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    23  // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    24  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    25  // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    26  
    27  (function(){
    28  
    29  // Shortcuts
    30  var C = Crypto,
    31      util = C.util,
    32      charenc = C.charenc,
    33      UTF8 = charenc.UTF8,
    34      Binary = charenc.Binary;
    35  
    36  // Public API
    37  var SHA1 = C.SHA1 = function (message, options) {
    38  	var digestbytes = util.wordsToBytes(SHA1._sha1(message));
    39  	return options && options.asBytes ? digestbytes :
    40  	       options && options.asString ? Binary.bytesToString(digestbytes) :
    41  	       util.bytesToHex(digestbytes);
    42  };
    43  
    44  // The core
    45  SHA1._sha1 = function (message) {
    46  
    47  	// Convert to byte array
    48  	if (message.constructor == String) message = UTF8.stringToBytes(message);
    49  	/* else, assume byte array already */
    50  
    51  	var m  = util.bytesToWords(message),
    52  	    l  = message.length * 8,
    53  	    w  =  [],
    54  	    H0 =  1732584193,
    55  	    H1 = -271733879,
    56  	    H2 = -1732584194,
    57  	    H3 =  271733878,
    58  	    H4 = -1009589776;
    59  
    60  	// Padding
    61  	m[l >> 5] |= 0x80 << (24 - l % 32);
    62  	m[((l + 64 >>> 9) << 4) + 15] = l;
    63  
    64  	for (var i = 0; i < m.length; i += 16) {
    65  
    66  		var a = H0,
    67  		    b = H1,
    68  		    c = H2,
    69  		    d = H3,
    70  		    e = H4;
    71  
    72  		for (var j = 0; j < 80; j++) {
    73  
    74  			if (j < 16) w[j] = m[i + j];
    75  			else {
    76  				var n = w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16];
    77  				w[j] = (n << 1) | (n >>> 31);
    78  			}
    79  
    80  			var t = ((H0 << 5) | (H0 >>> 27)) + H4 + (w[j] >>> 0) + (
    81  			         j < 20 ? (H1 & H2 | ~H1 & H3) + 1518500249 :
    82  			         j < 40 ? (H1 ^ H2 ^ H3) + 1859775393 :
    83  			         j < 60 ? (H1 & H2 | H1 & H3 | H2 & H3) - 1894007588 :
    84  			                  (H1 ^ H2 ^ H3) - 899497514);
    85  
    86  			H4 =  H3;
    87  			H3 =  H2;
    88  			H2 = (H1 << 30) | (H1 >>> 2);
    89  			H1 =  H0;
    90  			H0 =  t;
    91  
    92  		}
    93  
    94  		H0 += a;
    95  		H1 += b;
    96  		H2 += c;
    97  		H3 += d;
    98  		H4 += e;
    99  
   100  	}
   101  
   102  	return [H0, H1, H2, H3, H4];
   103  
   104  };
   105  
   106  // Package private blocksize
   107  SHA1._blocksize = 16;
   108  
   109  })();