From 14468db1225577c436cf868314a108c7d9b65285 Mon Sep 17 00:00:00 2001 From: Erik Mulder Date: Mon, 21 Nov 2016 12:42:38 +0100 Subject: [PATCH] Code Review algorithm v2 The algorithm looks good to me. I just changed some names and added a few semi-colons. --- src/v2.js | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/v2.js b/src/v2.js index 5f2d014..b85ac38 100644 --- a/src/v2.js +++ b/src/v2.js @@ -15,8 +15,9 @@ module.exports = { }; function generatePassword(site, login, masterPassword, passwordProfile) { - return calcEntropy(site, login, masterPassword, passwordProfile).then(function (entropy) { - return renderPassword(entropy, passwordProfile) + var _passwordProfile = objectAssign({}, defaultPasswordProfile, passwordProfile); + return calcEntropy(site, login, masterPassword, _passwordProfile).then(function (entropy) { + return renderPassword(entropy, _passwordProfile); }); } @@ -26,17 +27,16 @@ var defaultPasswordProfile = { digits: true, symbols: true, length: 16, - counter: 1, + index: 1, iterations: 100000, keylen: 32, digest: 'sha256' }; function calcEntropy(site, login, masterPassword, passwordProfile) { - var _passwordProfile = objectAssign({}, defaultPasswordProfile, passwordProfile); return new Promise(function (resolve, reject) { - var salt = site + login + _passwordProfile.counter.toString(16); - pbkdf2.pbkdf2(masterPassword, salt, _passwordProfile.iterations, _passwordProfile.keylen, _passwordProfile.digest, function (error, key) { + var salt = site + login + passwordProfile.index.toString(16); + pbkdf2.pbkdf2(masterPassword, salt, passwordProfile.iterations, passwordProfile.keylen, passwordProfile.digest, function (error, key) { if (error) { reject('error in pbkdf2'); } else { @@ -46,7 +46,7 @@ function calcEntropy(site, login, masterPassword, passwordProfile) { }); } -var subsetOfChars = { +var characterSubsets = { lowercase: 'abcdefghijklmnopqrstuvwxyz', uppercase: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', digits: '0123456789', @@ -55,22 +55,22 @@ var subsetOfChars = { function getSetOfCharacters(rules) { if (typeof rules === 'undefined') { - return subsetOfChars.lowercase + subsetOfChars.uppercase + subsetOfChars.digits + subsetOfChars.symbols; + return characterSubsets.lowercase + characterSubsets.uppercase + characterSubsets.digits + characterSubsets.symbols; } var setOfChars = ''; rules.forEach(function (rule) { - setOfChars += subsetOfChars[rule] + setOfChars += characterSubsets[rule]; }); return setOfChars; } function consumeEntropy(generatedPassword, quotient, setOfCharacters, maxLength) { if (generatedPassword.length >= maxLength) { - return {value: generatedPassword, entropy: quotient} + return {value: generatedPassword, entropy: quotient}; } var longDivision = quotient.divmod(setOfCharacters.length); generatedPassword += setOfCharacters[longDivision.remainder]; - return consumeEntropy(generatedPassword, longDivision.quotient, setOfCharacters, maxLength) + return consumeEntropy(generatedPassword, longDivision.quotient, setOfCharacters, maxLength); } function insertStringPseudoRandomly(generatedPassword, entropy, string) { @@ -85,24 +85,23 @@ function insertStringPseudoRandomly(generatedPassword, entropy, string) { function getOneCharPerRule(entropy, rules) { var oneCharPerRules = ''; rules.forEach(function (rule) { - var password = consumeEntropy('', entropy, subsetOfChars[rule], 1); + var password = consumeEntropy('', entropy, characterSubsets[rule], 1); oneCharPerRules += password.value; entropy = password.entropy; }); return {value: oneCharPerRules, entropy: entropy}; } -function validRules(passwordProfile) { +function getConfiguredRules(passwordProfile) { return ['lowercase', 'uppercase', 'digits', 'symbols'].filter(function (rule) { return passwordProfile[rule]; }); } function renderPassword(entropy, passwordProfile) { - var _passwordProfile = objectAssign({}, defaultPasswordProfile, passwordProfile); - var rules = validRules(_passwordProfile); + var rules = getConfiguredRules(passwordProfile); var setOfCharacters = getSetOfCharacters(rules); - var password = consumeEntropy('', bigInt(entropy, 16), setOfCharacters, _passwordProfile.length - rules.length); + var password = consumeEntropy('', bigInt(entropy, 16), setOfCharacters, passwordProfile.length - rules.length); var charactersToAdd = getOneCharPerRule(password.entropy, rules); return insertStringPseudoRandomly(password.value, charactersToAdd.entropy, charactersToAdd.value); }