From 7df460c766b152c25c8b3e6506e61f3b8c88b547 Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 19 Dec 2016 14:55:39 +0100 Subject: [PATCH] implement createChannelId api --- www/common/cryptpad-common.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 2ec46617a..0843fffc1 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -240,11 +240,30 @@ define([ }; var uint8ArrayToHex = common.uint8ArrayToHex = function (a) { - return a.map(function (e) { - return Number(e & 0xff).toString(16); + // call slice so Uint8Arrays work as expected + return Array.prototype.slice.call(a).map(function (e, i) { + var n = Number(e & 0xff).toString(16); + if (n === 'NaN') { + throw new Error('invalid input resulted in NaN'); + } + + switch (n.length) { + case 0: return '00'; // just being careful, shouldn't happen + case 1: return '0' + n; + case 2: return n; + default: throw new Error('unexpected value'); + } }).join(''); }; + var createChannelId = common.createChannelId = function () { + var id = uint8ArrayToHex(Crypto.Nacl.randomBytes(16)); + if (id.length !== 32 || /[^a-f0-9]/.test(id)) { + throw new Error('channel ids must consist of 32 hex characters'); + } + return id; + }; + var replaceHash = common.replaceHash = function (hash) { if (window.history && window.history.replaceState) { if (!/^#/.test(hash)) { hash = '#' + hash; }