Edentity Web Systems
You solve creative. We crack code.
/Header/WhoWeAre.jpg
 

07/16/2009 Password Generation Class
Posted by Alexandru Cibotari

Here is a class that allows generating passwords. The result is customizable.  It’s possible to specify:

1.       Password length

2.       Allow/Don’t allow upper case characters

3.       Allow/Don’t allow lower case characters

4.       Allow/Don’t allow numbers

5.       Allow/Don’t allow symbols

Source code:

        public static string GeneratePassword(int passworLength, bool allowLowerCaseCharacters, bool allowUppercaseCharacters, bool allowNumbers, bool allowSymbols)

        {

            string[] Lower = new string[26] { "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" };

            string[] Upper = new string[26] { "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" };

            string[] Number = new string[10] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };

            string[] Symbol = new string[] { "!", "@", "#", "$", "%", "&", "*", "?" };

 

            string password = "";

 

            for (int i = 0; i < passworLength; i++)

            {

                Random rand = new System.Random(Guid.NewGuid().GetHashCode());

                int rplace = rand.Next(0, 4);

                int rplace2 = 0;

                while (!IsChecked(rplace, allowLowerCaseCharacters, allowUppercaseCharacters, allowNumbers, allowSymbols))

                {

                    rplace = rand.Next(0, 4);

                }

                switch (rplace)

                {

                    case 0:

                        rplace2 = rand.Next(0, 26);

                        password += Lower[rplace2];

                        break;

                    case 1:

                        rplace2 = rand.Next(0, 26);

                        password += Upper[rplace2];

                        break;

                    case 2:

                        rplace2 = rand.Next(0, 10);

                        password += Number[rplace2];

                        break;

                    case 3:

                        rplace2 = rand.Next(0, 8);

                        password += Symbol[rplace2];

                        break;

                    default:

                        password += "";

                        break;

                }

            }

 

            return password;

        }

 

        private static bool IsChecked(int rplace, bool allowLowerCaseCharacters, bool allowUppercaseCharacters, bool allowNumbers, bool allowSymbols)

        {

            switch (rplace)

            {

                case 0:

                    return allowLowerCaseCharacters;

                case 1:

                    return allowUppercaseCharacters;

                case 2:

                    return allowNumbers;

                case 3:

                    return allowSymbols;

                default:

                    return true;

            }

        }

    }

« Back to Blog Main Page |

0 Comment(s)

Leave Comment

Name:
Comment:  
 
Security Check: 
 
 
Edentity Web Systems Inc.
Suite 201
40 Spadina Ave.
Toronto ON
M5V 2H8
Tel:
416.591.2500
Fax:
416.352.5270
Home | Who We Are | What We Do | Clients & Brands | Careers | Contact

Copyright © 2010 Edentity Web Systems Inc. Privacy Policy