Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating random password in bulk

I use this source code for generating random passwords :

public string GetRandomPasswordUsingGUID(int length)
{
    // Get the GUID
    string guidResult = System.Guid.NewGuid().ToString();

    // Remove the hyphens
    guidResult = guidResult.Replace("-", string.Empty);

    // Make sure length is valid
    if (length <= 0 || length > guidResult.Length)
        throw new ArgumentException("Length must be between 1 and " + guidResult.Length);

    // Return the first length bytes
    return guidResult.Substring(0, length).ToUpper();
}

It works fine when you invoke the method ,But not in "for" loop statement .

At this case it generate some repeated password which is wrong .

for example like this :

A4MNB597D7
AMGJCCC902
AWJ80CF6HX
A78EDJECIW
A78EDJECIW
A78EDJECIW
A78EDJECIW
A78EDJECIW
A2LYJCH23N
A2LYJCH23N

How can i create random password in "For" loop statement ?

like image 859
Mostafa Avatar asked Dec 09 '22 08:12

Mostafa


1 Answers

GUIDs are not random, they are only unique (within a single system). Even a random number generator has limits on it, the minimum and maximum values it will return, and being truly random means you could get the same result over and over again, you just can't tell.

Are you sure you mean random, as opposed to strong?

XKCDhttp://xkcd.com/221/

Ok, so now we have some idea of what you want 500 -1000 unique passwords. I'd question the need for uniqueness, as I would presume that they're for a user account, however ... (entered without VS handy)

List<string> passwords = new List<string>();

while (passwords.Length < 1000)
{
    string generated = System.Web.Security.Membership.GeneratePassword(
                           10, // maximum length
                           3)  // number of non-ASCII characters.
    if (!passwords.Contains(generated))
        passwords.Add(generated);
}

And then you'll have a list of 1000 unique passwords, which have a maximum of 10 characters, and 3 non-ASCII characters.

like image 105
blowdart Avatar answered Dec 11 '22 22:12

blowdart