feat: add randomstringgenerator
This commit is contained in:
parent
e0d744ca5a
commit
4973464c88
1 changed files with 26 additions and 0 deletions
26
Util/RandomStringGenerator.cs
Normal file
26
Util/RandomStringGenerator.cs
Normal file
|
@ -0,0 +1,26 @@
|
|||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace ZenithInfo.Util;
|
||||
|
||||
public class RandomStringGenerator
|
||||
{
|
||||
private static readonly char[] chars =
|
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*+?".ToCharArray();
|
||||
|
||||
public static string GenerateRandomString(int length)
|
||||
{
|
||||
var stringBuilder = new StringBuilder();
|
||||
using (var rng = RandomNumberGenerator.Create())
|
||||
{
|
||||
byte[] buffer = new byte[1];
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
rng.GetBytes(buffer);
|
||||
var randomIndex = buffer[0] % chars.Length;
|
||||
stringBuilder.Append(chars[randomIndex]);
|
||||
}
|
||||
}
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue