mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-28 07:58:40 +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,30 +39,27 @@ public static class CensorHelper
|
|||
{
|
||||
StringBuilder sb = new();
|
||||
|
||||
char prevRandomChar = '\0';
|
||||
|
||||
sb.Append(message.AsSpan(0, profanityIndex));
|
||||
|
||||
switch (CensorConfiguration.Instance.UserInputFilterMode)
|
||||
{
|
||||
case FilterMode.Random:
|
||||
for(int i = 0; i < profanityLength; i++)
|
||||
lock(CryptoHelper.Random)
|
||||
char prevRandomChar = '\0';
|
||||
for (int i = 0; i < profanityLength; i++)
|
||||
{
|
||||
if (message[i] == ' ')
|
||||
{
|
||||
if (message[i] == ' ')
|
||||
{
|
||||
sb.Append(' ');
|
||||
}
|
||||
else
|
||||
{
|
||||
char randomChar = randomCharacters[CryptoHelper.Random.Next(0, randomCharacters.Length - 1)];
|
||||
if (randomChar == prevRandomChar) randomChar = randomCharacters[CryptoHelper.Random.Next(0, randomCharacters.Length - 1)];
|
||||
|
||||
prevRandomChar = randomChar;
|
||||
|
||||
sb.Append(randomChar);
|
||||
}
|
||||
sb.Append(' ');
|
||||
}
|
||||
else
|
||||
{
|
||||
char randomChar = randomCharacters[CryptoHelper.GenerateRandomInt32(0, randomCharacters.Length)];
|
||||
if (randomChar == prevRandomChar) randomChar = randomCharacters[CryptoHelper.GenerateRandomInt32(0, randomCharacters.Length)];
|
||||
|
||||
prevRandomChar = randomChar;
|
||||
sb.Append(randomChar);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case FilterMode.Asterisks:
|
||||
|
@ -73,12 +70,8 @@ public static class CensorHelper
|
|||
|
||||
break;
|
||||
case FilterMode.Furry:
|
||||
lock(CryptoHelper.Random)
|
||||
{
|
||||
string randomWord = randomFurry[CryptoHelper.Random.Next(0, randomFurry.Length - 1)];
|
||||
sb.Append(randomWord);
|
||||
}
|
||||
|
||||
string randomWord = randomFurry[CryptoHelper.GenerateRandomInt32(0, randomFurry.Length)];
|
||||
sb.Append(randomWord);
|
||||
break;
|
||||
case FilterMode.None: break;
|
||||
default: throw new ArgumentOutOfRangeException(nameof(message));
|
||||
|
|
|
@ -13,10 +13,6 @@ namespace LBPUnion.ProjectLighthouse.Helpers;
|
|||
[SuppressMessage("ReSharper", "UnusedMember.Global")]
|
||||
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 SHA256 sha256 = SHA256.Create();
|
||||
|
@ -64,17 +60,15 @@ public static class CryptoHelper
|
|||
/// </summary>
|
||||
/// <param name="count">The amount of bytes to generate.</param>
|
||||
/// <returns>The bytes generated</returns>
|
||||
public static IEnumerable<byte> GenerateRandomBytes(int count)
|
||||
{
|
||||
byte[] b = new byte[count];
|
||||
public static IEnumerable<byte> GenerateRandomBytes(int count) => RandomNumberGenerator.GetBytes(count);
|
||||
|
||||
lock(Random)
|
||||
{
|
||||
Random.NextBytes(b);
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
/// <summary>
|
||||
/// Generates a random bounded 32 bit integer
|
||||
/// </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>
|
||||
/// <returns>The randomly generated integer</returns>
|
||||
public static int GenerateRandomInt32(int fromInclusive, int toExclusive) => RandomNumberGenerator.GetInt32(fromInclusive, toExclusive);
|
||||
|
||||
public static string ToBase64(string str)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue