makePassword function
Create and display on terminal one or more password or passphrase.
arguments
are arguments from command line
displayEntropy
Allow to display bits of entropy
useColor
Allow to to use AnsiPen
to use color on terminal
Implementation
int makePassword(List<String> arguments,
{bool displayEntropy = false, bool useColor = true}) {
var collection = getCollection(arguments);
var size = getSize(arguments);
var number = getNumber(arguments);
var exclude = getExclude(arguments);
var itemsFromFile = getItemsFromFile(arguments);
late PasswordService generator;
switch (collection) {
case Collection.latin:
generator = PasswordService.latinBase();
break;
case Collection.french:
generator = PasswordService.latinFrench();
break;
case Collection.german:
generator = PasswordService.latinGerman();
break;
case Collection.italian:
generator = PasswordService.latinItalian();
break;
case Collection.spanish:
generator = PasswordService.latinSpanish();
break;
case Collection.eff:
generator = PasswordService.effLargeListWords();
break;
default:
// If cli option -f isn't used. It set by default latinBase
generator = itemsFromFile != null && itemsFromFile.isNotEmpty
? PasswordService()
: PasswordService.latinBase();
}
// Test if cli option -f. If yes read from file custom collection
var hasCustomItems = itemsFromFile != null && itemsFromFile.isNotEmpty;
if (hasCustomItems) {
itemsFromFile.forEach((key, collection) =>
generator.addCollectionOfPasswordItems(key, collection));
}
// Create and display password,passphrase
late Password password;
for (var i = 0; i < number; i++) {
password = generator.generatePassword(length: size, excludeItems: exclude);
displayPassword(password, useColor: !hasCustomItems && useColor);
}
if (displayEntropy) {
print('\nBits of entropy ${password.entropy.toInt()}');
}
return exitOk;
}