| | | 1 | | using System.Security.Cryptography; |
| | | 2 | | using System.Text; |
| | | 3 | | using ArturRios.Util.Collections; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Util.Random; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides helpers for generating random numbers and strings with optional exclusion constraints. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class CustomRandom |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Generates a cryptographically strong random integer between <paramref name="start"/> (inclusive) and <paramref n |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="start">Minimum value (inclusive).</param> |
| | | 16 | | /// <param name="end">Maximum value (inclusive).</param> |
| | | 17 | | /// <param name="differentFrom">Optional value to avoid returning; regeneration occurs until distinct.</param> |
| | | 18 | | /// <returns>A random integer in the specified range.</returns> |
| | | 19 | | public static int NumberFromRng(int start, int end, int? differentFrom = null) |
| | 2 | 20 | | { |
| | 2 | 21 | | end++; |
| | | 22 | | |
| | 2 | 23 | | var random = RandomNumberGenerator.GetInt32(start, end); |
| | | 24 | | |
| | 2 | 25 | | if (differentFrom is null) |
| | 1 | 26 | | { |
| | 1 | 27 | | return random; |
| | | 28 | | } |
| | | 29 | | |
| | 1 | 30 | | while (random == differentFrom) |
| | 0 | 31 | | { |
| | 0 | 32 | | random = RandomNumberGenerator.GetInt32(start, end); |
| | 0 | 33 | | } |
| | | 34 | | |
| | 1 | 35 | | return random; |
| | 2 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Generates a random integer using <see cref="System.Random"/> between <paramref name="start"/> (inclusive) and <p |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="start">Minimum value (inclusive).</param> |
| | | 42 | | /// <param name="end">Maximum value (exclusive).</param> |
| | | 43 | | /// <param name="differentFrom">Optional value to avoid returning; regeneration occurs until distinct.</param> |
| | | 44 | | /// <returns>A random integer in the specified range.</returns> |
| | | 45 | | public static int NumberFromSystemRandom(int start, int end, int? differentFrom = null) |
| | 2 | 46 | | { |
| | 2 | 47 | | System.Random rng = new(); |
| | | 48 | | |
| | 2 | 49 | | var random = rng.Next(start, end); |
| | | 50 | | |
| | 2 | 51 | | if (differentFrom is null) |
| | 1 | 52 | | { |
| | 1 | 53 | | return random; |
| | | 54 | | } |
| | | 55 | | |
| | 1 | 56 | | while (random == differentFrom) |
| | 0 | 57 | | { |
| | 0 | 58 | | random = rng.Next(start, end); |
| | 0 | 59 | | } |
| | | 60 | | |
| | 1 | 61 | | return random; |
| | 2 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Generates a random string respecting the constraints defined in <paramref name="options"/>. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <param name="options">Options controlling length and character inclusion.</param> |
| | | 68 | | /// <param name="differentFrom">Optional collection of strings to exclude from results.</param> |
| | | 69 | | /// <returns>A randomly generated string.</returns> |
| | | 70 | | public static string Text(RandomStringOptions options, string[]? differentFrom = null) |
| | 3 | 71 | | { |
| | 3 | 72 | | while (true) |
| | 3 | 73 | | { |
| | 3 | 74 | | var random = new System.Random(); |
| | 3 | 75 | | var password = new StringBuilder(); |
| | | 76 | | |
| | 3 | 77 | | var charCollectionCount = 0; |
| | | 78 | | |
| | 3 | 79 | | if (options.IncludeLowercase) |
| | 3 | 80 | | { |
| | 3 | 81 | | password.Append(Characters.LowerLetters[random.Next(Characters.LowerLetters.Length)]); |
| | 3 | 82 | | charCollectionCount++; |
| | 3 | 83 | | } |
| | | 84 | | |
| | 3 | 85 | | if (options.IncludeUppercase) |
| | 3 | 86 | | { |
| | 3 | 87 | | password.Append(Characters.UpperLetters[random.Next(Characters.UpperLetters.Length)]); |
| | 3 | 88 | | charCollectionCount++; |
| | 3 | 89 | | } |
| | | 90 | | |
| | 3 | 91 | | if (options.IncludeSpecialCharacters) |
| | 3 | 92 | | { |
| | 3 | 93 | | password.Append(Characters.Special[random.Next(Characters.Special.Length)]); |
| | 3 | 94 | | charCollectionCount++; |
| | 3 | 95 | | } |
| | | 96 | | |
| | 3 | 97 | | if (options.IncludeDigits) |
| | 3 | 98 | | { |
| | 3 | 99 | | password.Append(Characters.Digits[random.Next(Characters.Digits.Length)]); |
| | 3 | 100 | | charCollectionCount++; |
| | 3 | 101 | | } |
| | | 102 | | |
| | 144 | 103 | | for (var i = charCollectionCount; i < options.Length; i++) |
| | 69 | 104 | | { |
| | 69 | 105 | | password.Append(Characters.All[random.Next(Characters.All.Length)]); |
| | 69 | 106 | | } |
| | | 107 | | |
| | 84 | 108 | | var generatedString = new string(password.ToString().OrderBy(_ => random.Next()).ToArray()); |
| | 3 | 109 | | var matchesExcludedString = false; |
| | | 110 | | |
| | 3 | 111 | | if (differentFrom != null) |
| | 1 | 112 | | { |
| | 4 | 113 | | matchesExcludedString = differentFrom.Any(excludedString => excludedString.Equals(generatedString)); |
| | 1 | 114 | | } |
| | | 115 | | |
| | 3 | 116 | | if (matchesExcludedString) |
| | 0 | 117 | | { |
| | 0 | 118 | | continue; |
| | | 119 | | } |
| | | 120 | | |
| | 3 | 121 | | return generatedString; |
| | | 122 | | } |
| | 3 | 123 | | } |
| | | 124 | | } |