Friday, April 10, 2009

A Simple password generator

Here is a simple password generator.
there is nothing special in this code, i have just used the Random class in C# to generate random number and some range of character from Character Array, below code illustrate is more easily.

public string GetRandomPassword(int numChars , int seed)
{
string[] chars = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"0", "1", "2" ,"3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N","O","P", "Q", "R", "S",
"T", "U", "V", "W", "X", "Y", "Z" };

Random rnd = new Random(seed);
string random = string.Empty;
for(int i = 0 ; i < numChars ; i++)
{
random += chars[rnd.Next(0 , 62)];
}
return random;
}

Call to the Method above

Random random = new Random();
string Password = GetRandomPassword(15 , random.Next(100000))); //15 character long password