From db5683a69dec16b85772dac22693170d2357f88b Mon Sep 17 00:00:00 2001 From: Daniel Reichert Date: Sat, 22 Nov 2014 20:36:57 -0800 Subject: [PATCH] Move Base64 functions to their own file Remove Base64 string functions from helpers.js Rename Base64 string functions Removed unused functions from stringview.js --- js/api.js | 8 ++--- js/helpers.js | 75 --------------------------------------------- js/protocol.js | 2 +- js/stringview.js | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 80 deletions(-) create mode 100644 js/stringview.js diff --git a/js/api.js b/js/api.js index 766a0a6f..02f336c2 100644 --- a/js/api.js +++ b/js/api.js @@ -192,12 +192,12 @@ window.textsecure.api = function () { urlParameters : "/" + number + "/" + deviceId, }).then(function(res) { var promises = []; - res.identityKey = base64DecToArr(res.identityKey); + res.identityKey = StringView.base64ToBytes(res.identityKey); for (var i = 0; i < res.devices.length; i++) { - res.devices[i].signedPreKey.publicKey = base64DecToArr(res.devices[i].signedPreKey.publicKey); - res.devices[i].signedPreKey.signature = base64DecToArr(res.devices[i].signedPreKey.signature); + res.devices[i].signedPreKey.publicKey = StringView.base64ToBytes(res.devices[i].signedPreKey.publicKey); + res.devices[i].signedPreKey.signature = StringView.base64ToBytes(res.devices[i].signedPreKey.signature); promises[i] = window.textsecure.crypto.Ed25519Verify(res.identityKey, res.devices[i].signedPreKey.publicKey, res.devices[i].signedPreKey.signature); - res.devices[i].preKey.publicKey = base64DecToArr(res.devices[i].preKey.publicKey); + res.devices[i].preKey.publicKey = StringView.base64ToBytes(res.devices[i].preKey.publicKey); //TODO: Is this still needed? //if (res.devices[i].keyId === undefined) // res.devices[i].keyId = 0; diff --git a/js/helpers.js b/js/helpers.js index df88dea0..ca68abe6 100644 --- a/js/helpers.js +++ b/js/helpers.js @@ -14,81 +14,6 @@ * along with this program. If not, see . */ -function b64ToUint6 (nChr) { - - return nChr > 64 && nChr < 91 ? - nChr - 65 - : nChr > 96 && nChr < 123 ? - nChr - 71 - : nChr > 47 && nChr < 58 ? - nChr + 4 - : nChr === 43 ? - 62 - : nChr === 47 ? - 63 - : - 0; - -} - -function base64DecToArr (sBase64, nBlocksSize) { - var - sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""), nInLen = sB64Enc.length, - nOutLen = nBlocksSize ? Math.ceil((nInLen * 3 + 1 >> 2) / nBlocksSize) * nBlocksSize : nInLen * 3 + 1 >> 2; -var aBBytes = new ArrayBuffer(nOutLen); -var taBytes = new Uint8Array(aBBytes); - - for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) { - nMod4 = nInIdx & 3; - nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4; - if (nMod4 === 3 || nInLen - nInIdx === 1) { - for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) { - taBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255; - } - nUint24 = 0; - } - } - return aBBytes; -} - -/* Base64 string to array encoding */ - -function uint6ToB64 (nUint6) { - - return nUint6 < 26 ? - nUint6 + 65 - : nUint6 < 52 ? - nUint6 + 71 - : nUint6 < 62 ? - nUint6 - 4 - : nUint6 === 62 ? - 43 - : nUint6 === 63 ? - 47 - : - 65; - -} - -function base64EncArr (aBytes) { - - var nMod3, sB64Enc = ""; - - for (var nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) { - nMod3 = nIdx % 3; - //if (nIdx > 0 && (nIdx * 4 / 3) % 76 === 0) { sB64Enc += "\r\n"; } - nUint24 |= aBytes[nIdx] << (16 >>> nMod3 & 24); - if (nMod3 === 2 || aBytes.length - nIdx === 1) { - sB64Enc += String.fromCharCode(uint6ToB64(nUint24 >>> 18 & 63), uint6ToB64(nUint24 >>> 12 & 63), uint6ToB64(nUint24 >>> 6 & 63), uint6ToB64(nUint24 & 63)); - nUint24 = 0; - } - } - - return sB64Enc.replace(/A(?=A$|$)/g, "="); - -} - - window.textsecure = window.textsecure || {}; /********************************* diff --git a/js/protocol.js b/js/protocol.js index ac429107..08472d44 100644 --- a/js/protocol.js +++ b/js/protocol.js @@ -545,7 +545,7 @@ window.textsecure.protocol = function() { var aes_key = toArrayBuffer(signaling_key.substring(0, 32)); var mac_key = toArrayBuffer(signaling_key.substring(32, 32 + 20)); - var decodedMessage = base64DecToArr(getString(message)); + var decodedMessage = StringView.base64ToBytes(getString(message)); if (new Uint8Array(decodedMessage)[0] != 1) throw new Error("Got bad version number: " + decodedMessage[0]); diff --git a/js/stringview.js b/js/stringview.js new file mode 100644 index 00000000..a5f7543c --- /dev/null +++ b/js/stringview.js @@ -0,0 +1,79 @@ +"use strict"; + +/* + * These functions are from the Mozilla Developer Network + * https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding + * + */ + +var StringView = { + + b64ToUint6: function(nChr) { + return nChr > 64 && nChr < 91 ? + nChr - 65 + : nChr > 96 && nChr < 123 ? + nChr - 71 + : nChr > 47 && nChr < 58 ? + nChr + 4 + : nChr === 43 ? + 62 + : nChr === 47 ? + 63 + : + 0; + }, + + base64ToBytes: function(sBase64, nBlocksSize) { + var + sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""), nInLen = sB64Enc.length, + nOutLen = nBlocksSize ? Math.ceil((nInLen * 3 + 1 >> 2) / nBlocksSize) * nBlocksSize : nInLen * 3 + 1 >> 2; + var aBBytes = new ArrayBuffer(nOutLen); + var taBytes = new Uint8Array(aBBytes); + + for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) { + nMod4 = nInIdx & 3; + nUint24 |= StringView.b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4; + if (nMod4 === 3 || nInLen - nInIdx === 1) { + for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) { + taBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255; + } + nUint24 = 0; + } + } + return aBBytes; + }, + + uint6ToB64: function(nUint6) { + return nUint6 < 26 ? + nUint6 + 65 + : nUint6 < 52 ? + nUint6 + 71 + : nUint6 < 62 ? + nUint6 - 4 + : nUint6 === 62 ? + 43 + : nUint6 === 63 ? + 47 + : + 65; + }, + + bytesToBase64: function(aBytes) { + var nMod3, sB64Enc = ""; + for (var nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) { + nMod3 = nIdx % 3; + if (nIdx > 0 && (nIdx * 4 / 3) % 76 === 0) { sB64Enc += "\r\n"; } + nUint24 |= aBytes[nIdx] << (16 >>> nMod3 & 24); + if (nMod3 === 2 || aBytes.length - nIdx === 1) { + sB64Enc += String.fromCharCode( + StringView.uint6ToB64(nUint24 >>> 18 & 63), + StringView.uint6ToB64(nUint24 >>> 12 & 63), + StringView.uint6ToB64(nUint24 >>> 6 & 63), + StringView.uint6ToB64(nUint24 & 63) + ); + nUint24 = 0; + } + } + return sB64Enc.replace(/A(?=A$|$)/g, "="); + } +};