diff --git a/ProjectLighthouse/Helpers/CensorHelper.cs b/ProjectLighthouse/Helpers/CensorHelper.cs index bcf364b7..e566c2df 100644 --- a/ProjectLighthouse/Helpers/CensorHelper.cs +++ b/ProjectLighthouse/Helpers/CensorHelper.cs @@ -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)); diff --git a/ProjectLighthouse/Helpers/CryptoHelper.cs b/ProjectLighthouse/Helpers/CryptoHelper.cs index 116289ef..0b42f473 100644 --- a/ProjectLighthouse/Helpers/CryptoHelper.cs +++ b/ProjectLighthouse/Helpers/CryptoHelper.cs @@ -13,10 +13,6 @@ namespace LBPUnion.ProjectLighthouse.Helpers; [SuppressMessage("ReSharper", "UnusedMember.Global")] public static class CryptoHelper { - /// - /// An instance of Random. Must be locked when in use. - /// - 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 /// /// The amount of bytes to generate. /// The bytes generated - public static IEnumerable GenerateRandomBytes(int count) - { - byte[] b = new byte[count]; + public static IEnumerable GenerateRandomBytes(int count) => RandomNumberGenerator.GetBytes(count); - lock(Random) - { - Random.NextBytes(b); - } - - return b; - } + /// + /// Generates a random bounded 32 bit integer + /// + /// The lowest possible integer than can be generated, inclusive + /// The highest possible integer than can be generated, exclusive + /// The randomly generated integer + public static int GenerateRandomInt32(int fromInclusive, int toExclusive) => RandomNumberGenerator.GetInt32(fromInclusive, toExclusive); public static string ToBase64(string str) {