forked from jasonpang/RemoteDesktop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlphaNumericGenerator.cs
More file actions
150 lines (125 loc) · 4.92 KB
/
Copy pathAlphaNumericGenerator.cs
File metadata and controls
150 lines (125 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Security.Cryptography;
namespace Model
{
// Nova ID: f2h 1,594,323 Permutations
// Password:
public enum GeneratorOptions
{
Alpha = 1,
Numeric = 2,
AlphaNumeric = Alpha + Numeric,
AlphaNumericSpecial = 4
}
/// <remarks>Copied from http://eyeung003.blogspot.com/2010/09/c-random-password-generator.html </remarks>
public class AlphaNumericGenerator
{
// Define default password length.
private static int DEFAULT_PASSWORD_LENGTH = 3;
// No characters that are confusing: i, I, l, L, o, O, 0, 1, u, v
// No characters that are hard to pronounce over the phone (rhyming letters): a-j-h, b-c-d-e-g-z, m-n...etc
private static string PASSWORD_CHARS_Alpha =
"fhpqrw";
private static string PASSWORD_CHARS_NUMERIC = "2456789";
private static string PASSWORD_CHARS_SPECIAL = "*$-+?_&=!%{}/";
#region Overloads
/// <summary>
/// Generates a random password with the default length.
/// </summary>
/// <returns>Randomly generated password.</returns>
public static string Generate()
{
return Generate(DEFAULT_PASSWORD_LENGTH,
GeneratorOptions.Numeric);
}
/// <summary>
/// Generates a random password with the default length.
/// </summary>
/// <returns>Randomly generated password.</returns>
public static string Generate(GeneratorOptions option)
{
return Generate(DEFAULT_PASSWORD_LENGTH, option);
}
/// <summary>
/// Generates a random password with the default length.
/// </summary>
/// <returns>Randomly generated password.</returns>
public static string Generate(int passwordLength)
{
return Generate(DEFAULT_PASSWORD_LENGTH,
GeneratorOptions.Numeric);
}
/// <summary>
/// Generates a random password.
/// </summary>
/// <returns>Randomly generated password.</returns>
public static string Generate(int passwordLength,
GeneratorOptions option)
{
return GeneratePassword(passwordLength, option);
}
#endregion
/// <summary>
/// Generates the password.
/// </summary>
/// <returns></returns>
private static string GeneratePassword(int passwordLength,
GeneratorOptions option)
{
if (passwordLength < 0) return null;
var passwordChars = GetCharacters(option);
if (string.IsNullOrEmpty(passwordChars)) return null;
var password = new char[passwordLength];
var random = GetRandom();
for (int i = 0; i < passwordLength; i++)
{
var index = random.Next(passwordChars.Length);
var passwordChar = passwordChars[index];
password[i] = passwordChar;
}
return new string(password);
}
/// <summary>
/// Gets the characters selected by the option
/// </summary>
/// <returns></returns>
private static string GetCharacters(GeneratorOptions option)
{
switch (option)
{
case GeneratorOptions.Alpha:
return PASSWORD_CHARS_Alpha;
case GeneratorOptions.Numeric:
return PASSWORD_CHARS_NUMERIC;
case GeneratorOptions.AlphaNumeric:
return PASSWORD_CHARS_Alpha + PASSWORD_CHARS_NUMERIC;
case GeneratorOptions.AlphaNumericSpecial:
return PASSWORD_CHARS_Alpha + PASSWORD_CHARS_NUMERIC +
PASSWORD_CHARS_SPECIAL;
default:
break;
}
return string.Empty;
}
/// <summary>
/// Gets a random object with a real random seed
/// </summary>
/// <returns></returns>
private static Random GetRandom()
{
// Use a 4-byte array to fill it with random bytes and convert it then
// to an integer value.
var randomBytes = new byte[4];
// Generate 4 random bytes.
var rng = new RNGCryptoServiceProvider();
rng.GetBytes(randomBytes);
// Convert 4 bytes into a 32-bit integer value.
int seed = (randomBytes[0] & 0x7f) << 24 |
randomBytes[1] << 16 |
randomBytes[2] << 8 |
randomBytes[3];
// Now, this is real randomization.
return new Random(seed);
}
}
}