From 0e44b10aebb462ec96cf4bd6cdfec1227fc0d2df Mon Sep 17 00:00:00 2001 From: Caleb James DeLisle Date: Fri, 30 Jan 2015 16:52:23 +0100 Subject: [PATCH] Seperated common crypto operations to common file and made common toolbar used for both pad and spreadsheet --- www/common/crypto.js | 77 ++++++++++++++++++++++++++ www/common/toolbar.js | 107 ++++++++++++++++++++---------------- www/pad/main.js | 31 +++-------- www/pad/realtime-wysiwyg.js | 83 +++------------------------- www/sheet/main.js | 84 +++------------------------- 5 files changed, 160 insertions(+), 222 deletions(-) create mode 100644 www/common/crypto.js diff --git a/www/common/crypto.js b/www/common/crypto.js new file mode 100644 index 000000000..aa4f0b68d --- /dev/null +++ b/www/common/crypto.js @@ -0,0 +1,77 @@ +define([ + '/bower_components/tweetnacl/nacl-fast.min.js', +], function () { + var Nacl = window.nacl; + var module = { exports: {} }; + + var encryptStr = function (str, key) { + var array = Nacl.util.decodeUTF8(str); + var nonce = Nacl.randomBytes(24); + var packed = Nacl.secretbox(array, nonce, key); + if (!packed) { throw new Error(); } + return Nacl.util.encodeBase64(nonce) + "|" + Nacl.util.encodeBase64(packed); + }; + + var decryptStr = function (str, key) { + var arr = str.split('|'); + if (arr.length !== 2) { throw new Error(); } + var nonce = Nacl.util.decodeBase64(arr[0]); + var packed = Nacl.util.decodeBase64(arr[1]); + var unpacked = Nacl.secretbox.open(packed, nonce, key); + if (!unpacked) { throw new Error(); } + return Nacl.util.encodeUTF8(unpacked); + }; + + // this is crap because of bencoding messages... it should go away.... + var splitMessage = function (msg, sending) { + var idx = 0; + var nl; + for (var i = ((sending) ? 0 : 1); i < 3; i++) { + nl = msg.indexOf(':',idx); + idx = nl + Number(msg.substring(idx,nl)) + 1; + } + return [ msg.substring(0,idx), msg.substring(msg.indexOf(':',idx) + 1) ]; + }; + + var encrypt = module.exports.encrypt = function (msg, key) { + var spl = splitMessage(msg, true); + var json = JSON.parse(spl[1]); + // non-patches are not encrypted. + if (json[0] !== 2) { return msg; } + json[1] = encryptStr(JSON.stringify(json[1]), key); + var res = JSON.stringify(json); + return spl[0] + res.length + ':' + res; + }; + + var decrypt = module.exports.decrypt = function (msg, key) { + var spl = splitMessage(msg, false); + var json = JSON.parse(spl[1]); + // non-patches are not encrypted. + if (json[0] !== 2) { return msg; } + if (typeof(json[1]) !== 'string') { throw new Error(); } + json[1] = JSON.parse(decryptStr(json[1], key)); + var res = JSON.stringify(json); + return spl[0] + res.length + ':' + res; + }; + + var parseKey = module.exports.parseKey = function (str) { + var array = Nacl.util.decodeBase64(str); + var hash = Nacl.hash(array); + var lk = hash.subarray(32); + return { + lookupKey: lk, + cryptKey: hash.subarray(0,32), + channel: Nacl.util.encodeBase64(lk).substring(0,10) + }; + }; + + var rand64 = module.exports.rand64 = function (bytes) { + return Nacl.util.encodeBase64(Nacl.randomBytes(bytes)); + }; + + var genKey = module.exports.genKey = function () { + return rand64(18); + }; + + return module.exports; +}); diff --git a/www/common/toolbar.js b/www/common/toolbar.js index 3399e804b..a471591ec 100644 --- a/www/common/toolbar.js +++ b/www/common/toolbar.js @@ -1,4 +1,6 @@ -var Toolbar = function ($, container, Messages, myUserName, realtime) { +define([ + '/common/messages.js' +], function (Messages) { /** Id of the element for getting debug info. */ var DEBUG_LINK_CLS = 'rtwysiwyg-debug-link'; @@ -22,15 +24,15 @@ var Toolbar = function ($, container, Messages, myUserName, realtime) { return 'rtwysiwyg-uid-' + String(Math.random()).substring(2); }; - var createRealtimeToolbar = function (container) { + var createRealtimeToolbar = function ($container) { var id = uid(); - $(container).prepend( + $container.prepend( '
' + '
' + '
' + '
' ); - var toolbar = $('#'+id); + var toolbar = $container.find('#'+id); toolbar.append([ '