To use the PasswordGeneratorFactory

  • Homepage
  • Software
  • Thoughts and Ideas
  • Photos and Art
  • Contact

In the last examples, the particular password generators are always created directly in the code. But what do you do, when you decide later, that you want to generate pronouncable passwords instead of random passwords? To change that, you need to change the code and recompile the project.

The Password Generator Library provides a PasswordGeneratorFactory, that you can use to create the password generator. Which password generator will be created, is defined in the *.config file.

By the property Instance of the class PasswordGeneratorFactory you get access to the factory. The method Create creates an instance of a password generator and returns it. This method expects the password options for the password generator as parameter.

using CAM.PasswordGeneratorLibrary;
...
PasswordGenerator pwdGen = 
  PasswordGeneratorFactory.Instance.Create(PasswordOptions.AllCharacters);

Console.WriteLine(pwdGen.Generate(8));

To change the password generator, you have two possibilities. A possibility is to change the *.config file. In the *.config file is an element called "PasswordGeneratorDefaults", that is a child element of "CAM.PasswordGeneratorLibrary". Here you need to set the value of the property "PasswordGenerator" to another valid value. By default, the values "RandomPasswordGenerator" and "PronounceablePasswordGenerator" are possible.

<CAM.PasswordGeneratorLibrary>
 ...
 <PasswordGeneratorDefaults PasswordGenerator="RandomPasswordGenerator" 
  RandomNumberGenerator="cryptographic" />
 ...
</CAM.PasswordGeneratorLibrary>

The other possibility is to use the API. The PasswordGeneratorFactory provides the property PasswordGenerator, to which you can assign the same values as in the *.config file. It is important that you change this property before you create the password generator.

using CAM.PasswordGeneratorLibrary;
...

PasswordGeneratorFactory.Instance.PasswordGenerator = "RandomPasswordGenerator";

PasswordGenerator pwdGen = 
  PasswordGeneratorFactory.Instance.Create(PasswordOptions.AllCharacters);

Console.WriteLine(pwdGen.Generate(8));
© Christoph A. Müller - Contact/Imprint - Privacy