A simple way to generate random password

I took que from Ambuj’s post to generate a simple password which is more random. It simply takes random specified number of characters from a guid value. Following is the code.

static string GenerateRandomPassword(int numberOfCharactersInPassword)
           if (numberOfCharactersInPassword <= 0 || numberOfCharactersInPassword > 32)
               throw new ArgumentException(“A password of only length 1 to 32 can be generated.”);
           string guidWithoutDashes = Guid.NewGuid().ToString(“n”);
           //Console.WriteLine(“Guid without dashes :- ” + guidWithoutDashes);
           var chars = new char[numberOfCharactersInPassword];
           var random = new Random();
           for (int i = 0; i < chars.Length; i++)
               chars[i] = guidWithoutDashes[random.Next(guidWithoutDashes.Length)];
           //Console.WriteLine(“Random password :- ” + new string(chars));
           return new string(chars);

The complete project to test quickly can be downloaded from here.

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Random Thoughts

The World as I see it

Simple Programmer

Making The Complex Simple

Ionic Solutions

Random thoughts on software construction, design patterns and optimization.

Long (Way) Off

A tragic's view from the cricket hinterlands

%d bloggers like this: