55 lines
1.7 KiB
JavaScript
55 lines
1.7 KiB
JavaScript
5 years ago
|
(function () {
|
||
5 years ago
|
var factory = function (Util, Nacl, Scrypt) {
|
||
5 years ago
|
var Invite = {};
|
||
|
|
||
5 years ago
|
Invite.deriveSeeds = function (safeSeed) {
|
||
5 years ago
|
// take the hash of the provided seed
|
||
5 years ago
|
var seed = safeSeed.replace(/\-/g, '/');
|
||
5 years ago
|
var u8_seed = Nacl.hash(Nacl.util.decodeBase64(seed));
|
||
5 years ago
|
|
||
5 years ago
|
// hash the first half again for scrypt's input
|
||
|
var subseed1 = Nacl.hash(u8_seed.subarray(0, 32));
|
||
|
// hash the remainder for the invite content
|
||
|
var subseed2 = Nacl.hash(u8_seed.subarray(32));
|
||
5 years ago
|
|
||
5 years ago
|
return {
|
||
|
scrypt: Nacl.util.encodeBase64(subseed1),
|
||
|
preview: Nacl.util.encodeBase64(subseed2),
|
||
|
};
|
||
|
};
|
||
5 years ago
|
|
||
5 years ago
|
Invite.deriveSalt = function (password, instance_salt) {
|
||
|
return (password || '') + (instance_salt || '');
|
||
5 years ago
|
};
|
||
|
|
||
5 years ago
|
// seed => bytes64
|
||
|
Invite.deriveBytes = function (scrypt_seed, salt, cb) {
|
||
|
Scrypt(scrypt_seed,
|
||
|
salt,
|
||
|
8, // memoryCost (n)
|
||
|
1024, // block size parameter (r)
|
||
|
192, // dkLen
|
||
|
200, // interruptStep
|
||
|
cb,
|
||
|
'base64'); // format, could be 'base64'
|
||
|
};
|
||
5 years ago
|
|
||
5 years ago
|
return Invite;
|
||
|
};
|
||
|
if (typeof(module) !== 'undefined' && module.exports) {
|
||
|
module.exports = factory(
|
||
5 years ago
|
require("../common-util"),
|
||
5 years ago
|
require("tweetnacl/nacl-fast"),
|
||
5 years ago
|
require("scrypt-async")
|
||
5 years ago
|
);
|
||
|
} else if ((typeof(define) !== 'undefined' && define !== null) && (define.amd !== null)) {
|
||
|
define([
|
||
5 years ago
|
'/common/common-util.js',
|
||
5 years ago
|
'/bower_components/tweetnacl/nacl-fast.min.js',
|
||
5 years ago
|
'/bower_components/scrypt-async/scrypt-async.min.js',
|
||
5 years ago
|
], function (Util) {
|
||
|
return factory(Util, window.nacl, window.scrypt);
|
||
5 years ago
|
});
|
||
|
}
|
||
|
}());
|