This repository was archived by the owner on Jul 21, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyChecker.cs
More file actions
52 lines (45 loc) · 1.64 KB
/
Copy pathKeyChecker.cs
File metadata and controls
52 lines (45 loc) · 1.64 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
using System.Security.Cryptography;
using System.Text;
namespace MythicalWebPanel {
public class KeyChecker
{
public static bool isStrongKey(string password, int minimumLength = 8)
{
bool hasUppercase = false;
bool hasLowercase = false;
bool hasDigit = false;
if (password.Length < minimumLength)
return false;
foreach (char c in password)
{
if (char.IsUpper(c))
hasUppercase = true;
else if (char.IsLower(c))
hasLowercase = true;
else if (char.IsDigit(c))
hasDigit = true;
}
return hasUppercase && hasLowercase && hasDigit;
}
public static string GenerateStrongKey(int length = 32)
{
const string uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string lowercaseChars = "abcdefghijklmnopqrstuvwxyz";
const string digitChars = "0123456789";
string validChars = uppercaseChars + lowercaseChars + digitChars;
#pragma warning disable
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
{
byte[] randomBytes = new byte[length];
rng.GetBytes(randomBytes);
StringBuilder sb = new StringBuilder(length);
foreach (byte b in randomBytes)
{
sb.Append(validChars[b % validChars.Length]);
}
return sb.ToString();
}
#pragma warning restore
}
}
}