Click here to Skip to main content
15,887,214 members
Articles / Programming Languages / C#
Article

Password Generator

Rate me:
Please Sign up or sign in to vote.
1.39/5 (13 votes)
13 Dec 20051 min read 29.8K   506   15   6
This program generates random passwords (and SHA1 hash)

Sample Image - PasswordGenerator.jpg

Introduction

This program generates random passwords. They may include chars from "A" to "Z", "a" to "z", "0" to 9" and the symbols "!"#$%&$()*+,-./". If you choose you can receive the hash of the password to store the hash and give the user the real password. To use this class in your program, you only need to copy the "public class PasswordGenerator" to your own code.

Thanks for the tip/help to my friend Tiago.

The code is quite simple. To generate password or passwords for your site just use the class like this:

C#
// build a new passwordgenerator object
// and call the Generate method with the parameters

PasswordGenerator p = new PasswordGenerator();
string[] pass = p.Generate(8, true, true, true, 2,true);

// we're accessing pass[1] because we wanted the hash
Console.WriteLine("{0} = {1}", pass[0],pass[1]);

System.Threading.Thread.Sleep(15) is really necessary if you dont want the passwords to be all the same. The Random() function needs it.

Check down bellow in the console app how to use this class in your own programs.

C#
string ans = Console.ReadLine();

string mySavedHash = "5D61F65D654D3ADE50FED6EE145F0DE9A157C80C";

string strUser = FormsAuthentication.HashPasswordForStoringInConfigFile(ans, "sha1");

if (strUser == mySavedHash)

{

Console.WriteLine("Ok, nice job.");

}

else

{

Console.WriteLine("Ops... wrong password");

}
You can generate a password like "WH4\"!0-I):". Then you can save the hash you your database or xml file, without any problem in keeping the hash in plain text. The user has the password and in runtime, you verify it like the example above.

The class

public string[] Generate( int n,
bool bIncludeLowerCase,
bool bIncludeNumbers, 
bool bIncludeNonAlphaChars,
int iNumberOfFirstNonSymbolChars,
bool HashWanted)
is very (ok, blame me later...) well commented so I think it is easy to understand the global of it.The project has the class and in the main function you can see it working. It generates 32 new passwords with the hashes and in the end, the user inputs the password that will be checked.Any questions and bugs please report me.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Portugal Portugal
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questiongood job Pin
Gun Gun Febrianza18-Aug-12 8:28
Gun Gun Febrianza18-Aug-12 8:28 
GeneralMy vote of 5 Pin
Gun Gun Febrianza18-Aug-12 8:28
Gun Gun Febrianza18-Aug-12 8:28 
GeneralA GUI for the class Pin
P.Sandgren24-Oct-06 1:09
professionalP.Sandgren24-Oct-06 1:09 
GeneralA small miss Pin
P.Sandgren24-Oct-06 0:06
professionalP.Sandgren24-Oct-06 0:06 
GeneralWould need more feature Pin
Arch4ngel14-Dec-05 9:19
Arch4ngel14-Dec-05 9:19 
AnswerRe: Would need more feature Pin
Adelino Araújo14-Dec-05 11:15
Adelino Araújo14-Dec-05 11:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.