Click here to Skip to main content
Click here to Skip to main content

Simple Password Generator

By , 18 Dec 2013
Rate this:
Please Sign up or sign in to vote.

Introduction 

Sometimes you need to create words from a given alphabet, so I'd like to share a very simple solution. The solution is scalable, so you can add your own alphabet. Let's take a look at the code.

Using the Code

The code consists of several classes:

  • AlphabetDictionaries - predefined alphabets
  • AlphabetWordGenerator - generate word accordingly alphabet

public static class AlphabetDictionaries
{
    public const string EnglishLowerCase = "abcdefghijklmnopqrstuvwxyz";
    public const string EnglishUpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public const string Numeric = "0123456789";
    public const string SpecialSymbols = "`~!@#$%^&*()-_=+[]{}\\|;:'\",<.>/?";
}   

Here's main and only one class - AlphabetWordGenerator

public sealed class AlphabetWordGenerator : IWordGenerator
{
    private readonly string _alphabet;
    private readonly RNGCryptoServiceProvider _provider = new RNGCryptoServiceProvider();

    public AlphabetWordGenerator(ICollection<string> alphabets)
    {
        if (alphabets == null || alphabets.Count == 0)
        {
            throw new ArgumentException("Alphabets is null or empty");
        }
        if (alphabets.Any(string.IsNullOrWhiteSpace))
        {
            throw new ArgumentException("Alphabet should not be ampty");
        }
        _alphabet = string.Join(string.Empty, alphabets);
    }

    public AlphabetWordGenerator(string alphabet)
        : this(new[] { alphabet })
    {
    }

    public string Generate(int wordLength)
    {
        if (wordLength < 1)
        {
            throw new IndexOutOfRangeException("wordLength is out of range");
        }
        return DoGenerate(wordLength);
    }

    private string DoGenerate(int wordLength)
    {
        var repository = new List<char>(wordLength);
        var data = new byte[wordLength];
        _provider.GetBytes(data);
        foreach (byte item in data)
        {
            int index = Convert.ToInt32(item)%_alphabet.Length;
            char symbol = _alphabet[index];
            repository.Add(symbol);
        }
        return string.Join(string.Empty, repository);
    }
} 

Let's create a word with lowercase English letters:

var generator = new AlphabetWordGenerator(AlphabetDictionaries.EnglishLowerCase);
                    
var password = generator.Generate(10); 

Result password is equal to uddfxjfbxp 

Another example with several alphabets:

var alphabets = new List<string>
{
    AlphabetDictionaries.EnglishLowerCase,
    AlphabetDictionaries.EnglishUpperCase,
    AlphabetDictionaries.Numeric,
    AlphabetDictionaries.SpecialSymbols,
};

var generator = new AlphabetWordGenerator(alphabets);
                    
var password = generator.Generate(40);    

Result password is equal to [3xH(kzizo@51m@q}qpIYq[CgUBbjKf^3E@JF^T;

History

  • 26th July, 2012: Initial version
  • 20th December, 2013: Length's been moved to generate method

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sergey Morenko
Software Developer (Senior)
Russian Federation Russian Federation
B.Sc. in Computer Science.
Follow on   Twitter   LinkedIn

Comments and Discussions

 
SuggestionDoes what it says PinmemberJohn Brett19-Dec-13 4:30 
GeneralRe: Does what it says [modified] PinprofessionalSergey Morenko20-Dec-13 9:06 
Question[My vote of 2] 2 Easier Way PinmemberKevin Marois26-Jul-12 7:01 
AnswerRe: [My vote of 2] 2 Easier Way [modified] PinmemberGSerjo26-Jul-12 7:18 
GeneralRe: [My vote of 2] 2 Easier Way PinmemberKevin Marois26-Jul-12 7:27 
GeneralRe: [My vote of 2] 2 Easier Way [modified] PinmemberGSerjo26-Jul-12 7:40 
1) Could you please read carefully, I've said when require generate word based on an alphabet, I don't create random password.
 
2) You're using wrong method for password generating. .Net has Membership.GeneratePassword from System.Web.Security namespace. GetRandomFileName - generate File Name not password.

-- modified 8-Sep-13 11:04am.
GeneralRe: [My vote of 2] 2 Easier Way PinmemberCaner Korkmaz26-Jul-12 8:33 
GeneralMy vote of 5 Pinmemberzhanazhan25-Jul-12 17:50 
GeneralRe: My vote of 5 PinmemberGSerjo25-Jul-12 19:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 18 Dec 2013
Article Copyright 2012 by Sergey Morenko
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid