tags:

views:

21

answers:

1

Hi I am using BasePage.GeneratePassword(6, 10) to generate the unqiue password for the user. but I have notice that sometimes( i got 5 out of 150 users) same password is generated and i use this as unqiueId for some purpose.

How reliable is this function?

I am not setting any other property like this 'numberOfNonAlphanumericCharacters'.

Any guidance will be of great help?

Thanks Harshit

+1  A: 

it uses the RNGCryptoServiceProvider class to get a "random" sequence of bytes that are then used to generate the password. Something like the following

public static string GeneratePassword(int length, int numberOfNonAlphanumericCharacters)
{
    string str;
    int num;
    char[] punctuations= @"!@@$%^^*()_-+=[{]};:>|./?".ToCharArray();

    // removed checks for brevity //

    while(true)
    {
        byte[] data = new byte[length];
        char[] chArray = new char[length];
        int num2 = 0;
        new RNGCryptoServiceProvider().GetBytes(data);
        for (int i = 0; i < length; i++)
        {
            int num4 = data[i] % 0x57;
            if (num4 < 10)
            {
                // generate characters 0 -9
                chArray[i] = (char) (0x30 + num4);
            }
            else if (num4 < 0x24)
            {
                // generate characters A-Z
                chArray[i] = (char) ((0x41 + num4) - 10);
            }
            else if (num4 < 0x3e)
            {
                // generate characters a-z
                chArray[i] = (char) ((0x61 + num4) - 0x24);
            }
            else
            {
                // get a non alphanumeric character from the punctuations array
                chArray[i] = punctuations[num4 - 0x3e];
                num2++;
            }
        }
        if (num2 < numberOfNonAlphanumericCharacters)
        {
            Random random = new Random();
            for (int j = 0; j < (numberOfNonAlphanumericCharacters - num2); j++)
            {
                int num6;
                do
                {
                    num6 = random.Next(0, length);
                }
                while (!char.IsLetterOrDigit(chArray[num6]));
                chArray[num6] = punctuations[random.Next(0, punctuations.Length)];
            }
        }
        str = new string(chArray);
    }
    return str;
}
Russ Cam