github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/clients/chrome/clip-it-good/Crypto.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  if (typeof Crypto == "undefined" || ! Crypto.util)
    28  {
    29  (function(){
    30  
    31  var base64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    32  
    33  // Global Crypto object
    34  var Crypto = window.Crypto = {};
    35  
    36  // Crypto utilities
    37  var util = Crypto.util = {
    38  
    39  	// Bit-wise rotate left
    40  	rotl: function (n, b) {
    41  		return (n << b) | (n >>> (32 - b));
    42  	},
    43  
    44  	// Bit-wise rotate right
    45  	rotr: function (n, b) {
    46  		return (n << (32 - b)) | (n >>> b);
    47  	},
    48  
    49  	// Swap big-endian to little-endian and vice versa
    50  	endian: function (n) {
    51  
    52  		// If number given, swap endian
    53  		if (n.constructor == Number) {
    54  			return util.rotl(n,  8) & 0x00FF00FF |
    55  			       util.rotl(n, 24) & 0xFF00FF00;
    56  		}
    57  
    58  		// Else, assume array and swap all items
    59  		for (var i = 0; i < n.length; i++)
    60  			n[i] = util.endian(n[i]);
    61  		return n;
    62  
    63  	},
    64  
    65  	// Generate an array of any length of random bytes
    66  	randomBytes: function (n) {
    67  		for (var bytes = []; n > 0; n--)
    68  			bytes.push(Math.floor(Math.random() * 256));
    69  		return bytes;
    70  	},
    71  
    72  	// Convert a byte array to big-endian 32-bit words
    73  	bytesToWords: function (bytes) {
    74  		for (var words = [], i = 0, b = 0; i < bytes.length; i++, b += 8)
    75  			words[b >>> 5] |= bytes[i] << (24 - b % 32);
    76  		return words;
    77  	},
    78  
    79  	// Convert big-endian 32-bit words to a byte array
    80  	wordsToBytes: function (words) {
    81  		for (var bytes = [], b = 0; b < words.length * 32; b += 8)
    82  			bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF);
    83  		return bytes;
    84  	},
    85  
    86  	// Convert a byte array to a hex string
    87  	bytesToHex: function (bytes) {
    88  		for (var hex = [], i = 0; i < bytes.length; i++) {
    89  			hex.push((bytes[i] >>> 4).toString(16));
    90  			hex.push((bytes[i] & 0xF).toString(16));
    91  		}
    92  		return hex.join("");
    93  	},
    94  
    95  	// Convert a hex string to a byte array
    96  	hexToBytes: function (hex) {
    97  		for (var bytes = [], c = 0; c < hex.length; c += 2)
    98  			bytes.push(parseInt(hex.substr(c, 2), 16));
    99  		return bytes;
   100  	},
   101  
   102  	// Convert a byte array to a base-64 string
   103  	bytesToBase64: function (bytes) {
   104  
   105  		// Use browser-native function if it exists
   106  		if (typeof btoa == "function") return btoa(Binary.bytesToString(bytes));
   107  
   108  		for(var base64 = [], i = 0; i < bytes.length; i += 3) {
   109  			var triplet = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
   110  			for (var j = 0; j < 4; j++) {
   111  				if (i * 8 + j * 6 <= bytes.length * 8)
   112  					base64.push(base64map.charAt((triplet >>> 6 * (3 - j)) & 0x3F));
   113  				else base64.push("=");
   114  			}
   115  		}
   116  
   117  		return base64.join("");
   118  
   119  	},
   120  
   121  	// Convert a base-64 string to a byte array
   122  	base64ToBytes: function (base64) {
   123  
   124  		// Use browser-native function if it exists
   125  		if (typeof atob == "function") return Binary.stringToBytes(atob(base64));
   126  
   127  		// Remove non-base-64 characters
   128  		base64 = base64.replace(/[^A-Z0-9+\/]/ig, "");
   129  
   130  		for (var bytes = [], i = 0, imod4 = 0; i < base64.length; imod4 = ++i % 4) {
   131  			if (imod4 == 0) continue;
   132  			bytes.push(((base64map.indexOf(base64.charAt(i - 1)) & (Math.pow(2, -2 * imod4 + 8) - 1)) << (imod4 * 2)) |
   133  			           (base64map.indexOf(base64.charAt(i)) >>> (6 - imod4 * 2)));
   134  		}
   135  
   136  		return bytes;
   137  
   138  	}
   139  
   140  };
   141  
   142  // Crypto mode namespace
   143  Crypto.mode = {};
   144  
   145  // Crypto character encodings
   146  var charenc = Crypto.charenc = {};
   147  
   148  // UTF-8 encoding
   149  var UTF8 = charenc.UTF8 = {
   150  
   151  	// Convert a string to a byte array
   152  	stringToBytes: function (str) {
   153  		return Binary.stringToBytes(unescape(encodeURIComponent(str)));
   154  	},
   155  
   156  	// Convert a byte array to a string
   157  	bytesToString: function (bytes) {
   158  		return decodeURIComponent(escape(Binary.bytesToString(bytes)));
   159  	}
   160  
   161  };
   162  
   163  // Binary encoding
   164  var Binary = charenc.Binary = {
   165  
   166  	// Convert a string to a byte array
   167  	stringToBytes: function (str) {
   168  		for (var bytes = [], i = 0; i < str.length; i++)
   169  			bytes.push(str.charCodeAt(i));
   170  		return bytes;
   171  	},
   172  
   173  	// Convert a byte array to a string
   174  	bytesToString: function (bytes) {
   175  		for (var str = [], i = 0; i < bytes.length; i++)
   176  			str.push(String.fromCharCode(bytes[i]));
   177  		return str.join("");
   178  	}
   179  
   180  };
   181  
   182  })();
   183  }