Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions PasswordGenerator.Tests/OmitCharactersTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Text;

namespace PasswordGenerator.Tests
{
public class OmitCharactersTests
{
[Test]
public void TestOmit_IlO0B8()
{
var charactersToOmit = "IlO0B8";
var pwd = new Password(true, true, true, true, charactersToOmit, 8, 100);
var result = pwd.NextGroup(1000);

foreach (var password in result)
{
var index = password.IndexOfAny(charactersToOmit.ToCharArray());
Assert.AreEqual(-1, index);
}
}

[Test]
public void TestNotOmit_IlO0B8()
{
var pwd = new Password(true, true, true, true, null, 8, 100);
var result = pwd.NextGroup(1000);

var charactersToOmit = "IlO0B8";
long hasOmitedCharacters = 0;
foreach (var password in result)
{
var index = password.IndexOfAny(charactersToOmit.ToCharArray());
if (index >= 0)
hasOmitedCharacters++;
}

Assert.IsTrue(hasOmitedCharacters > 0);
}
}
}
1 change: 1 addition & 0 deletions PasswordGenerator/IPasswordSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ public interface IPasswordSettings
IPasswordSettings AddSpecial();
IPasswordSettings AddSpecial(string specialCharactersToAdd);
string SpecialCharacters { get; set; }
string OmitCharacters { get; }
}
}
21 changes: 16 additions & 5 deletions PasswordGenerator/Password.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ public class Password : IPassword
private const bool DefaultIncludeUppercase = true;
private const bool DefaultIncludeNumeric = true;
private const bool DefaultIncludeSpecial = true;
private const string DefaultOmitCharacters = null;
private static RNGCryptoServiceProvider _rng;

public Password()
{
Settings = new PasswordSettings(DefaultIncludeLowercase, DefaultIncludeUppercase,
DefaultIncludeNumeric, DefaultIncludeSpecial, DefaultPasswordLength, DefaultMaxPasswordAttempts,
true);
true, DefaultOmitCharacters);

_rng = new RNGCryptoServiceProvider();
}
Expand All @@ -38,15 +39,16 @@ public Password(IPasswordSettings settings)
public Password(int passwordLength)
{
Settings = new PasswordSettings(DefaultIncludeLowercase, DefaultIncludeUppercase,
DefaultIncludeNumeric, DefaultIncludeSpecial, passwordLength, DefaultMaxPasswordAttempts, true);
DefaultIncludeNumeric, DefaultIncludeSpecial, passwordLength, DefaultMaxPasswordAttempts, true,
DefaultOmitCharacters);

_rng = new RNGCryptoServiceProvider();
}

public Password(bool includeLowercase, bool includeUppercase, bool includeNumeric, bool includeSpecial)
{
Settings = new PasswordSettings(includeLowercase, includeUppercase, includeNumeric,
includeSpecial, DefaultPasswordLength, DefaultMaxPasswordAttempts, false);
includeSpecial, DefaultPasswordLength, DefaultMaxPasswordAttempts, false, DefaultOmitCharacters);

_rng = new RNGCryptoServiceProvider();
}
Expand All @@ -55,7 +57,7 @@ public Password(bool includeLowercase, bool includeUppercase, bool includeNumeri
int passwordLength)
{
Settings = new PasswordSettings(includeLowercase, includeUppercase, includeNumeric,
includeSpecial, passwordLength, DefaultMaxPasswordAttempts, false);
includeSpecial, passwordLength, DefaultMaxPasswordAttempts, false, DefaultOmitCharacters);

_rng = new RNGCryptoServiceProvider();
}
Expand All @@ -64,7 +66,16 @@ public Password(bool includeLowercase, bool includeUppercase, bool includeNumeri
int passwordLength, int maximumAttempts)
{
Settings = new PasswordSettings(includeLowercase, includeUppercase, includeNumeric,
includeSpecial, passwordLength, maximumAttempts, false);
includeSpecial, passwordLength, maximumAttempts, false, DefaultOmitCharacters);

_rng = new RNGCryptoServiceProvider();
}

public Password(bool includeLowercase, bool includeUppercase, bool includeNumeric, bool includeSpecial,
string omitCharacters ,int passwordLength, int maximumAttempts)
{
Settings = new PasswordSettings(includeLowercase, includeUppercase, includeNumeric,
includeSpecial, passwordLength, maximumAttempts, false, omitCharacters);

_rng = new RNGCryptoServiceProvider();
}
Expand Down
2 changes: 1 addition & 1 deletion PasswordGenerator/PasswordGeneratorSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class PasswordGeneratorSettings : PasswordSettings
public PasswordGeneratorSettings(bool includeLowercase, bool includeUppercase, bool includeNumeric,
bool includeSpecial, int passwordLength, int maximumAttempts, bool usingDefaults)
: base(includeLowercase, includeUppercase, includeNumeric,
includeSpecial, passwordLength, maximumAttempts, usingDefaults)
includeSpecial, passwordLength, maximumAttempts, usingDefaults, null)
{
}
}
Expand Down
14 changes: 13 additions & 1 deletion PasswordGenerator/PasswordSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ public class PasswordSettings : IPasswordSettings
private const int DefaultMinPasswordLength = 4;
private const int DefaultMaxPasswordLength = 256;
public string SpecialCharacters { get; set; }
public string OmitCharacters { get; private set; }

public PasswordSettings(bool includeLowercase, bool includeUppercase, bool includeNumeric, bool includeSpecial,
int passwordLength, int maximumAttempts, bool usingDefaults)
int passwordLength, int maximumAttempts, bool usingDefaults, string omitCharacters)
{
IncludeLowercase = includeLowercase;
IncludeUppercase = includeUppercase;
Expand All @@ -28,6 +29,8 @@ public PasswordSettings(bool includeLowercase, bool includeUppercase, bool inclu
MaximumLength = DefaultMaxPasswordLength;
UsingDefaults = usingDefaults;
SpecialCharacters = DefaultSpecialCharacters;
OmitCharacters = omitCharacters;

CharacterSet = BuildCharacterSet(includeLowercase, includeUppercase, includeNumeric, includeSpecial);
}

Expand Down Expand Up @@ -96,9 +99,18 @@ private string BuildCharacterSet(bool includeLowercase, bool includeUppercase, b
if (includeNumeric) characterSet.Append(NumericCharacters);

if (includeSpecial) characterSet.Append(SpecialCharacters);

if (!string.IsNullOrEmpty(OmitCharacters)) RemoveOmittedCharacters(characterSet);

return characterSet.ToString();
}

private void RemoveOmittedCharacters(StringBuilder characterSet)
{
foreach (var character in OmitCharacters)
characterSet.Replace(character.ToString(), string.Empty);
}

private void StopUsingDefaults()
{
if (!UsingDefaults) return;
Expand Down