var DEBUG = false;
var BOSH_SERVICE = '/http-bind/';
-var NS_VCARD = 'vcard-temp';
-var NS_CAPS= 'http://jabber.org/protocol/caps';
-var NS_PEP = 'http://jabber.org/protocol/pubsub#event';
-var NS_TUNE = 'http://jabber.org/protocol/tune';
+Strophe.addNamespace('VCARD', 'vcard-temp');
+Strophe.addNamespace('CAPS', 'http://jabber.org/protocol/caps');
+Strophe.addNamespace('PEP', 'http://jabber.org/protocol/pubsub#event');
+Strophe.addNamespace('TUNE', 'http://jabber.org/protocol/tune');
var localJID = null;
-var connection = null;
-var show_log = true;
+var connection = null;
+var show_log = true;
-var features = new Array(NS_CAPS, NS_TUNE+'+notify', Strophe.NS.DISCO_INFO);
+var features = [Strophe.NS.CAPS, Strophe.NS.TUNE + '+notify', Strophe.NS.DISCO_INFO];
var appName = 'socialXMPP';
-var roster = new Array();
+var roster = [];
function log(msg) {
var entry = $('<div></div>').append(document.createTextNode(msg));
return jid.split('@').join('-').split('.').join('-');
}
+function genCaps() {
+ var S = '', i;
+ S += 'client/web//' + appName + '<';
+ features.sort();
+ for (i = 0; i < features.length; i++) {
+ S += features[i] + '<';
+ }
+ return b64_sha1(S);
+}
+
function populateVCard(e, jid) {
- var easy_cases = new Array('FN', 'FAMILY', 'MIDDLE', 'GIVEN', 'NICKNAME', 'BDAY', 'CTRY', 'USERID');
- for (var i=0; i<easy_cases.length; i++) {
- var text = e.getElementsByTagName(easy_cases[i])[0];
+ var easy_cases = ['FN', 'FAMILY', 'MIDDLE', 'GIVEN', 'NICKNAME', 'BDAY', 'CTRY', 'USERID'], i, text, avatar, mime, binval;
+ for (i = 0; i < easy_cases.length; i++) {
+ text = e.getElementsByTagName(easy_cases[i])[0];
if (text) {
text = Strophe.getText(text);
- $('#'+easy_cases[i]).append(Strophe.xmlTextNode(text));
+ $('#' + easy_cases[i]).append(Strophe.xmlTextNode(text));
}
}
- var avatar = e.getElementsByTagName('PHOTO')[0];
+ avatar = e.getElementsByTagName('PHOTO')[0];
if (avatar) {
- var mime = Strophe.getText(avatar.getElementsByTagName('TYPE')[0]);
- var binval = Strophe.getText(avatar.getElementsByTagName('BINVAL')[0]);
+ mime = Strophe.getText(avatar.getElementsByTagName('TYPE')[0]);
+ binval = Strophe.getText(avatar.getElementsByTagName('BINVAL')[0]);
$("#"+jid2id(jid)+" img").attr('src', 'data:'+mime+';base64,'+binval);
$("#PHOTO img").attr('src', 'data:'+mime+';base64,'+binval);
if (!roster[jid2id(jid)].tune.isEmpty()) {
$('#vCard ul').append('<li id="tune"><span class="vCardName">Tune: </span>Listening to '+
roster[jid2id(jid)].tune.title + ' by ' + roster[jid2id(jid)].tune.artist +
- ' from ' + roster[jid2id(jid)].tune.source + '</li>')
+ ' from ' + roster[jid2id(jid)].tune.source + '</li>');
}
$('#vCard').click(function () {
function _cbVCard(e) {
var jid = e.getAttribute('from');
- if (roster[jid2id(jid)].vCard == "") {
+ if (roster[jid2id(jid)].vCard === "") {
roster[jid2id(jid)].vCard = e;
}
if ($('#vCard_container').is(':visible')) {
}
function _cbOwnVCard(e) {
+ var easy_cases = ['FN', 'FAMILY', 'MIDDLE', 'GIVEN', 'NICKNAME', 'BDAY', 'CTRY', 'USERID'], i, text, avatar, mime, binval;
$('#ownInfo').empty();
$('#ownInfo').load('vCard.html #ownvCard', function() {
- var easy_cases = new Array('FN', 'FAMILY', 'MIDDLE', 'GIVEN', 'NICKNAME', 'BDAY', 'CTRY', 'USERID');
- for (var i=0; i<easy_cases.length; i++) {
- var text = e.getElementsByTagName(easy_cases[i])[0];
+ for (i = 0; i < easy_cases.length; i++) {
+ text = e.getElementsByTagName(easy_cases[i])[0];
if (text) {
text = Strophe.getText(text);
- $('#own'+easy_cases[i]).append(Strophe.xmlTextNode(text));
+ $('#own' + easy_cases[i]).append(Strophe.xmlTextNode(text));
}
}
- var avatar = e.getElementsByTagName('PHOTO')[0];
+ avatar = e.getElementsByTagName('PHOTO')[0];
if (avatar) {
- var mime = Strophe.getText(avatar.getElementsByTagName('TYPE')[0]);
- var binval = Strophe.getText(avatar.getElementsByTagName('BINVAL')[0]);
+ mime = Strophe.getText(avatar.getElementsByTagName('TYPE')[0]);
+ binval = Strophe.getText(avatar.getElementsByTagName('BINVAL')[0]);
$("#ownPHOTO img").attr('src', 'data:'+mime+';base64,'+binval);
}
}
function getVCard(jid) {
- var id = connection.getUniqueId('vCardGet');
- if (roster[jid2id(jid)].vCard == "") {
- var vCardiq = $iq({'to':jid,
+ var id = connection.getUniqueId('vCardGet'), vCardiq;
+ if (roster[jid2id(jid)].vCard === "") {
+ vCardiq = $iq({'to':jid,
'id':id,
'type':'get'}
- ).c('vCard', {'xmlns':NS_VCARD});
+ ).c('vCard', {'xmlns': Strophe.NS.VCARD});
connection.addHandler(_cbVCard, null, 'iq', 'result', id);
connection.send(vCardiq.tree());
} else {
}
function getOwnInfo() {
- var id = connection.getUniqueId('vCardGet');
- var vCardiq = $iq({'id': id, 'type': 'get'}
- ).c('vCard', {'xmlns':NS_VCARD});
+ var id = connection.getUniqueId('vCardGet'),
+ vCardiq = $iq({'id': id, 'type': 'get'}
+ ).c('vCard', {'xmlns': Strophe.NS.VCARD});
connection.addHandler(_cbOwnVCard, null, 'iq', 'result', id);
connection.send(vCardiq.tree());
}
+function createBubble(jid) {
+ var id = jid2id(jid) + 'Chat';
+ $('body').append('<div class="chat" id="' + id + '" onmousedown="startDrag(this)"><a href="#" onclick="$(' + "'#" + id + "'" + ').hide()">Close</a>' +
+ '<span class ="chatTitle"> ' + jid + '</span><p></p></div>');
+ $('#' + id).append('<form id="' + id + 'Form" class="chatForm" onsubmit="return sendMessage(this, \'' + jid + '\');" action="#"><input type="text" name="text" id="' + id + 'Text" class="chatForm" /></form>');
+ $('#' + id).css( 'top', $('#' + jid2id(jid)).position().top + 40);
+ $('#' + id).css( 'left', $('#' + jid2id(jid)).position().left + 40);
+}
+
function addFriend(jid, nick) {
roster[jid2id(jid)] = new Buddy(nick, jid);
$('#friends').append('<div class="friend" id="'+jid2id(jid)+'"><img src="imgs/none.png" /><br /><a class="nick">'+nick+'</a></div>');
$('#' + jid2id(jid) + ' img').click(function() {
- getVCard(jid);
+ getVCard(jid);
});
$('#' + jid2id(jid) + ' a').click(function() {
var id = jid2id(jid) + 'Chat';
}
+function handleTune(jid, tuneXML) {
+ var tune = new Tune();
+ if (tuneXML.childNodes.length > 0) {
+ tune.artist = Strophe.getText(tuneXML.getElementsByTagName('artist')[0]);
+ tune.length= Strophe.getText(tuneXML.getElementsByTagName('length')[0]);
+ tune.rating= Strophe.getText(tuneXML.getElementsByTagName('rating')[0]);
+ tune.source= Strophe.getText(tuneXML.getElementsByTagName('source')[0]);
+ tune.title= Strophe.getText(tuneXML.getElementsByTagName('title')[0]);
+ tune.track= Strophe.getText(tuneXML.getElementsByTagName('track')[0]);
+ tune.uri= Strophe.getText(tuneXML.getElementsByTagName('uri')[0]);
+ if (roster[jid2id(jid)].visible === true) {
+ if ( $('#tune').length > 0) {
+ $('#tune').empty();
+ $('#tune').append('<span class="vCardName">Tune: </span>');
+ $('#tune').append(Strophe.xmlTextNode('Listening to '+ tune.title +
+ ' by ' + tune.artist + ' from ' + tune.source));
+ } else {
+ $('#vCard ul').append('<li id="tune"><span class="vCardName">Tune: </span>Listening to '+ tune.title +
+ ' by ' + tune.artist + ' from ' + tune.source + '</li>');
+ }
+ }
+ } else {
+ $('#tune').remove();
+ }
+ roster[jid2id(jid)].tune = tune;
+}
+
+function _cbPEP(e) {
+ var from = e.getAttribute('from'), items;
+ if (Strophe.getBareJidFromJid(from) == Strophe.getBareJidFromJid(connection.jid)) {
+ return true; // Drop own PEP events
+ }
+ items = e.getElementsByTagName('items')[0];
+ // Handle Tune
+ if (items.getAttribute('node') == Strophe.NS.TUNE) {
+ handleTune(from, items.getElementsByTagName('tune')[0]);
+ }
+ return true;
+}
+
function _cbRoster(e) {
- var query = e.getElementsByTagName('query')[0];
- var entries = query.getElementsByTagName('item');
- for (var item=0; item<entries.length; item++) {
+ var query = e.getElementsByTagName('query')[0],
+ entries = query.getElementsByTagName('item'), item, nick, initialPresence;
+ for (item = 0; item < entries.length; item++) {
nick = entries[item].getAttribute('name');
if (!nick) {
nick = entries[item].getAttribute('jid').split('@')[0];
}
addFriend(entries[item].getAttribute('jid'), nick);
}
- connection.addHandler(_cbPEP, NS_PEP, 'message');
- var initialPresence = $pres().c('show').t('online').up().c('status').t('Hy, I am an socialXMPP instance').up().c('priority').t('1').up().c('c', {xmlns: NS_CAPS, hash: 'sha-1', node: 'http://jabber.babelmonkeys.de', ver: genCaps()}).up();
+ connection.addHandler(_cbPEP, Strophe.NS.PEP, 'message');
+ initialPresence = $pres().c('show').t('online').up().c('status').t('Hy, I am an socialXMPP instance').up().c('priority').t('-1').up().c('c', {xmlns: Strophe.NS.CAPS, hash: 'sha-1', node: 'http://jabber.babelmonkeys.de', ver: genCaps()}).up();
connection.send(initialPresence.tree());
$('#friends').css('display', 'table');
}
function getRoster() {
- var id = connection.getUniqueId('roster');
+ var id, rosteriq;
+ id = connection.getUniqueId('roster');
- var rosteriq = $iq({'id':id,
+ rosteriq = $iq({'id':id,
'type':'get'}
).c('query', {'xmlns':Strophe.NS.ROSTER});
}
function _cbDisco(e) {
- var id = e.getAttribute('id');
- var jid = e.getAttribute('from');
+ var i, id, jid, response, query, response;
+ id = e.getAttribute('id');
+ jid = e.getAttribute('from');
if (jid) {
- var response = $iq({id: id, type: 'result', to: jid});
+ response = $iq({id: id, type: 'result', to: jid});
} else {
- var response = $iq({id: id, type: 'result'});
+ response = $iq({id: id, type: 'result'});
}
- var query = response.c('query', {xmlns: Strophe.NS.DISCO_INFO})
+ query = response.c('query', {xmlns: Strophe.NS.DISCO_INFO});
query.c('identity', {category: 'client', type: 'web', name: appName}).up();
- for (var i = 0; i < features.length; i++) {
+ for (i = 0; i < features.length; i++) {
query.c('feature', {'var': features[i]}).up();
}
connection.send(response.tree());
}
function sendMessage(form, to) {
+ var id, message;
if (form.text.value) {
- var id = jid2id(to) + 'Chat';
- var message = $msg({'type': 'chat', 'to': to}).c('body').t(form.text.value);
+ id = jid2id(to) + 'Chat';
+ message = $msg({'type': 'chat', 'to': to}).c('body').t(form.text.value);
connection.send(message.tree());
if ($('#' + id + ' p *').length > 0) {
$('#' + id + ' p').append('<br/>');
return false;
}
-function createBubble(jid) {
- var id = jid2id(jid) + 'Chat';
- $('body').append('<div class="chat" id="' + id + '" onmousedown="startDrag(this)"><a href="#" onclick="$(' + "'#" + id + "'" + ').hide()">Close</a>' +
- '<span class ="chatTitle"> ' + jid + '</span><p></p></div>');
- $('#' + id).append('<form id="' + id + 'Form" class="chatForm" onsubmit="return sendMessage(this, \'' + jid + '\');" action="#"><input type="text" name="text" id="' + id + 'Text" class="chatForm" /></form>')
- $('#' + id).css( 'top', $('#' + jid2id(jid)).position().top + 40);
- $('#' + id).css( 'left', $('#' + jid2id(jid)).position().left + 40);
-}
-
-
function _cbMessage(msg) {
- if ($(msg).attr('type') != 'chat')
+ var id, jid, body;
+ if ($(msg).attr('type') != 'chat') {
return true;
- var jid = $(msg).attr('from');
- var id = jid2id(jid) + 'Chat';
- var body = $(msg).find('body:first').text();
+ }
+ jid = $(msg).attr('from');
+ id = jid2id(jid) + 'Chat';
+ body = $(msg).find('body:first').text();
body = escape(body);
body = body.replace(/%0A/g, '<br/>');
body = body.replace(/%3C/g, '<');
return true;
}
-function genCaps() {
- var S = '';
- S += 'client/web//' + appName + '<'
- features.sort();
- for (var i = 0; i < features.length; i++) {
- S += features[i] + '<';
- }
- return b64_sha1(S);
-}
-
-function handleTune(jid, tuneXML) {
- var tune = new Tune();
- if (tuneXML.childNodes.length > 0) {
- tune.artist = Strophe.getText(tuneXML.getElementsByTagName('artist')[0]);
- tune.length= Strophe.getText(tuneXML.getElementsByTagName('length')[0]);
- tune.rating= Strophe.getText(tuneXML.getElementsByTagName('rating')[0]);
- tune.source= Strophe.getText(tuneXML.getElementsByTagName('source')[0]);
- tune.title= Strophe.getText(tuneXML.getElementsByTagName('title')[0]);
- tune.track= Strophe.getText(tuneXML.getElementsByTagName('track')[0]);
- tune.uri= Strophe.getText(tuneXML.getElementsByTagName('uri')[0]);
- if (roster[jid2id(jid)].visible == true) {
- if ( $('#tune').length > 0) {
- $('#tune').empty();
- $('#tune').append('<span class="vCardName">Tune: </span>');
- $('#tune').append(Strophe.xmlTextNode('Listening to '+ tune.title +
- ' by ' + tune.artist + ' from ' + tune.source));
- } else {
- $('#vCard ul').append('<li id="tune"><span class="vCardName">Tune: </span>Listening to '+ tune.title +
- ' by ' + tune.artist + ' from ' + tune.source + '</li>')
- }
- }
- } else {
- $('#tune').remove();
- }
- roster[jid2id(jid)].tune = tune;
-}
+function showConnect() {
+ var jid, pass, button;
+ jid = $('#jid');
+ pass = $('#pass');
+ button = $('#connect').get(0);
-function _cbPEP(e) {
- var from = e.getAttribute('from');
- if (Strophe.getBareJidFromJid(from) == Strophe.getBareJidFromJid(connection.jid)) {
- return true; // Drop own PEP events
- }
- var items = e.getElementsByTagName('items')[0];
- // Handle Tune
- if (items.getAttribute('node') == NS_TUNE) {
- handleTune(from, items.getElementsByTagName('tune')[0]);
- }
- return true;
+ $('#log').empty();
+ $('#ownInfo').empty();
+ $('#ownInfo').hide();
+ $('#vCard_container').empty();
+ $('#friends').empty();
+ $('#friends').hide();
+ $('.chat').remove();
+ $('label').show();
+ jid.show();
+ pass.show();
+ button.value = 'connect';
+ return false;
}
function onConnect(status) {
}
}
-function showConnect() {
- var jid = $('#jid');
- var pass = $('#pass');
- var button = $('#connect').get(0);
-
- $('#log').empty();
- $('#ownInfo').empty();
- $('#ownInfo').hide();
- $('#vCard_container').empty();
- $('#friends').empty();
- $('#friends').hide();
- $('.chat').remove();
- $('label').show();
- jid.show();
- pass.show();
- button.value = 'connect';
- return false;
-}
-
function showDisconnect() {
- var jid = $('#jid');
- var pass = $('#pass');
- var button = $('#connect').get(0);
+ var jid, pass, button;
+ jid = $('#jid');
+ pass = $('#pass');
+ button = $('#connect').get(0);
button.value = 'disconnect';
pass.hide();
}
$(document).ready(function () {
+ var button, jid, pass;
if (DEBUG) {
$('#log_container').show();
}
});
$('#cred').bind('submit', function () {
- var button = $('#connect').get(0);
- var jid = $('#jid');
- var pass = $('#pass');
+ button = $('#connect').get(0);
+ jid = $('#jid');
+ pass = $('#pass');
localJID = jid.get(0).value;
if (button.value == 'connect') {
- showDisconnect();
+ showDisconnect();
connection.connect(localJID,
pass.get(0).value,
onConnect);
dragElement = null;
}
-onunload = function() {
+var onunload = function() {
if (connection) {
connection.disconnect();
}
-}
+};
-onmousemove = doDrag;
-onmouseup = stopDrag;
+var onmousemove = doDrag;
+var onmouseup = stopDrag;
--- /dev/null
+/*
+ * 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.
+ */
+
+/*
+ * 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 */
+var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
+
+/*
+ * 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 binb2hex(core_sha1(str2binb(s),s.length * chrsz));}
+function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length * chrsz));}
+function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length * chrsz));}
+function hex_hmac_sha1(key, data){ return binb2hex(core_hmac_sha1(key, data));}
+function b64_hmac_sha1(key, data){ return binb2b64(core_hmac_sha1(key, data));}
+function str_hmac_sha1(key, data){ return binb2str(core_hmac_sha1(key, data));}
+
+/*
+ * Perform a simple self-test to see if the VM is working
+ */
+function sha1_vm_test()
+{
+ return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
+}
+
+/*
+ * Calculate the SHA-1 of an array of big-endian words, and a bit length
+ */
+function core_sha1(x, len)
+{
+ /* append padding */
+ x[len >> 5] |= 0x80 << (24 - len % 32);
+ x[((len + 64 >> 9) << 4) + 15] = len;
+
+ var w = new Array(80);
+ var a = 1732584193;
+ var b = -271733879;
+ var c = -1732584194;
+ var d = 271733878;
+ var e = -1009589776;
+
+ var i, j, t, olda, oldb, oldc, oldd, olde;
+ for (i = 0; i < x.length; i += 16)
+ {
+ olda = a;
+ oldb = b;
+ oldc = c;
+ oldd = d;
+ olde = e;
+
+ for (j = 0; j < 80; j++)
+ {
+ if (j < 16) { w[j] = x[i + j]; }
+ else { w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1); }
+ t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
+ safe_add(safe_add(e, w[j]), sha1_kt(j)));
+ e = d;
+ d = c;
+ c = 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);
+ }
+ return [a, b, c, d, e];
+}
+
+/*
+ * Perform the appropriate triplet combination function for the current
+ * iteration
+ */
+function sha1_ft(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)
+{
+ return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
+ (t < 60) ? -1894007588 : -899497514;
+}
+
+/*
+ * Calculate the HMAC-SHA1 of a key and some data
+ */
+function core_hmac_sha1(key, data)
+{
+ var bkey = str2binb(key);
+ if (bkey.length > 16) { bkey = core_sha1(bkey, key.length * chrsz); }
+
+ var ipad = new Array(16), opad = new Array(16);
+ for (var i = 0; i < 16; i++)
+ {
+ ipad[i] = bkey[i] ^ 0x36363636;
+ opad[i] = bkey[i] ^ 0x5C5C5C5C;
+ }
+
+ var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz);
+ return core_sha1(opad.concat(hash), 512 + 160);
+}
+
+/*
+ * 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 rol(num, cnt)
+{
+ return (num << cnt) | (num >>> (32 - cnt));
+}
+
+/*
+ * 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.
+ */
+function str2binb(str)
+{
+ var bin = [];
+ var mask = (1 << chrsz) - 1;
+ for (var i = 0; i < str.length * chrsz; i += chrsz)
+ {
+ bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i%32);
+ }
+ return bin;
+}
+
+/*
+ * Convert an array of big-endian words to a string
+ */
+function binb2str(bin)
+{
+ var str = "";
+ var mask = (1 << chrsz) - 1;
+ for (var i = 0; i < bin.length * 32; i += chrsz)
+ {
+ str += String.fromCharCode((bin[i>>5] >>> (32 - chrsz - i%32)) & mask);
+ }
+ return str;
+}
+
+/*
+ * Convert an array of big-endian words to a hex string.
+ */
+function binb2hex(binarray)
+{
+ var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
+ var str = "";
+ for (var i = 0; i < binarray.length * 4; i++)
+ {
+ str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
+ hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF);
+ }
+ return str;
+}
+
+/*
+ * Convert an array of big-endian words to a base-64 string
+ */
+function binb2b64(binarray)
+{
+ var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+ var str = "";
+ var triplet, j;
+ for (var i = 0; i < binarray.length * 4; i += 3)
+ {
+ triplet = (((binarray[i >> 2] >> 8 * (3 - i %4)) & 0xFF) << 16) |
+ (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 ) |
+ ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF);
+ for (j = 0; j < 4; j++)
+ {
+ if (i * 8 + j * 6 > binarray.length * 32) { str += b64pad; }
+ else { str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); }
+ }
+ }
+ return str;
+}