mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-29 16:38:37 +00:00
Use stronger method for generating randoms
This commit is contained in:
parent
bb1d1b835f
commit
abfa53ca47
2 changed files with 24 additions and 37 deletions
|
@ -39,15 +39,13 @@ public static class CensorHelper
|
||||||
{
|
{
|
||||||
StringBuilder sb = new();
|
StringBuilder sb = new();
|
||||||
|
|
||||||
char prevRandomChar = '\0';
|
|
||||||
|
|
||||||
sb.Append(message.AsSpan(0, profanityIndex));
|
sb.Append(message.AsSpan(0, profanityIndex));
|
||||||
|
|
||||||
switch (CensorConfiguration.Instance.UserInputFilterMode)
|
switch (CensorConfiguration.Instance.UserInputFilterMode)
|
||||||
{
|
{
|
||||||
case FilterMode.Random:
|
case FilterMode.Random:
|
||||||
|
char prevRandomChar = '\0';
|
||||||
for (int i = 0; i < profanityLength; i++)
|
for (int i = 0; i < profanityLength; i++)
|
||||||
lock(CryptoHelper.Random)
|
|
||||||
{
|
{
|
||||||
if (message[i] == ' ')
|
if (message[i] == ' ')
|
||||||
{
|
{
|
||||||
|
@ -55,11 +53,10 @@ public static class CensorHelper
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
char randomChar = randomCharacters[CryptoHelper.Random.Next(0, randomCharacters.Length - 1)];
|
char randomChar = randomCharacters[CryptoHelper.GenerateRandomInt32(0, randomCharacters.Length)];
|
||||||
if (randomChar == prevRandomChar) randomChar = randomCharacters[CryptoHelper.Random.Next(0, randomCharacters.Length - 1)];
|
if (randomChar == prevRandomChar) randomChar = randomCharacters[CryptoHelper.GenerateRandomInt32(0, randomCharacters.Length)];
|
||||||
|
|
||||||
prevRandomChar = randomChar;
|
prevRandomChar = randomChar;
|
||||||
|
|
||||||
sb.Append(randomChar);
|
sb.Append(randomChar);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,12 +70,8 @@ public static class CensorHelper
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case FilterMode.Furry:
|
case FilterMode.Furry:
|
||||||
lock(CryptoHelper.Random)
|
string randomWord = randomFurry[CryptoHelper.GenerateRandomInt32(0, randomFurry.Length)];
|
||||||
{
|
|
||||||
string randomWord = randomFurry[CryptoHelper.Random.Next(0, randomFurry.Length - 1)];
|
|
||||||
sb.Append(randomWord);
|
sb.Append(randomWord);
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case FilterMode.None: break;
|
case FilterMode.None: break;
|
||||||
default: throw new ArgumentOutOfRangeException(nameof(message));
|
default: throw new ArgumentOutOfRangeException(nameof(message));
|
||||||
|
|
|
@ -13,10 +13,6 @@ namespace LBPUnion.ProjectLighthouse.Helpers;
|
||||||
[SuppressMessage("ReSharper", "UnusedMember.Global")]
|
[SuppressMessage("ReSharper", "UnusedMember.Global")]
|
||||||
public static class CryptoHelper
|
public static class CryptoHelper
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// An instance of Random. Must be locked when in use.
|
|
||||||
/// </summary>
|
|
||||||
public static readonly Random Random = new();
|
|
||||||
|
|
||||||
// private static readonly SHA1 sha1 = SHA1.Create();
|
// private static readonly SHA1 sha1 = SHA1.Create();
|
||||||
private static readonly SHA256 sha256 = SHA256.Create();
|
private static readonly SHA256 sha256 = SHA256.Create();
|
||||||
|
@ -64,17 +60,15 @@ public static class CryptoHelper
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="count">The amount of bytes to generate.</param>
|
/// <param name="count">The amount of bytes to generate.</param>
|
||||||
/// <returns>The bytes generated</returns>
|
/// <returns>The bytes generated</returns>
|
||||||
public static IEnumerable<byte> GenerateRandomBytes(int count)
|
public static IEnumerable<byte> GenerateRandomBytes(int count) => RandomNumberGenerator.GetBytes(count);
|
||||||
{
|
|
||||||
byte[] b = new byte[count];
|
|
||||||
|
|
||||||
lock(Random)
|
/// <summary>
|
||||||
{
|
/// Generates a random bounded 32 bit integer
|
||||||
Random.NextBytes(b);
|
/// </summary>
|
||||||
}
|
/// <param name="fromInclusive">The lowest possible integer than can be generated, inclusive</param>
|
||||||
|
/// <param name="toExclusive">The highest possible integer than can be generated, exclusive</param>
|
||||||
return b;
|
/// <returns>The randomly generated integer</returns>
|
||||||
}
|
public static int GenerateRandomInt32(int fromInclusive, int toExclusive) => RandomNumberGenerator.GetInt32(fromInclusive, toExclusive);
|
||||||
|
|
||||||
public static string ToBase64(string str)
|
public static string ToBase64(string str)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue