Przeglądaj źródła

Code Review algorithm v2

The algorithm looks good to me. I just changed some names and added a few semi-colons.
pull/342/head
Erik Mulder 8 lat temu
committed by GitHub
rodzic
commit
14468db122
1 zmienionych plików z 15 dodań i 16 usunięć
  1. +15
    -16
      src/v2.js

+ 15
- 16
src/v2.js Wyświetl plik

@@ -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);
}

Ładowanie…
Anuluj
Zapisz