From b350b565c7d538b8730bd2e499c30cde5f4ebdee Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sun, 29 May 2011 15:18:23 -0700 Subject: [PATCH] Copy the clip-it-good js libraries to the UI static files --- server/go/camlistored/ui/Crypto.js | 183 +++++++++++++++++++++++++ server/go/camlistored/ui/SHA1.js | 109 +++++++++++++++ server/go/camlistored/ui/base64.js | 206 +++++++++++++++++++++++++++++ 3 files changed, 498 insertions(+) create mode 100644 server/go/camlistored/ui/Crypto.js create mode 100644 server/go/camlistored/ui/SHA1.js create mode 100644 server/go/camlistored/ui/base64.js diff --git a/server/go/camlistored/ui/Crypto.js b/server/go/camlistored/ui/Crypto.js new file mode 100644 index 000000000..278bbfd8e --- /dev/null +++ b/server/go/camlistored/ui/Crypto.js @@ -0,0 +1,183 @@ +// From http://code.google.com/p/crypto-js/ +// License: http://www.opensource.org/licenses/bsd-license.php +// +// Copyright (c) 2009, Jeff Mott. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. Redistributions in binary +// form must reproduce the above copyright notice, this list of conditions and +// the following disclaimer in the documentation and/or other materials provided +// with the distribution. Neither the name Crypto-JS nor the names of its +// contributors may be used to endorse or promote products derived from this +// software without specific prior written permission. THIS SOFTWARE IS PROVIDED +// BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +// EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +if (typeof Crypto == "undefined" || ! Crypto.util) +{ +(function(){ + +var base64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +// Global Crypto object +var Crypto = window.Crypto = {}; + +// Crypto utilities +var util = Crypto.util = { + + // Bit-wise rotate left + rotl: function (n, b) { + return (n << b) | (n >>> (32 - b)); + }, + + // Bit-wise rotate right + rotr: function (n, b) { + return (n << (32 - b)) | (n >>> b); + }, + + // Swap big-endian to little-endian and vice versa + endian: function (n) { + + // If number given, swap endian + if (n.constructor == Number) { + return util.rotl(n, 8) & 0x00FF00FF | + util.rotl(n, 24) & 0xFF00FF00; + } + + // Else, assume array and swap all items + for (var i = 0; i < n.length; i++) + n[i] = util.endian(n[i]); + return n; + + }, + + // Generate an array of any length of random bytes + randomBytes: function (n) { + for (var bytes = []; n > 0; n--) + bytes.push(Math.floor(Math.random() * 256)); + return bytes; + }, + + // Convert a byte array to big-endian 32-bit words + bytesToWords: function (bytes) { + for (var words = [], i = 0, b = 0; i < bytes.length; i++, b += 8) + words[b >>> 5] |= bytes[i] << (24 - b % 32); + return words; + }, + + // Convert big-endian 32-bit words to a byte array + wordsToBytes: function (words) { + for (var bytes = [], b = 0; b < words.length * 32; b += 8) + bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF); + return bytes; + }, + + // Convert a byte array to a hex string + bytesToHex: function (bytes) { + for (var hex = [], i = 0; i < bytes.length; i++) { + hex.push((bytes[i] >>> 4).toString(16)); + hex.push((bytes[i] & 0xF).toString(16)); + } + return hex.join(""); + }, + + // Convert a hex string to a byte array + hexToBytes: function (hex) { + for (var bytes = [], c = 0; c < hex.length; c += 2) + bytes.push(parseInt(hex.substr(c, 2), 16)); + return bytes; + }, + + // Convert a byte array to a base-64 string + bytesToBase64: function (bytes) { + + // Use browser-native function if it exists + if (typeof btoa == "function") return btoa(Binary.bytesToString(bytes)); + + for(var base64 = [], i = 0; i < bytes.length; i += 3) { + var triplet = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]; + for (var j = 0; j < 4; j++) { + if (i * 8 + j * 6 <= bytes.length * 8) + base64.push(base64map.charAt((triplet >>> 6 * (3 - j)) & 0x3F)); + else base64.push("="); + } + } + + return base64.join(""); + + }, + + // Convert a base-64 string to a byte array + base64ToBytes: function (base64) { + + // Use browser-native function if it exists + if (typeof atob == "function") return Binary.stringToBytes(atob(base64)); + + // Remove non-base-64 characters + base64 = base64.replace(/[^A-Z0-9+\/]/ig, ""); + + for (var bytes = [], i = 0, imod4 = 0; i < base64.length; imod4 = ++i % 4) { + if (imod4 == 0) continue; + bytes.push(((base64map.indexOf(base64.charAt(i - 1)) & (Math.pow(2, -2 * imod4 + 8) - 1)) << (imod4 * 2)) | + (base64map.indexOf(base64.charAt(i)) >>> (6 - imod4 * 2))); + } + + return bytes; + + } + +}; + +// Crypto mode namespace +Crypto.mode = {}; + +// Crypto character encodings +var charenc = Crypto.charenc = {}; + +// UTF-8 encoding +var UTF8 = charenc.UTF8 = { + + // Convert a string to a byte array + stringToBytes: function (str) { + return Binary.stringToBytes(unescape(encodeURIComponent(str))); + }, + + // Convert a byte array to a string + bytesToString: function (bytes) { + return decodeURIComponent(escape(Binary.bytesToString(bytes))); + } + +}; + +// Binary encoding +var Binary = charenc.Binary = { + + // Convert a string to a byte array + stringToBytes: function (str) { + for (var bytes = [], i = 0; i < str.length; i++) + bytes.push(str.charCodeAt(i)); + return bytes; + }, + + // Convert a byte array to a string + bytesToString: function (bytes) { + for (var str = [], i = 0; i < bytes.length; i++) + str.push(String.fromCharCode(bytes[i])); + return str.join(""); + } + +}; + +})(); +} diff --git a/server/go/camlistored/ui/SHA1.js b/server/go/camlistored/ui/SHA1.js new file mode 100644 index 000000000..e9631dab2 --- /dev/null +++ b/server/go/camlistored/ui/SHA1.js @@ -0,0 +1,109 @@ +// From http://code.google.com/p/crypto-js/ +// License: http://www.opensource.org/licenses/bsd-license.php +// +// Copyright (c) 2009, Jeff Mott. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. Redistributions in binary +// form must reproduce the above copyright notice, this list of conditions and +// the following disclaimer in the documentation and/or other materials provided +// with the distribution. Neither the name Crypto-JS nor the names of its +// contributors may be used to endorse or promote products derived from this +// software without specific prior written permission. THIS SOFTWARE IS PROVIDED +// BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +// EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +(function(){ + +// Shortcuts +var C = Crypto, + util = C.util, + charenc = C.charenc, + UTF8 = charenc.UTF8, + Binary = charenc.Binary; + +// Public API +var SHA1 = C.SHA1 = function (message, options) { + var digestbytes = util.wordsToBytes(SHA1._sha1(message)); + return options && options.asBytes ? digestbytes : + options && options.asString ? Binary.bytesToString(digestbytes) : + util.bytesToHex(digestbytes); +}; + +// The core +SHA1._sha1 = function (message) { + + // Convert to byte array + if (message.constructor == String) message = UTF8.stringToBytes(message); + /* else, assume byte array already */ + + var m = util.bytesToWords(message), + l = message.length * 8, + w = [], + H0 = 1732584193, + H1 = -271733879, + H2 = -1732584194, + H3 = 271733878, + H4 = -1009589776; + + // Padding + m[l >> 5] |= 0x80 << (24 - l % 32); + m[((l + 64 >>> 9) << 4) + 15] = l; + + for (var i = 0; i < m.length; i += 16) { + + var a = H0, + b = H1, + c = H2, + d = H3, + e = H4; + + for (var j = 0; j < 80; j++) { + + if (j < 16) w[j] = m[i + j]; + else { + var n = w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16]; + w[j] = (n << 1) | (n >>> 31); + } + + var t = ((H0 << 5) | (H0 >>> 27)) + H4 + (w[j] >>> 0) + ( + j < 20 ? (H1 & H2 | ~H1 & H3) + 1518500249 : + j < 40 ? (H1 ^ H2 ^ H3) + 1859775393 : + j < 60 ? (H1 & H2 | H1 & H3 | H2 & H3) - 1894007588 : + (H1 ^ H2 ^ H3) - 899497514); + + H4 = H3; + H3 = H2; + H2 = (H1 << 30) | (H1 >>> 2); + H1 = H0; + H0 = t; + + } + + H0 += a; + H1 += b; + H2 += c; + H3 += d; + H4 += e; + + } + + return [H0, H1, H2, H3, H4]; + +}; + +// Package private blocksize +SHA1._blocksize = 16; + +})(); diff --git a/server/go/camlistored/ui/base64.js b/server/go/camlistored/ui/base64.js new file mode 100644 index 000000000..9d9a1f3a8 --- /dev/null +++ b/server/go/camlistored/ui/base64.js @@ -0,0 +1,206 @@ +/* +Copyright (c) 2008 Fred Palmer fred.palmer_at_gmail.com + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. +*/ +function StringBuffer() +{ + this.buffer = []; +} + +StringBuffer.prototype.append = function append(string) +{ + this.buffer.push(string); + return this; +}; + +StringBuffer.prototype.toString = function toString() +{ + return this.buffer.join(""); +}; + +var Base64 = +{ + codex : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", + + encode : function (input) + { + var output = new StringBuffer(); + + var enumerator = new Utf8EncodeEnumerator(input); + while (enumerator.moveNext()) + { + var chr1 = enumerator.current; + + enumerator.moveNext(); + var chr2 = enumerator.current; + + enumerator.moveNext(); + var chr3 = enumerator.current; + + var enc1 = chr1 >> 2; + var enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); + var enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); + var enc4 = chr3 & 63; + + if (isNaN(chr2)) + { + enc3 = enc4 = 64; + } + else if (isNaN(chr3)) + { + enc4 = 64; + } + + output.append(this.codex.charAt(enc1) + this.codex.charAt(enc2) + this.codex.charAt(enc3) + this.codex.charAt(enc4)); + } + + return output.toString(); + }, + + decode : function (input) + { + // TypedArray usage added by brett@haxor.com 11/27/2010 + var size = 0; + var buffer = new ArrayBuffer(input.length); + var output = new Uint8Array(buffer, 0); + + var enumerator = new Base64DecodeEnumerator(input); + while (enumerator.moveNext()) { + output[size++] = enumerator.current; + } + + // There is nothing in the TypedArray spec to copy/subset a buffer, + // so we have to do a copy to ensure that typedarray.buffer is the + // correct length when passed to XmlHttpRequest methods, etc. + var outputBuffer = new ArrayBuffer(size); + var outputArray = new Uint8Array(outputBuffer, 0); + for (var i = 0; i < size; i++) { + outputArray[i] = output[i]; + } + return outputArray; + } +} + + +function Utf8EncodeEnumerator(input) +{ + this._input = input; + this._index = -1; + this._buffer = []; +} + +Utf8EncodeEnumerator.prototype = +{ + current: Number.NaN, + + moveNext: function() + { + if (this._buffer.length > 0) + { + this.current = this._buffer.shift(); + return true; + } + else if (this._index >= (this._input.length - 1)) + { + this.current = Number.NaN; + return false; + } + else + { + var charCode = this._input.charCodeAt(++this._index); + + // "\r\n" -> "\n" + // + if ((charCode == 13) && (this._input.charCodeAt(this._index + 1) == 10)) + { + charCode = 10; + this._index += 2; + } + + if (charCode < 128) + { + this.current = charCode; + } + else if ((charCode > 127) && (charCode < 2048)) + { + this.current = (charCode >> 6) | 192; + this._buffer.push((charCode & 63) | 128); + } + else + { + this.current = (charCode >> 12) | 224; + this._buffer.push(((charCode >> 6) & 63) | 128); + this._buffer.push((charCode & 63) | 128); + } + + return true; + } + } +} + +function Base64DecodeEnumerator(input) +{ + this._input = input; + this._index = -1; + this._buffer = []; +} + +Base64DecodeEnumerator.prototype = +{ + current: 64, + + moveNext: function() + { + if (this._buffer.length > 0) + { + this.current = this._buffer.shift(); + return true; + } + else if (this._index >= (this._input.length - 1)) + { + this.current = 64; + return false; + } + else + { + var enc1 = Base64.codex.indexOf(this._input.charAt(++this._index)); + var enc2 = Base64.codex.indexOf(this._input.charAt(++this._index)); + var enc3 = Base64.codex.indexOf(this._input.charAt(++this._index)); + var enc4 = Base64.codex.indexOf(this._input.charAt(++this._index)); + + var chr1 = (enc1 << 2) | (enc2 >> 4); + var chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); + var chr3 = ((enc3 & 3) << 6) | enc4; + + this.current = chr1; + + if (enc3 != 64) + this._buffer.push(chr2); + + if (enc4 != 64) + this._buffer.push(chr3); + + return true; + } + } +};