mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
refactor(backend:utilties): simplify GenerateSecureRandomString
This commit is contained in:
@@ -14,27 +14,7 @@ namespace SharpbinV3.Server
|
|||||||
public static string GenerateSecureRandomString(int length)
|
public static string GenerateSecureRandomString(int length)
|
||||||
{
|
{
|
||||||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||||
Span<byte> buffer = stackalloc byte[128];
|
return RandomNumberGenerator.GetString(chars, length);
|
||||||
|
|
||||||
var result = new char[length];
|
|
||||||
int pos = 0;
|
|
||||||
|
|
||||||
while (pos < length)
|
|
||||||
{
|
|
||||||
RandomNumberGenerator.Fill(buffer);
|
|
||||||
|
|
||||||
foreach (byte b in buffer)
|
|
||||||
{
|
|
||||||
if (b >= 248)
|
|
||||||
continue; // 248 is the largest multiple of 62 < 256
|
|
||||||
|
|
||||||
result[pos++] = chars[b % chars.Length];
|
|
||||||
if (pos == length)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return new string(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string ComputeSha256(string input)
|
public static string ComputeSha256(string input)
|
||||||
|
|||||||
Reference in New Issue
Block a user