-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPassword.cs
143 lines (117 loc) · 4.8 KB
/
Password.cs
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
using System;
using System.Linq;
using System.Security.Cryptography;
namespace CustomFramework.Utils
{
public static class Password
{
private static readonly char[] Punctuations = "*_-.".ToCharArray();
private static readonly char[] UpperCases = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
private static readonly char[] LowerCases = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
private static readonly char[] Numerics = "0123456789".ToCharArray();
public static string Generate(int length, int numberOfNonAlphanumericCharacters)
{
if (length < 1 || length > 128)
{
throw new ArgumentException(nameof(length));
}
if (numberOfNonAlphanumericCharacters > length || numberOfNonAlphanumericCharacters < 0)
{
throw new ArgumentException(nameof(numberOfNonAlphanumericCharacters));
}
using(var rng = RandomNumberGenerator.Create())
{
var byteBuffer = new byte[length];
rng.GetBytes(byteBuffer);
var countAlphaNumeric = 0;
var countUpperCase = 0;
var countLowerCase = 0;
var countNumeric = 0;
var characterBuffer = new char[length];
for (var iter = 0; iter < length; iter++)
{
var i = byteBuffer[iter] % 66;
if (i < 10)
{
characterBuffer[iter] = (char) ('0' + i);
countNumeric++;
}
else if (i < 36)
{
characterBuffer[iter] = (char) ('A' + i - 10);
countUpperCase++;
}
else if (i < 62)
{
characterBuffer[iter] = (char) ('a' + i - 36);
countLowerCase++;
}
else
{
characterBuffer[iter] = Punctuations[i - 62];
countAlphaNumeric++;
}
}
if (countAlphaNumeric >= numberOfNonAlphanumericCharacters &&
countUpperCase >= 1 &&
countLowerCase >= 1 &&
countNumeric >= 1)
{
return new string(characterBuffer);
}
var check1 = 0;
var check2 = 0;
var check3 = 0;
var check4 = 0;
do
{
if (countAlphaNumeric < numberOfNonAlphanumericCharacters)
{
int j;
var rand = new Random();
for (j = 0; j < numberOfNonAlphanumericCharacters - countAlphaNumeric; j++)
{
int k;
do
{
k = rand.Next(0, length);
}
while (!char.IsLetterOrDigit(characterBuffer[k]));
characterBuffer[k] = Punctuations[rand.Next(0, Punctuations.Length)];
}
}
if (countUpperCase < 1)
{
var rand = new Random();
int k;
k = rand.Next(0, length);
characterBuffer[k] = UpperCases[rand.Next(0, UpperCases.Length)];
}
if (countLowerCase < 1)
{
var rand = new Random();
int k;
k = rand.Next(0, length);
characterBuffer[k] = LowerCases[rand.Next(0, LowerCases.Length)];
}
if (countNumeric < 1)
{
var rand = new Random();
int k;
k = rand.Next(0, length);
characterBuffer[k] = Numerics[rand.Next(0, Numerics.Length)];
}
check1 = characterBuffer.Intersect(Punctuations).Count();
check2 = characterBuffer.Intersect(LowerCases).Count();
check3 = characterBuffer.Intersect(UpperCases).Count();
check4 = characterBuffer.Intersect(Numerics).Count();
}
while (check1 < numberOfNonAlphanumericCharacters &&
check2 < 1 &&
check3 < 1 &&
check4 < 1);
return new string(characterBuffer);
}
}
}
}