Add common password requirements option to passphrase generator (#43)
* Added commont password requirements option to passphrase generator * Processed PR comments
This commit is contained in:
parent
f4f90f83cd
commit
a017f72506
|
@ -25,6 +25,8 @@ const DefaultOptions = {
|
||||||
type: 'password',
|
type: 'password',
|
||||||
numWords: 3,
|
numWords: 3,
|
||||||
wordSeparator: '-',
|
wordSeparator: '-',
|
||||||
|
capitalize: false,
|
||||||
|
includeNumber: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
const Keys = {
|
const Keys = {
|
||||||
|
@ -171,12 +173,29 @@ export class PasswordGenerationService implements PasswordGenerationServiceAbstr
|
||||||
o.wordSeparator = ' ';
|
o.wordSeparator = ' ';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (o.capitalize == null) {
|
||||||
|
o.addCommonRequirements = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (o.includeNumber == null) {
|
||||||
|
o.includeNumber = false;
|
||||||
|
}
|
||||||
|
|
||||||
const listLength = EEFLongWordList.length - 1;
|
const listLength = EEFLongWordList.length - 1;
|
||||||
const wordList = new Array(o.numWords);
|
let wordList = new Array(o.numWords);
|
||||||
for (let i = 0; i < o.numWords; i++) {
|
for (let i = 0; i < o.numWords; i++) {
|
||||||
const wordIndex = await this.cryptoService.randomNumber(0, listLength);
|
const wordIndex = await this.cryptoService.randomNumber(0, listLength);
|
||||||
|
if (o.capitalize) {
|
||||||
|
wordList[i] = this.capitalize(EEFLongWordList[wordIndex]);
|
||||||
|
} else {
|
||||||
wordList[i] = EEFLongWordList[wordIndex];
|
wordList[i] = EEFLongWordList[wordIndex];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (o.includeNumber) {
|
||||||
|
return await this.insertNumber(wordList.join(o.wordSeparator));
|
||||||
|
}
|
||||||
|
|
||||||
return wordList.join(o.wordSeparator);
|
return wordList.join(o.wordSeparator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -256,6 +275,16 @@ export class PasswordGenerationService implements PasswordGenerationServiceAbstr
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private capitalize(str: string) {
|
||||||
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async insertNumber(word: string) {
|
||||||
|
const charIndex = await this.cryptoService.randomNumber(0, word.length - 1);
|
||||||
|
const addedNumber = await this.cryptoService.randomNumber(0, 9);
|
||||||
|
return word.substring(0, charIndex) + addedNumber + word.substring(charIndex, word.length);
|
||||||
|
}
|
||||||
|
|
||||||
private async encryptHistory(history: GeneratedPasswordHistory[]): Promise<GeneratedPasswordHistory[]> {
|
private async encryptHistory(history: GeneratedPasswordHistory[]): Promise<GeneratedPasswordHistory[]> {
|
||||||
if (history == null || history.length === 0) {
|
if (history == null || history.length === 0) {
|
||||||
return Promise.resolve([]);
|
return Promise.resolve([]);
|
||||||
|
|
Loading…
Reference in New Issue