using Petr.Felzmann; |
using System; |
using System.IO; |
using System.Xml; |
namespace PasswordGenerator |
{ |
class Program |
{ |
public const string HelpParameter = "/?" ; |
public const string MinParameter = "/min" ; |
public const string MaxParameter = "/max" ; |
public const string CatParameter = "/cat" ; |
public const string CountParameter = "/count" ; |
public const int CountDefault = 1; |
[STAThread] |
static void Main( string [] args) |
{ |
XmlDocument categories = null ; |
int minPasswordLength = -1; |
int maxPasswordLength = -1; |
int count = CountDefault; |
if (ParseArguments( |
args, |
ref categories, |
ref minPasswordLength, |
ref maxPasswordLength, |
ref count)) |
{ |
StrongPasswordGenerator gen = null ; |
if (categories == null ) |
{ |
gen = new StrongPasswordGenerator(); |
} |
else |
{ |
gen = new StrongPasswordGenerator(categories); |
} |
for ( int i = 0; i < count; i++) |
{ |
string password = gen.Generate(minPasswordLength, maxPasswordLength); |
Console.WriteLine(password); |
} |
} |
} |
public static bool ParseArguments( |
string [] args, |
ref XmlDocument categories, |
ref int minPasswordLength, |
ref int maxPasswordLength, |
ref int count) |
{ |
string min = null ; |
string max = null ; |
foreach ( string arg in args) |
{ |
if (arg == HelpParameter) |
{ |
Help( null ); |
return false ; |
} |
string [] keyValue = arg.Split( ':' ); |
if (keyValue.Length != 2) |
{ |
Help( "The parameter [" + arg + "] has no value." ); |
return false ; |
} |
string key = keyValue[0]; |
string val = keyValue[1]; |
switch (key) |
{ |
case MinParameter: |
min = val; |
break ; |
case MaxParameter: |
max = val; |
break ; |
case CatParameter: |
if (File.Exists(val)) |
{ |
categories = new XmlDocument(); |
categories.Load(val); |
} |
else |
{ |
Help( "The file [" + val + "] does not exists." ); |
return false ; |
} |
break ; |
case CountParameter: |
count = int .Parse(val); |
break ; |
} |
} |
if (min == null || min == null ) |
{ |
Help( "The parameters [" + MinParameter + "] and [" + MaxParameter + " must be set.]" ); |
return false ; |
} |
minPasswordLength = int .Parse(min); |
maxPasswordLength = int .Parse(max); |
return true ; |
} |
public static void Help( string message) |
{ |
if (message != null ) |
{ |
Console.WriteLine(message); |
Console.WriteLine(); |
} |
Console.WriteLine( "The utility pwdgen generate strong passwords." ); |
Console.WriteLine( "\nUsage:" ); |
Console.WriteLine( "\tpwdgen /min:MIN /max:MAX [/count:C] [/cat:PATH]" ); |
Console.WriteLine( "\nwhere" ); |
Console.WriteLine( "\tMIN is the minimal password length" ); |
Console.WriteLine( "\tMAX is the maximal password length" ); |
Console.WriteLine( "\tC is the count of the passwords will be generated." ); |
Console.WriteLine( "\t Default value is " + CountDefault + "." ); |
Console.WriteLine( "\tPATH is the path to the XML file" ); |
Console.WriteLine( "\t contains the definition of the categories of the characters" ); |
Console.WriteLine( "\nExample:" ); |
Console.WriteLine( "pwdgen /min:6 /max:10 /count:2 /cat:\"c:\\Program Files\\MyCategories.xml\"" ); |
} |
} |
} |