här
orkar inte sortera men...
using System;
using System.Text;
namespace myNameSpace
{
/// <summary>
/// The Allow flags define restriction values for password generation.
/// </summary>
[Flags]
public enum Allow : byte
{
Alpha = 1, Digit = 2, Symbol = 4, Lowercase = 8, Uppercase = 16,
DigitFirst = 32, DigitLast = 64, SymbolFirst = 128, SymbolLast = 255
}
/// <remarks>
/// This class implements a simple password generator. It allows setting properties
/// for the types of characters allowed and sizing the generated password.
/// </remarks>
public class PasswordGenerator
{
/// <summary>
/// The ASCII enumeration defines several ranges for ASCII characters.
/// 32 - 47, 58 - 64, 91 - 95, 123 - 126 special characters
/// 48 - 57 digits
/// 65 - 90, 97 - 122 uppercase, lowercase letters
/// </summary>
public enum ASCII
{
SpecialLower1 = 32, SpecialUpper1 = 47, SpecialLower2 = 58, SpecialUpper2 = 64,
SpecialLower3 = 91, SpecialUpper3 = 95, SpecialLower4 = 123, SpecialUpper4 = 126,
DigitsLower = 48, DigitsUpper = 57,
UppercaseLower = 65, UppercaseUpper = 90, LowercaseLower = 97, LowercaseUpper = 122
}
public PasswordGenerator()
{
passwordBuffer = new StringBuilder();
this.Restrictions = Allow.Alpha;
this.MinimumSize = 6;
this.MaximumSize = 10;
this.ExcludedCharacters = String.Empty;
}
/// <summary>
/// The Password property generates and returns a new password.
/// </summary>
public string Password
{
get
{
return Generate();
}
}
/// <summary>
/// The Restrictions property gets and sets the flags that
/// control allowable characters for the password.
/// </summary>
public Allow Restrictions
{
get
{
return this.allowFlags;
}
set
{
this.allowFlags = value;
}
}
/// <summary>
/// The MinimumSize property gets/sets the minimum, allowable
/// length of the password.
/// </summary>
public int MinimumSize
{
get
{
return this.minSize;
}
set
{
this.minSize = value;
}
}
/// <summary>
/// The MaximumSize property gets/sets the maximum, allowable
/// length of the password.
/// </summary>
public int MaximumSize
{
get
{
return this.maxSize;
}
set
{
this.maxSize = value;
}
}
/// <summary>
/// The ExcludedCharacters property gets/sets the set of individual characters
/// to be excluded from the password.
/// </summary>
public string ExcludedCharacters
{
get
{
return this.excludedChars;
}
set
{
this.excludedChars = value;
}
}
/// <summary>
/// The MaximumDigits property gets/sets the maximum, allowable
/// number of digits in the password.
/// </summary>
public int MaximumDigits
{
get
{
return this.maxDigits;
}
set
{
this.maxDigits = value;
}
}
/// <summary>
/// The MaximumSymbols property gets/sets the maximum, allowable
/// number of symbols in the password.
/// </summary>
public int MaximumSymbols
{
get
{
return this.maxSymbols;
}
set
{
this.maxSymbols = value;
}
}
protected virtual string Generate()
{
rnd = new Random();
passwordBuffer.Capacity = this.MaximumSize * 2;
int length = 0;
int max = passwordBuffer.Capacity - 1;
while ( length < max )
{
int genChar = this.rnd.Next(0, 4);
switch (genChar)
{
case 0:
if ( (this.Restrictions & Allow.Lowercase) != 0 )
{
AppendRandomCharacter(ASCII.LowercaseLower, ASCII.LowercaseUpper);
length = length + 1;
}
break;
case 1:
if ( (this.Restrictions & Allow.Uppercase) != 0 )
{
AppendRandomCharacter(ASCII.UppercaseLower, ASCII.UppercaseUpper);
length = length + 1;
}
break;
case 2:
if ( (this.Restrictions & Allow.Digit) != 0 )
{
AppendRandomCharacter(ASCII.DigitsLower, ASCII.DigitsUpper);
length = length + 1;
}
break;
case 3:
if ( (this.Restrictions & Allow.Symbol) != 0 )
{
int sym = this.rnd.Next(0, 4);
switch (sym)
{
case 0:
AppendRandomCharacter(ASCII.SpecialLower1, ASCII.SpecialUpper1);
length = length + 1;
break;
case 1:
AppendRandomCharacter(ASCII.SpecialLower2, ASCII.SpecialUpper2);
length = length + 1;
break;
case 2:
AppendRandomCharacter(ASCII.SpecialLower3, ASCII.SpecialUpper3);
length = length + 1;
break;
case 3:
AppendRandomCharacter(ASCII.SpecialLower4, ASCII.SpecialUpper4);
length = length + 1;
break;
}
}
break;
}
}
EnforceConstraints();
return passwordBuffer.ToString().Trim();
}
protected void AppendRandomCharacter(ASCII lower, ASCII upper)
{
int rndchar = this.rnd.Next((int)lower, (int)(upper + 1));
byte[] rndbyte = new byte[1];
rndbyte[0] = (byte)rndchar;
string appendstr = Encoding.ASCII.GetString(rndbyte);
passwordBuffer.Append(appendstr);
}
protected void EnforceConstraints()
{
// Remove excluded characters
foreach (char c in this.ExcludedCharacters)
{
passwordBuffer.Replace(c.ToString(), null);
}
// Count digits and symbols and ensure they don't exceed maximums
int numDigits = 0, numSymbols = 0;
StringBuilder temp = new StringBuilder(passwordBuffer.ToString(), 0,this.MaximumSize, this.MaximumSize);
foreach (char count in temp.ToString())
{
if ( Char.IsDigit(count) == true )
{
numDigits++;
}
if ( Char.IsSymbol(count) == true )
{
numSymbols++;
}
}
if ( numDigits > this.MaximumDigits )
{
int count = 0;
for ( int i = 0; i < temp.Length; i++ )
{
if ( Char.IsDigit(temp[i]) == true )
{
if ( count < numDigits - this.MaximumDigits )
{
temp.Replace(temp[i].ToString(), null);
count++;
}
}
}
}
if ( numSymbols > this.MaximumSymbols )
{
int count = 0;
for ( int i = 0; i < temp.Length; i++ )
{
if ( Char.IsSymbol(temp[i]) == true )
{
if ( count < numSymbols - this.MaximumSymbols )
{
temp.Replace(temp[i].ToString(), null);
count++;
}
}
}
}
passwordBuffer.Remove(0, temp.Length);
passwordBuffer.Insert(0, temp.ToString(), temp.Length);
// Check first character
if ( (this.Restrictions & Allow.DigitFirst) == 0 )
{
while ( Char.IsDigit(passwordBuffer[0]) == true )
{
passwordBuffer.Remove(0, 1);
}
}
if ( (this.Restrictions & Allow.SymbolFirst) == 0 )
{
while ( Char.IsSymbol(passwordBuffer[0]) == true )
{
passwordBuffer.Remove(0, 1);
}
}
// Pick random length between minimum and maximum
int pwdLength = rnd.Next(this.MinimumSize, ( this.MaximumSize + 1 ));
passwordBuffer.Length = pwdLength;
// Check last character
if ( (this.Restrictions & Allow.DigitLast) == 0 )
{
while ( Char.IsDigit(passwordBuffer[passwordBuffer.Length - 1]) == true )
{
passwordBuffer.Remove(passwordBuffer.Length - 1, 1);
}
}
if ( (this.Restrictions & Allow.SymbolLast) == 0 )
{
while ( Char.IsSymbol(passwordBuffer[passwordBuffer.Length - 1]) == true )
{
passwordBuffer.Remove(passwordBuffer.Length - 1, 1);
}
}
}
private StringBuilder passwordBuffer;
private int minSize, maxSize;
private string excludedChars;
private int maxDigits, maxSymbols;
private Allow allowFlags;
private Random rnd;
}
}