From 26c4553a31bd94678c49749eddc89b565ecfb0eb Mon Sep 17 00:00:00 2001 From: tim b Date: Sun, 26 Sep 2010 20:20:46 -0700 Subject: integrated new search style, image cache, compressed sha1 code --- static/js/pichat.js | 834 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 486 insertions(+), 348 deletions(-) (limited to 'static/js/pichat.js') diff --git a/static/js/pichat.js b/static/js/pichat.js index 0ee71bc..7734aa7 100644 --- a/static/js/pichat.js +++ b/static/js/pichat.js @@ -33,6 +33,12 @@ Anim = { // Utils +EmptyFunction = function(){}; + +isImgBroken = function(img){ + return (img.height == 0 || img.width == 0 || img.height == NaN || img.width == NaN) +} + isEmptyObject = function(obj) { for (key in obj) { if (obj.hasOwnProperty(key)) return false; @@ -132,6 +138,7 @@ function linkify(text) { return text } +// use this in escapeHtml to turn everyone's text lIkE tHiS function annoyingCaps(text){ var chunks = text.split(" ") for(var i=0; i") - }, - 'setContent': function(x){ - $("#search-results-images").html(x) - }, - 'setMessage': function(x){ - $("#search-controls").css("display", "block") - $("#search-control-text").html(x) - }, - 'searchError': function(error){ - Search.setContent("") - $('#search-control-previous').css("visibility", "hidden") - $('#search-control-next').css("visibility", "hidden") - Search.setMessage(error) - }, - - 'doSearch': function(){ - term = $("#search-query").val().trim().toLowerCase() - var rawTokens = term.split(" ") - Search.tokens = [] - for(var t = 0; t < rawTokens.length; t++) { - if (rawTokens[t].length > 2) - Search.tokens.push(rawTokens[t]) - } - if (Search.tokens.length == 0) { - Search.setMessage("search query too small") - } else { - Search.setMessage("searching for '"+Search.tokens.join(" and ")+"'") - Search.addScript(Search.tokens.join("+")) - } - }, - - 'renderPage': function(num){ - $("#search-results-images").css("display", "block") - $("#search-controls").css("display", "block") - if (Search.images.length > 0) - Search.setMessage("results for '"+Search.tokens.join(" and ")+"' (page " + (num + 1) + ")"); - var contentString = '' - var start = num * Search.imagesPerPage - var imageCounter = 0 - for(var i = start; i < Search.images.length; i++){ - if(imageCounter > Search.imagesPerPage) break; - contentString += '' - imageCounter += 1 - } - contentString += '

' - if(num > 0) { - $('#search-control-previous').attr("href", 'javascript:Search.renderPage('+(num-1)+')') - $('#search-control-previous').css("visibility", "visible") - } else { - $('#search-control-previous').attr("href", 'javascript:void()') - $('#search-control-previous').css("visibility", "hidden") - } - - if (Search.images.length > start + imageCounter) { - $('#search-control-next').attr("href", 'javascript:Search.renderPage('+(num+1)+')') - $('#search-control-next').css("visibility", "visible") - } else { - $('#search-control-next').attr("href", 'javascript:void()') - $('#search-control-next').css("visibility", "hidden") - } - Search.setContent(contentString) - }, - - 'click': function(e){ - if (e.which == 1) // left click - if (Search.addToChatBoxIfPossible(this)) - window.open(this.href) - else if (e.which == 2) // middle click - window.open(this.href) - }, - - 'addToChatBoxIfPossible': function(img){ - var chatBoxExists = $("#msgInput").length - if (chatBoxExists) { - var chatText = $("#msgInput").val() - if (chatText.length && chatText[chatText.length - 1] != " ") - chatText += " " - chatText += $(img).attr("href") + " " - $("#msgInput").val(chatText) - $("#msgInput").focus().val($("#msgInput").val()) //http://stackoverflow.com/questions/1056359/ - return false - } else return true - }, - - 'searchResult': function(results){ - Search.images = [] - if(results === null || results.length == 0) { - Search.setContent("") - Search.setMessage("no results found") - } else { - for(var r = 0; r nodes indexed by url + "caches": {}, + + "init": function(name){ + // don't clear callback + var callback = EmptyFunction + if (name in ImgCache.caches) + callback = ImgCache.caches[name].onImgsLoaded + delete ImgCache.caches[name] + + ImgCache.caches[name] = { + "loadAtATime": 10, + "urlsToLoad": [], + "imgsLoading": {}, + "imgsLoadingCounter" : 0, // a hack so i don't have to iterate over the object to always get its size... + "imgsLoaded": {}, + "onImgsLoaded": callback, + "paused": false + } + }, + + "add": function(name, urls){ + if (!(name in ImgCache.caches)) ImgCache.init(name) + if (!$.isArray(urls)) urls = [urls]; + + var cache = ImgCache.caches[name] + + urls.forEach(function(url){ + cache.urlsToLoad.push(url) + }) + + }, + + "config": function(name, cfg){ + if (!(name in ImgCache.caches)) ImgCache.init(name) + var cache = ImgCache.caches[name] + for(var key in cfg) + cache[key] = cfg[key] + }, + + "clear": function(name){ + ImgCache.init(name) + }, + + "loadImages": function(cache){ + if (cache.paused) return; + + while(cache.urlsToLoad.length && cache.imgsLoadingCounter < cache.loadAtATime) { + var url = cache.urlsToLoad.shift() + if (url in ImgCache.imgs) { // already loading this image + var img = ImgCache.imgs[url] + if (img.complete) { + cache.imgsLoaded[url] = ImgCache.imgs[url] + } else if (!(url in cache.imgsLoading)) { + cache.imgsLoading[url] = ImgCache.imgs[url] + cache.imgsLoadingCounter += 1 + } + } else { + var img = new Image() + img.src = url + img.animated = (parseUri(url)["file"].toLowerCase().substr(-3) == "gif") ? true : false; + ImgCache.imgs[url] = img + cache.imgsLoading[url] = img + cache.imgsLoadingCounter += 1 + } + } + }, + "processLoadingImages": function(cache){ + for (var url in cache.imgsLoading) { + var img = cache.imgsLoading[url] + if (img.complete) { + cache.imgsLoaded[url] = img + delete cache.imgsLoading[url] + cache.imgsLoadingCounter -= 1 + } + } + }, + + "loader": function(){ + for (name in ImgCache.caches){ + var cache = ImgCache.caches[name] + ImgCache.processLoadingImages(cache) // move images from imgsLoading into imgsLoaded + ImgCache.loadImages(cache) // put new images in imgsLoading/imgsLoaded from urlsToLoad + for (var url in cache.imgsLoaded) { + cache.onImgsLoaded(cache.imgsLoaded) // only call if new images actually loaded + delete cache.imgsLoaded + cache.imgsLoaded = {} + break; + } + + } + setTimeout(ImgCache.loader, 500) + } +} + +ImgCache.loader() + + +var Search = { + + 'term': "", + 'images': [], + 'tokens': [], + 'closed': true, + + 'initFullpage': function(){ + Search.init() + Search.initSpaceFill = Search.initSpaceFillFullpage + }, + + 'initInpage': function(){ + Search.init() + Search.initSpaceFill = Search.initSpaceFillInpage + }, + + 'init': function(){ + ImgCache.config("search", {"onImgsLoaded": Search.imgsLoaded}) + + $('#search-results-images a').live('hover', Search.resultsHover) + + var input = Search.$input = $("#search-query") + var label = "search dump.fm" + Search.$container = $("#search-results-images") + + input.val(label) + input.focus(function(){ + if (input.val() == label) input.val("") + }) + input.blur(function(){ + if (input.val().trim() == '') input.val(label) + }) + input.keydown(ifEnter(Search.doSearch)) + $("#search-results-images a").live("mouseup", Search.click) + }, + + 'initSpaceFillFullpage': function() { + SpaceFill.init({ + "container": "#search-results-images", + "width": $(document).width(), + "height": $(document).height(), + "type": "columns", + "spacing": "justify", + "minMargin": 16, + "columnWidth": 250 + }) + }, + + 'initSpaceFillInpage': function() { + SpaceFill.init({ + "container": "#search-results-images", + "width": $(document).width() * 0.93, + "height": $(document).height(), + "type": "columns", + "spacing": "justify", + "minMargin": 16, + "columnWidth": 120 + }) + }, + + "resultsHover": function(e){ + if (e.type == 'mouseover') { + var img = ImgCache.imgs[this.href] + if (img.animated) { + img.width = img.adjWidth + img.height = img.adjHeight + $(this).addClass("animating") + $(this).append(img) + } + } else { + var img = ImgCache.imgs[this.href] + if (img.animated) { + $(this).removeClass("animating") + this.removeChild(img) + } + } + }, + + "imgsLoaded": function(imgs){ + //if (ColumnFill.isSpaceFilled()) return; + + if (Search.closed) return; + + if (Search.$container[0].style.display != "block") { + Search.$container.css("display", "block") + Search.setMessage("results for '"+Search.tokens.join(" and ")+"'") + } + + for (var url in imgs){ + var img = imgs[url] + if (isImgBroken(img)) continue; + var width = img.width + var height = img.height + + if (width > SpaceFill.config.columnWidth) { + scaleFactor = SpaceFill.config.columnWidth / width + width = SpaceFill.config.columnWidth + height = Math.floor(height * scaleFactor) + } + img.adjWidth = width + img.adjHeight = height + + var c = document.createElement("canvas") + c.width = width + c.height = height + var ctx = c.getContext('2d'); + ctx.drawImage(img, 0, 0, c.width, c.height) + + var a = document.createElement("a") + a.href = img.src + a.style.width = width + "px" + a.style.height = height + "px" + + a.appendChild(c) + + SpaceFill.add(a) + } + }, + + 'setContent': function(x){ + $("#search-results-images").html(x) + }, + + 'setMessage': function(x){ + $("#search-controls").css("display", "block") + $("#search-message").html(x) + }, + + 'searchError': function(error){ + Search.setContent("") + Search.setMessage(error) + }, + + 'doSearch': function(){ + Search.closed = false + term = $("#search-query").val().trim().toLowerCase() + var rawTokens = term.split(" ") + Search.tokens = [] + rawTokens.forEach(function(t){ if (t.length > 2) Search.tokens.push(t) }) + if (Search.tokens.length == 0) { + Search.setMessage("search query too small") + } else { + Search.setMessage("searching for '"+Search.tokens.join(" and ")+"'") + Search.doAjax(Search.tokens.join("+")) + } + }, + + 'doAjax': function(term) { + if (Domain == "http://dump.fm") { + $.ajax({ + "url": "/cmd/search/" + term, + "success": Search.results, + "error": Search.error, + "timeout": 20000, + }) + } else { // search main site via jsonp + $("#search-script").remove() + $("head").append("") + } + }, + + 'click': function(e){ + if (e.which == 1) // left click + if (Search.addToChatBoxIfPossible(this)) + window.open(this.href) + else if (e.which == 2) // middle click + window.open(this.href) + }, + + 'addToChatBoxIfPossible': function(img){ + var chatBoxExists = $("#msgInput").length + if (chatBoxExists) { + var chatText = $("#msgInput").val() + if (chatText.length && chatText[chatText.length - 1] != " ") + chatText += " " + chatText += $(img).attr("href") + " " + $("#msgInput").val(chatText) + $("#msgInput").focus().val($("#msgInput").val()) //http://stackoverflow.com/questions/1056359/ + return false + } else return true + }, + // 'results' + 'searchResult': function(results){ + Search.resultsClear() + if(results === null || results.length == 0) { + Search.setMessage("no results found") + } else { + Search.initSpaceFill() + var urls = [] + results.forEach(function(r){ urls.push(r.url) }) + ImgCache.add("search", urls) + } + }, + + 'resultsClear': function(){ + $("#search-results-images").html("") + ImgCache.clear("search") + }, + + 'close': function(){ + Search.resultsClear() + Search.closed = true + Search.$container.css("display", "none") + $("#search-controls").css("display", "none") + } + +} + +var ColumnFill = { + "init": function(){ + var cfg = SpaceFill.config + var numColumns = ColumnFill.calcColumns() + ColumnFill.columns = [] + for (var i = 0; i < numColumns; i++) { + ColumnFill.columns.push({"height": 0}) + } + + cfg.marginWidth = cfg.marginHeight = cfg.minMargin + + if (cfg.spacing == "center") { + cfg.columnSpacingAmt = (cfg.width - (numColumns * (cfg.columnWidth + cfg.marginWidth) + cfg.marginWidth)) / 2 + } else if (cfg.spacing == "justify") { + cfg.marginWidth = (cfg.width - (numColumns * cfg.columnWidth)) / (numColumns + 1) + } + + + }, + "add": function(obj){ + var cfg = SpaceFill.config + var colIndex = ColumnFill.shortestColumn() + var col = ColumnFill.columns[colIndex] + + if (cfg.spacing == "center") { + var colLeft = colIndex * (cfg.marginWidth + cfg.columnWidth) + cfg.columnSpacingAmt + var imgLeft = Math.floor((cfg.marginWidth / 2) + (cfg.columnWidth / 2) - (parseInt(obj.style.width) / 2)) + colLeft + "px" + } else if (cfg.spacing == "justify") { + var colLeft = (colIndex * (cfg.marginWidth + cfg.columnWidth)) + var imgLeft = Math.floor((cfg.marginWidth / 2) + (cfg.columnWidth / 2) - (parseInt(obj.style.width) / 2)) + colLeft + "px" + } + + obj.style.position = 'absolute' + obj.style.top = col.height + cfg.marginHeight + "px" + obj.style.left = imgLeft + + col.height += cfg.marginHeight + parseInt(obj.style.height) + + $(cfg.container).append(obj) + + }, + "calcColumns": function(){ + var cfg = SpaceFill.config + var numColumns = 0 + var width = cfg.width - cfg.minMargin + var columnSub = cfg.columnWidth + cfg.minMargin + while (width > columnSub) { + width -= columnSub + numColumns++ + } + return numColumns + }, + + "shortestColumn": function(){ + var min = Infinity + var mindex = 0 + for(var i = 0; i< ColumnFill.columns.length; i++){ + var col = ColumnFill.columns[i] + if ( min > col.height) { + min = col.height + mindex = i + } + } + return mindex + }, + + "isSpaceFilled": function(){ + var config = SpaceFill.config + var colIndex = ColumnFill.shortestColumn() + var col = ColumnFill.columns[colIndex] + + if (col.height > 4 * config.height) return true; + else return false; + } + +} + +var SpaceFill = { + "init": function(config){ + config.type = "columns" + SpaceFill.config = config + + SpaceFill.types[config.type].init() + SpaceFill.add = SpaceFill.types[config.type].add + }, + "types": { "columns": ColumnFill } +} // uhhh todo: move preload stuff into js: // var nextImage = new Image(); @@ -1367,142 +1644,73 @@ function initChatMsgs() { }); } -// sha1.js -/* - * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined - * in FIPS 180-1 +/* SHA1.js (timb: compressed this) * Version 2.2 Copyright Paul Johnston 2000 - 2009. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet * Distributed under the BSD License - * See http://pajhome.org.uk/crypt/md5 for details. + * from http://pajhome.org.uk/crypt/md5/sha1.html */ - -/* - * Configurable variables. You may need to tweak these to be compatible with - * the server-side, but the defaults work in most cases. - */ -var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */ -var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */ - -/* - * These are the functions you'll usually want to call - * They take string arguments and return either hex or base-64 encoded strings - */ -function hex_sha1(s) { return rstr2hex(rstr_sha1(str2rstr_utf8(s))); } -function b64_sha1(s) { return rstr2b64(rstr_sha1(str2rstr_utf8(s))); } -function any_sha1(s, e) { return rstr2any(rstr_sha1(str2rstr_utf8(s)), e); } -function hex_hmac_sha1(k, d) - { return rstr2hex(rstr_hmac_sha1(str2rstr_utf8(k), str2rstr_utf8(d))); } -function b64_hmac_sha1(k, d) - { return rstr2b64(rstr_hmac_sha1(str2rstr_utf8(k), str2rstr_utf8(d))); } -function any_hmac_sha1(k, d, e) - { return rstr2any(rstr_hmac_sha1(str2rstr_utf8(k), str2rstr_utf8(d)), e); } - -/* - * Perform a simple self-test to see if the VM is working - */ -function sha1_vm_test() -{ - return hex_sha1("abc").toLowerCase() == "a9993e364706816aba3e25717850c26c9cd0d89d"; -} - -/* - * Calculate the SHA1 of a raw string - */ -function rstr_sha1(s) -{ - return binb2rstr(binb_sha1(rstr2binb(s), s.length * 8)); -} - -/* - * Calculate the HMAC-SHA1 of a key and some data (raw strings) - */ -function rstr_hmac_sha1(key, data) -{ - var bkey = rstr2binb(key); - if(bkey.length > 16) bkey = binb_sha1(bkey, key.length * 8); - +var SHA1 = { +"hexcase": 0, +"b64pad": "", +"hex": function(s) { return SHA1.rstr2hex(SHA1.rstr(SHA1.str2rstr_utf8(s))); }, +"b64": function(s) { return SHA1.rstr2b64(SHA1.rstr(SHA1.str2rstr_utf8(s))); }, +"any": function(s, e) { return SHA1.rstr2any(SHA1.rstr(SHA1.str2rstr_utf8(s)), e); }, +"hex_hmac": function(k, d){ return SHA1.rstr2hex(SHA1.rstr_hmac(SHA1.str2rstr_utf8(k), SHA1.str2rstr_utf8(d))); }, +"b64_hmac": function(k, d){ return SHA1.rstr2b64(SHA1.rstr_hmac(SHA1.str2rstr_utf8(k), SHA1.str2rstr_utf8(d))); }, +"any_hmac": function(k, d, e){ return SHA1.rstr2any(SHA1.rstr_hmac(SHA1.str2rstr_utf8(k), SHA1.str2rstr_utf8(d)), e); }, +"rstr": function(s) { return SHA1.binb2rstr(SHA1.binb(SHA1.rstr2binb(s), s.length * 8)); }, +"rstr_hmac": function(key, data){ + var bkey = SHA1.rstr2binb(key); + if(bkey.length > 16) bkey = SHA1.binb(bkey, key.length * 8); var ipad = Array(16), opad = Array(16); - for(var i = 0; i < 16; i++) - { + for(var i = 0; i < 16; i++){ ipad[i] = bkey[i] ^ 0x36363636; opad[i] = bkey[i] ^ 0x5C5C5C5C; } - - var hash = binb_sha1(ipad.concat(rstr2binb(data)), 512 + data.length * 8); - return binb2rstr(binb_sha1(opad.concat(hash), 512 + 160)); -} - -/* - * Convert a raw string to a hex string - */ -function rstr2hex(input) -{ - try { hexcase } catch(e) { hexcase=0; } - var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; + var hash = SHA1.binb(ipad.concat(SHA1.rstr2binb(data)), 512 + data.length * 8); + return SHA1.binb2rstr(SHA1.binb(opad.concat(hash), 512 + 160)); +}, +"rstr2hex": function(input){ + try { SHA1.hexcase } catch(e) { SHA1.hexcase=0; } + var hex_tab = SHA1.hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; var output = ""; var x; - for(var i = 0; i < input.length; i++) - { + for(var i = 0; i < input.length; i++){ x = input.charCodeAt(i); output += hex_tab.charAt((x >>> 4) & 0x0F) + hex_tab.charAt( x & 0x0F); } return output; -} - -/* - * Convert a raw string to a base-64 string - */ -function rstr2b64(input) -{ - try { b64pad } catch(e) { b64pad=''; } +}, +"rstr2b64": function(input){ + try { SHA1.b64pad } catch(e) { SHA1.b64pad=''; } var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var output = ""; var len = input.length; - for(var i = 0; i < len; i += 3) - { + for(var i = 0; i < len; i += 3){ var triplet = (input.charCodeAt(i) << 16) | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0) | (i + 2 < len ? input.charCodeAt(i+2) : 0); - for(var j = 0; j < 4; j++) - { - if(i * 8 + j * 6 > input.length * 8) output += b64pad; + for(var j = 0; j < 4; j++){ + if(i * 8 + j * 6 > input.length * 8) output += SHA1.b64pad; else output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F); } } return output; -} - -/* - * Convert a raw string to an arbitrary string encoding - */ -function rstr2any(input, encoding) -{ +}, +"rstr2any": function(input, encoding){ var divisor = encoding.length; var remainders = Array(); var i, q, x, quotient; - - /* Convert to an array of 16-bit big-endian values, forming the dividend */ var dividend = Array(Math.ceil(input.length / 2)); for(i = 0; i < dividend.length; i++) - { dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1); - } - - /* - * Repeatedly perform a long division. The binary array forms the dividend, - * the length of the encoding is the divisor. Once computed, the quotient - * forms the dividend for the next step. We stop when the dividend is zero. - * All remainders are stored for later use. - */ - while(dividend.length > 0) - { + while(dividend.length > 0){ quotient = Array(); x = 0; - for(i = 0; i < dividend.length; i++) - { + for(i = 0; i < dividend.length; i++){ x = (x << 16) + dividend[i]; q = Math.floor(x / divisor); x -= q * divisor; @@ -1512,43 +1720,27 @@ function rstr2any(input, encoding) remainders[remainders.length] = x; dividend = quotient; } - - /* Convert the remainders to the output string */ var output = ""; for(i = remainders.length - 1; i >= 0; i--) output += encoding.charAt(remainders[i]); - - /* Append leading zero equivalents */ var full_length = Math.ceil(input.length * 8 / (Math.log(encoding.length) / Math.log(2))) for(i = output.length; i < full_length; i++) output = encoding[0] + output; return output; -} - -/* - * Encode a string as utf-8. - * For efficiency, this assumes the input is valid utf-16. - */ -function str2rstr_utf8(input) -{ +}, +"str2rstr_utf8": function(input){ var output = ""; var i = -1; var x, y; - - while(++i < input.length) - { - /* Decode utf-16 surrogate pairs */ + while(++i < input.length){ x = input.charCodeAt(i); y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0; - if(0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF) - { + if(0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF){ x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF); i++; } - - /* Encode output as utf-8 */ if(x <= 0x7F) output += String.fromCharCode(x); else if(x <= 0x7FF) @@ -1565,137 +1757,83 @@ function str2rstr_utf8(input) 0x80 | ( x & 0x3F)); } return output; -} - -/* - * Encode a string as utf-16 - */ -function str2rstr_utf16le(input) -{ +}, +"str2rstr_utf16le": function(input){ var output = ""; for(var i = 0; i < input.length; i++) output += String.fromCharCode( input.charCodeAt(i) & 0xFF, (input.charCodeAt(i) >>> 8) & 0xFF); return output; -} - -function str2rstr_utf16be(input) -{ +}, +"str2rstr_utf16be": function(input){ var output = ""; for(var i = 0; i < input.length; i++) output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF, input.charCodeAt(i) & 0xFF); return output; -} - -/* - * Convert a raw string to an array of big-endian words - * Characters >255 have their high-byte silently ignored. - */ -function rstr2binb(input) -{ +}, +"rstr2binb": function(input){ var output = Array(input.length >> 2); for(var i = 0; i < output.length; i++) output[i] = 0; for(var i = 0; i < input.length * 8; i += 8) output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (24 - i % 32); return output; -} - -/* - * Convert an array of big-endian words to a string - */ -function binb2rstr(input) -{ +}, +"binb2rstr": function(input){ var output = ""; for(var i = 0; i < input.length * 32; i += 8) output += String.fromCharCode((input[i>>5] >>> (24 - i % 32)) & 0xFF); return output; -} - -/* - * Calculate the SHA-1 of an array of big-endian words, and a bit length - */ -function binb_sha1(x, len) -{ - /* append padding */ +}, +"binb": function(x, len){ x[len >> 5] |= 0x80 << (24 - len % 32); x[((len + 64 >> 9) << 4) + 15] = len; - var w = Array(80); var a = 1732584193; var b = -271733879; var c = -1732584194; var d = 271733878; var e = -1009589776; - - for(var i = 0; i < x.length; i += 16) - { + for(var i = 0; i < x.length; i += 16){ var olda = a; var oldb = b; var oldc = c; var oldd = d; var olde = e; - - for(var j = 0; j < 80; j++) - { + for(var j = 0; j < 80; j++){ if(j < 16) w[j] = x[i + j]; - else w[j] = bit_rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1); - var t = safe_add(safe_add(bit_rol(a, 5), sha1_ft(j, b, c, d)), - safe_add(safe_add(e, w[j]), sha1_kt(j))); + else w[j] = SHA1.bit_rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1); + var t = SHA1.safe_add(SHA1.safe_add(SHA1.bit_rol(a, 5), SHA1.ft(j, b, c, d)), + SHA1.safe_add(SHA1.safe_add(e, w[j]), SHA1.kt(j))); e = d; d = c; - c = bit_rol(b, 30); + c = SHA1.bit_rol(b, 30); b = a; a = t; } - - a = safe_add(a, olda); - b = safe_add(b, oldb); - c = safe_add(c, oldc); - d = safe_add(d, oldd); - e = safe_add(e, olde); + a = SHA1.safe_add(a, olda); + b = SHA1.safe_add(b, oldb); + c = SHA1.safe_add(c, oldc); + d = SHA1.safe_add(d, oldd); + e = SHA1.safe_add(e, olde); } return Array(a, b, c, d, e); - -} - -/* - * Perform the appropriate triplet combination function for the current - * iteration - */ -function sha1_ft(t, b, c, d) -{ +}, +"ft": function(t, b, c, d){ if(t < 20) return (b & c) | ((~b) & d); if(t < 40) return b ^ c ^ d; if(t < 60) return (b & c) | (b & d) | (c & d); return b ^ c ^ d; -} - -/* - * Determine the appropriate additive constant for the current iteration - */ -function sha1_kt(t) -{ +}, +"kt": function(t){ return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : - (t < 60) ? -1894007588 : -899497514; -} - -/* - * Add integers, wrapping at 2^32. This uses 16-bit operations internally - * to work around bugs in some JS interpreters. - */ -function safe_add(x, y) -{ - var lsw = (x & 0xFFFF) + (y & 0xFFFF); - var msw = (x >> 16) + (y >> 16) + (lsw >> 16); - return (msw << 16) | (lsw & 0xFFFF); -} - -/* - * Bitwise rotate a 32-bit number to the left. - */ -function bit_rol(num, cnt) -{ - return (num << cnt) | (num >>> (32 - cnt)); -} + (t < 60) ? -1894007588 : -899497514; +}, +"safe_add": function(x, y){ + var lsw = (x & 0xFFFF) + (y & 0xFFFF); + var msw = (x >> 16) + (y >> 16) + (lsw >> 16); + return (msw << 16) | (lsw & 0xFFFF); +}, +"bit_rol": function(num, cnt) { return (num << cnt) | (num >>> (32 - cnt)) } +} \ No newline at end of file -- cgit v1.2.3-70-g09d2