diff --git a/CryptoExchange.Net/ExchangeHelpers.cs b/CryptoExchange.Net/ExchangeHelpers.cs index 9ce9f1a..988b874 100644 --- a/CryptoExchange.Net/ExchangeHelpers.cs +++ b/CryptoExchange.Net/ExchangeHelpers.cs @@ -1,5 +1,6 @@ using CryptoExchange.Net.Objects; using System; +using System.Security.Cryptography; namespace CryptoExchange.Net { @@ -8,6 +9,8 @@ namespace CryptoExchange.Net /// public static class ExchangeHelpers { + private const string _allowedRandomChars = "ABCDEFGHIJKLMONOPQRSTUVWXYZabcdefghijklmonopqrstuvwxyz0123456789"; + /// /// The last used id, use NextId() to get the next id and up this /// @@ -140,5 +143,43 @@ namespace CryptoExchange.Net return _lastId; } } + + /// + /// Generate a random string of specified length + /// + /// Length of the random string + /// + public static string RandomString(int length) + { + var randomChars = new char[length]; + +#if NETSTANDARD2_1_OR_GREATER + for (int i = 0; i < length; i++) + randomChars[i] = _allowedRandomChars[RandomNumberGenerator.GetInt32(0, _allowedRandomChars.Length)]; +#else + var random = new Random(); + for (int i = 0; i < length; i++) + randomChars[i] = _allowedRandomChars[random.Next(0, _allowedRandomChars.Length)]; +#endif + + return new string(randomChars); + } + + /// + /// Generate a random string of specified length + /// + /// The initial string + /// Total length of the resulting string + /// + public static string AppendRandomString(string source, int totalLength) + { + if (totalLength < source.Length) + throw new ArgumentException("Total length smaller than source string length", nameof(totalLength)); + + if (totalLength == source.Length) + return source; + + return source + RandomString(totalLength - source.Length); + } } }