|
|
@ -32,9 +32,58 @@ define([
|
|
|
|
return '/1/view/' + hexToBase64(chanKey) + '/'+Crypto.b64RemoveSlashes(keys.viewKeyStr)+'/';
|
|
|
|
return '/1/view/' + hexToBase64(chanKey) + '/'+Crypto.b64RemoveSlashes(keys.viewKeyStr)+'/';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
var getFileHashFromKeys = Hash.getFileHashFromKeys = function (fileKey, cryptKey) {
|
|
|
|
var getFileHashFromKeys = Hash.getFileHashFromKeys = function (fileKey, cryptKey) {
|
|
|
|
return '/2/' + hexToBase64(fileKey) + '/' + Crypto.b64RemoveSlashes(cryptKey) + '/';
|
|
|
|
return '/1/' + hexToBase64(fileKey) + '/' + Crypto.b64RemoveSlashes(cryptKey) + '/';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var fixDuplicateSlashes = function (s) {
|
|
|
|
|
|
|
|
return s.replace(/\/+/g, '/');
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var parseTypeHash = Hash.parseTypeHash = function (type, hash) {
|
|
|
|
|
|
|
|
if (!hash) { return; }
|
|
|
|
|
|
|
|
var parsed = {}
|
|
|
|
|
|
|
|
var hashArr = fixDuplicateSlashes(hash).split('/');
|
|
|
|
|
|
|
|
if (['media', 'file', 'user'].indexOf(type) === -1) {
|
|
|
|
|
|
|
|
parsed.type = 'pad';
|
|
|
|
|
|
|
|
if (hash.slice(0,1) !== '/' && hash.length >= 56) {
|
|
|
|
|
|
|
|
// Old hash
|
|
|
|
|
|
|
|
parsed.channel = hash.slice(0, 32);
|
|
|
|
|
|
|
|
parsed.key = hash.slice(32);
|
|
|
|
|
|
|
|
parsed.version = 0;
|
|
|
|
|
|
|
|
return parsed;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hashArr[1] && hashArr[1] === '1') {
|
|
|
|
|
|
|
|
parsed.version = 1;
|
|
|
|
|
|
|
|
parsed.mode = hashArr[2];
|
|
|
|
|
|
|
|
parsed.channel = hashArr[3];
|
|
|
|
|
|
|
|
parsed.key = hashArr[4].replace(/-/g, '/');
|
|
|
|
|
|
|
|
parsed.present = typeof(hashArr[5]) === "string" && hashArr[5] === 'present';
|
|
|
|
|
|
|
|
return parsed;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsed;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (['media', 'file'].indexOf(type) !== -1) {
|
|
|
|
|
|
|
|
parsed.type = 'file';
|
|
|
|
|
|
|
|
if (hashArr[1] && hashArr[1] === '1') {
|
|
|
|
|
|
|
|
parsed.version = 1;
|
|
|
|
|
|
|
|
parsed.channel = hashArr[2].replace(/-/g, '/');
|
|
|
|
|
|
|
|
parsed.key = hashArr[3].replace(/-/g, '/');
|
|
|
|
|
|
|
|
return parsed;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsed;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (['user'].indexOf(type) !== -1) {
|
|
|
|
|
|
|
|
parsed.type = 'user';
|
|
|
|
|
|
|
|
if (hashArr[1] && hashArr[1] === '1') {
|
|
|
|
|
|
|
|
parsed.version = 1;
|
|
|
|
|
|
|
|
parsed.user = hashArr[2];
|
|
|
|
|
|
|
|
parsed.pubkey = hashArr[3].replace(/-/g, '/');
|
|
|
|
|
|
|
|
return parsed;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsed;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
};
|
|
|
|
var parsePadUrl = Hash.parsePadUrl = function (href) {
|
|
|
|
var parsePadUrl = Hash.parsePadUrl = function (href) {
|
|
|
|
var patt = /^https*:\/\/([^\/]*)\/(.*?)\//i;
|
|
|
|
var patt = /^https*:\/\/([^\/]*)\/(.*?)\//i;
|
|
|
|
|
|
|
|
|
|
|
@ -43,10 +92,13 @@ define([
|
|
|
|
if (!href) { return ret; }
|
|
|
|
if (!href) { return ret; }
|
|
|
|
if (href.slice(-1) !== '/') { href += '/'; }
|
|
|
|
if (href.slice(-1) !== '/') { href += '/'; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!/^https*:\/\//.test(href)) {
|
|
|
|
if (!/^https*:\/\//.test(href)) {
|
|
|
|
var idx = href.indexOf('/#');
|
|
|
|
var idx = href.indexOf('/#');
|
|
|
|
ret.type = href.slice(1, idx);
|
|
|
|
ret.type = href.slice(1, idx);
|
|
|
|
ret.hash = href.slice(idx + 2);
|
|
|
|
ret.hash = href.slice(idx + 2);
|
|
|
|
|
|
|
|
ret.hashData = parseTypeHash(ret.type, ret.hash);
|
|
|
|
return ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -56,6 +108,7 @@ define([
|
|
|
|
return '';
|
|
|
|
return '';
|
|
|
|
});
|
|
|
|
});
|
|
|
|
ret.hash = hash.replace(/#/g, '');
|
|
|
|
ret.hash = hash.replace(/#/g, '');
|
|
|
|
|
|
|
|
ret.hashData = parseTypeHash(ret.type, ret.hash);
|
|
|
|
return ret;
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -66,16 +119,12 @@ define([
|
|
|
|
return '/' + parsed.type + '/#' + parsed.hash;
|
|
|
|
return '/' + parsed.type + '/#' + parsed.hash;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var fixDuplicateSlashes = function (s) {
|
|
|
|
|
|
|
|
return s.replace(/\/+/g, '/');
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Returns all needed keys for a realtime channel
|
|
|
|
* Returns all needed keys for a realtime channel
|
|
|
|
* - no argument: use the URL hash or create one if it doesn't exist
|
|
|
|
* - no argument: use the URL hash or create one if it doesn't exist
|
|
|
|
* - secretHash provided: use secretHash to find the keys
|
|
|
|
* - secretHash provided: use secretHash to find the keys
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
Hash.getSecrets = function (secretHash) {
|
|
|
|
Hash.getSecrets = function (type, secretHash) {
|
|
|
|
var secret = {};
|
|
|
|
var secret = {};
|
|
|
|
var generate = function () {
|
|
|
|
var generate = function () {
|
|
|
|
secret.keys = Crypto.createEditCryptor();
|
|
|
|
secret.keys = Crypto.createEditCryptor();
|
|
|
@ -85,50 +134,55 @@ define([
|
|
|
|
generate();
|
|
|
|
generate();
|
|
|
|
return secret;
|
|
|
|
return secret;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
var hash = secretHash || window.location.hash.slice(1);
|
|
|
|
var parsed;
|
|
|
|
|
|
|
|
var hash;
|
|
|
|
|
|
|
|
if (secretHash) {
|
|
|
|
|
|
|
|
if (!type) { throw new Error("getSecrets with a hash requires a type parameter"); }
|
|
|
|
|
|
|
|
parsed = parseTypeHash(type, secretHash);
|
|
|
|
|
|
|
|
hash = secretHash;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
var pHref = parsePadUrl(window.location.href);
|
|
|
|
|
|
|
|
parsed = pHref.hashData;
|
|
|
|
|
|
|
|
hash = pHref.hash;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//var parsed = parsePadUrl(window.location.href);
|
|
|
|
|
|
|
|
//var hash = secretHash || window.location.hash.slice(1);
|
|
|
|
|
|
|
|
console.log(hash, parsed);
|
|
|
|
if (hash.length === 0) {
|
|
|
|
if (hash.length === 0) {
|
|
|
|
generate();
|
|
|
|
generate();
|
|
|
|
return secret;
|
|
|
|
return secret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// old hash system : #{hexChanKey}{cryptKey}
|
|
|
|
// old hash system : #{hexChanKey}{cryptKey}
|
|
|
|
// new hash system : #/{hashVersion}/{b64ChanKey}/{cryptKey}
|
|
|
|
// new hash system : #/{hashVersion}/{b64ChanKey}/{cryptKey}
|
|
|
|
if (hash.slice(0,1) !== '/' && hash.length >= 56) {
|
|
|
|
if (parsed.version === 0) {
|
|
|
|
// Old hash
|
|
|
|
// Old hash
|
|
|
|
secret.channel = hash.slice(0, 32);
|
|
|
|
secret.channel = hash.slice(0, 32);
|
|
|
|
secret.key = hash.slice(32);
|
|
|
|
secret.key = hash.slice(32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else if (parsed.version === 1) {
|
|
|
|
// New hash
|
|
|
|
// New hash
|
|
|
|
var hashArray = fixDuplicateSlashes(hash).split('/');
|
|
|
|
if (parsed.type === "pad") {
|
|
|
|
if (hashArray.length < 4) {
|
|
|
|
secret.channel = base64ToHex(parsed.channel);
|
|
|
|
Hash.alert("Unable to parse the key");
|
|
|
|
if (parsed.mode === 'edit') {
|
|
|
|
throw new Error("Unable to parse the key");
|
|
|
|
console.log(parsed.key);
|
|
|
|
}
|
|
|
|
secret.keys = Crypto.createEditCryptor(parsed.key);
|
|
|
|
var version = hashArray[1];
|
|
|
|
secret.key = secret.keys.editKeyStr;
|
|
|
|
if (version === "1") {
|
|
|
|
|
|
|
|
var mode = hashArray[2];
|
|
|
|
|
|
|
|
if (mode === 'edit') {
|
|
|
|
|
|
|
|
secret.channel = base64ToHex(hashArray[3]);
|
|
|
|
|
|
|
|
var keys = Crypto.createEditCryptor(hashArray[4].replace(/-/g, '/'));
|
|
|
|
|
|
|
|
secret.keys = keys;
|
|
|
|
|
|
|
|
secret.key = keys.editKeyStr;
|
|
|
|
|
|
|
|
if (secret.channel.length !== 32 || secret.key.length !== 24) {
|
|
|
|
if (secret.channel.length !== 32 || secret.key.length !== 24) {
|
|
|
|
Hash.alert("The channel key and/or the encryption key is invalid");
|
|
|
|
Hash.alert("The channel key and/or the encryption key is invalid");
|
|
|
|
throw new Error("The channel key and/or the encryption key is invalid");
|
|
|
|
throw new Error("The channel key and/or the encryption key is invalid");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (mode === 'view') {
|
|
|
|
else if (parsed.mode === 'view') {
|
|
|
|
secret.channel = base64ToHex(hashArray[3]);
|
|
|
|
secret.keys = Crypto.createViewCryptor(parsed.key);
|
|
|
|
secret.keys = Crypto.createViewCryptor(hashArray[4].replace(/-/g, '/'));
|
|
|
|
|
|
|
|
if (secret.channel.length !== 32) {
|
|
|
|
if (secret.channel.length !== 32) {
|
|
|
|
Hash.alert("The channel key is invalid");
|
|
|
|
Hash.alert("The channel key is invalid");
|
|
|
|
throw new Error("The channel key is invalid");
|
|
|
|
throw new Error("The channel key is invalid");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (version === "2") {
|
|
|
|
} else if (parsed.type === "file") {
|
|
|
|
// version 2 hashes are to be used for encrypted blobs
|
|
|
|
// version 2 hashes are to be used for encrypted blobs
|
|
|
|
secret.channel = hashArray[2].replace(/-/g, '/');
|
|
|
|
secret.channel = parsed.channel;
|
|
|
|
secret.keys = { fileKeyStr: hashArray[3].replace(/-/g, '/') };
|
|
|
|
secret.keys = { fileKeyStr: parsed.key };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -175,6 +229,7 @@ Version 2
|
|
|
|
/file/#/2/K6xWU-LT9BJHCQcDCT-DcQ/ajExFODrFH4lVBwxxsrOKw/image-png
|
|
|
|
/file/#/2/K6xWU-LT9BJHCQcDCT-DcQ/ajExFODrFH4lVBwxxsrOKw/image-png
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
var parseHash = Hash.parseHash = function (hash) {
|
|
|
|
var parseHash = Hash.parseHash = function (hash) {
|
|
|
|
|
|
|
|
throw new Error('parseHash deprecated');
|
|
|
|
var parsed = {};
|
|
|
|
var parsed = {};
|
|
|
|
if (hash.slice(0,1) !== '/' && hash.length >= 56) {
|
|
|
|
if (hash.slice(0,1) !== '/' && hash.length >= 56) {
|
|
|
|
// Old hash
|
|
|
|
// Old hash
|
|
|
@ -211,9 +266,13 @@ Version 2
|
|
|
|
var p = parsePadUrl(pad.href);
|
|
|
|
var p = parsePadUrl(pad.href);
|
|
|
|
if (p.type !== parsed.type) { return; } // Not the same type
|
|
|
|
if (p.type !== parsed.type) { return; } // Not the same type
|
|
|
|
if (p.hash === parsed.hash) { return; } // Same hash, not stronger
|
|
|
|
if (p.hash === parsed.hash) { return; } // Same hash, not stronger
|
|
|
|
var pHash = parseHash(p.hash);
|
|
|
|
var pHash = p.hashData;
|
|
|
|
var parsedHash = parseHash(parsed.hash);
|
|
|
|
var parsedHash = parsed.hashData;
|
|
|
|
if (!parsedHash || !pHash) { return; }
|
|
|
|
if (!parsedHash || !pHash) { return; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// We don't have stronger/weaker versions of files or users
|
|
|
|
|
|
|
|
if (pHash.type !== 'pad' && parsedHash.type !== 'pad') { return; }
|
|
|
|
|
|
|
|
|
|
|
|
if (pHash.version !== parsedHash.version) { return; }
|
|
|
|
if (pHash.version !== parsedHash.version) { return; }
|
|
|
|
if (pHash.channel !== parsedHash.channel) { return; }
|
|
|
|
if (pHash.channel !== parsedHash.channel) { return; }
|
|
|
|
if (pHash.mode === 'view' && parsedHash.mode === 'edit') {
|
|
|
|
if (pHash.mode === 'view' && parsedHash.mode === 'edit') {
|
|
|
@ -233,9 +292,13 @@ Version 2
|
|
|
|
var p = parsePadUrl(pad.href);
|
|
|
|
var p = parsePadUrl(pad.href);
|
|
|
|
if (p.type !== parsed.type) { return; } // Not the same type
|
|
|
|
if (p.type !== parsed.type) { return; } // Not the same type
|
|
|
|
if (p.hash === parsed.hash) { return; } // Same hash, not stronger
|
|
|
|
if (p.hash === parsed.hash) { return; } // Same hash, not stronger
|
|
|
|
var pHash = parseHash(p.hash);
|
|
|
|
var pHash = p.hashData;
|
|
|
|
var parsedHash = parseHash(parsed.hash);
|
|
|
|
var parsedHash = parsed.hashData;
|
|
|
|
if (!parsedHash || !pHash) { return; }
|
|
|
|
if (!parsedHash || !pHash) { return; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// We don't have stronger/weaker versions of files or users
|
|
|
|
|
|
|
|
if (pHash.type !== 'pad' && parsedHash.type !== 'pad') { return; }
|
|
|
|
|
|
|
|
|
|
|
|
if (pHash.version !== parsedHash.version) { return; }
|
|
|
|
if (pHash.version !== parsedHash.version) { return; }
|
|
|
|
if (pHash.channel !== parsedHash.channel) { return; }
|
|
|
|
if (pHash.channel !== parsedHash.channel) { return; }
|
|
|
|
if (pHash.mode === 'edit' && parsedHash.mode === 'view') {
|
|
|
|
if (pHash.mode === 'edit' && parsedHash.mode === 'view') {
|
|
|
@ -254,8 +317,7 @@ Version 2
|
|
|
|
var parsed = Hash.parsePadUrl(href);
|
|
|
|
var parsed = Hash.parsePadUrl(href);
|
|
|
|
if (!parsed || !parsed.hash) { return; }
|
|
|
|
if (!parsed || !parsed.hash) { return; }
|
|
|
|
|
|
|
|
|
|
|
|
parsed = Hash.parseHash(parsed.hash);
|
|
|
|
parsed = parsed.hashData;
|
|
|
|
|
|
|
|
|
|
|
|
if (parsed.version === 0) {
|
|
|
|
if (parsed.version === 0) {
|
|
|
|
return parsed.channel;
|
|
|
|
return parsed.channel;
|
|
|
|
} else if (parsed.version !== 1 && parsed.version !== 2) {
|
|
|
|
} else if (parsed.version !== 1 && parsed.version !== 2) {
|
|
|
|