Skip to content

Commit

Permalink
add exclude option to restrict passwords (#15)
Browse files Browse the repository at this point in the history
This commit adds an option, `exclude`, through which one can pass a
string of characters that will be removed from the password generation
pool.

For example, to remove some similar characters (without
excludeSimilarCharacters), you can now pass { exclude: 'ilLI|' }.
  • Loading branch information
sketchthat authored and brendanashworth committed Dec 27, 2016
1 parent d16c953 commit 38d4ae0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ Any of these can be passed into the options object for each function.
| symbols | Boolean, put symbols in password. | false |
| uppercase | Boolean, use uppercase letters in password. | true |
| excludeSimilarCharacters | Boolean, exclude similar chars, like 'i' and 'l'. | false |
| exclude | String, characters to be excluded from password. | '' |
| strict | Boolean, password must include at least one character from each pool. | false |
8 changes: 8 additions & 0 deletions src/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ self.generate = function(options) {
if (!options.hasOwnProperty('length')) options.length = 10;
if (!options.hasOwnProperty('numbers')) options.numbers = false;
if (!options.hasOwnProperty('symbols')) options.symbols = false;
if (!options.hasOwnProperty('exclude')) options.exclude = '';
if (!options.hasOwnProperty('uppercase')) options.uppercase = true;
if (!options.hasOwnProperty('excludeSimilarCharacters')) options.excludeSimilarCharacters = false;
if (!options.hasOwnProperty('strict')) options.strict = false;
Expand All @@ -84,11 +85,18 @@ self.generate = function(options) {
if (options.symbols) {
pool += symbols;
}

// similar characters
if (options.excludeSimilarCharacters) {
pool = pool.replace(similarCharacters, '');
}

// excludes characters from the pool
var i = options.exclude.length;
while (i--) {
pool = pool.replace(options.exclude[i], '');
}

var password = generate(options, pool);

return password;
Expand Down
10 changes: 10 additions & 0 deletions test/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ describe('generate-password', function() {
assert.equal(passwords.length, amountToGenerate);
});

it('should generate strict random sequence that avoids all excluded characters', function() {
var passwords = generator.generateMultiple(amountToGenerate, {length: 4, strict: true, symbols: true, exclude: 'abcdefg+_-=}{[]|:;"/?.><,`~'});

passwords.forEach(function(password) {
assert.match(password, /[!@#$%^&*()]/, 'password uses normal symbols');
assert.notMatch(password, /[abcdefg+_\-=}{[\]|:;"/?.><,`~]/, 'password avoids excluded characters from the full pool');
});
assert.equal(passwords.length, amountToGenerate);
});

it('should throw an error if rules don\'t correlate with length', function() {
assert.throws(function() {
generator.generate({length: 2, strict: true, symbols: true, numbers: true});
Expand Down

0 comments on commit 38d4ae0

Please sign in to comment.