From 177daf903ba6e7d8e72f75e79147b3c2c69c7013 Mon Sep 17 00:00:00 2001 From: JKorf Date: Tue, 30 Dec 2025 09:53:30 +0100 Subject: [PATCH] Added some utils methods --- .../Authentication/AuthenticationProvider.cs | 28 +++++++++++++++++++ CryptoExchange.Net/ExchangeHelpers.cs | 16 +++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/CryptoExchange.Net/Authentication/AuthenticationProvider.cs b/CryptoExchange.Net/Authentication/AuthenticationProvider.cs index 7a4112f..09aa93c 100644 --- a/CryptoExchange.Net/Authentication/AuthenticationProvider.cs +++ b/CryptoExchange.Net/Authentication/AuthenticationProvider.cs @@ -442,6 +442,14 @@ namespace CryptoExchange.Net.Authentication /// /// protected static string BytesToHexString(byte[] buff) + => BytesToHexString(new ArraySegment(buff)); + + /// + /// Convert byte array to hex string + /// + /// + /// + protected static string BytesToHexString(ArraySegment buff) { #if NET9_0_OR_GREATER return Convert.ToHexString(buff); @@ -453,6 +461,26 @@ namespace CryptoExchange.Net.Authentication #endif } + /// + /// Convert a hex encoded string to byte array + /// + /// + /// + protected static byte[] HexToBytesString(string hexString) + { + if (hexString.StartsWith("0x")) + hexString = hexString.Substring(2); + + byte[] bytes = new byte[hexString.Length / 2]; + for (int i = 0; i < hexString.Length; i += 2) + { + string hexSubstring = hexString.Substring(i, 2); + bytes[i / 2] = Convert.ToByte(hexSubstring, 16); + } + + return bytes; + } + /// /// Convert byte array to base64 string /// diff --git a/CryptoExchange.Net/ExchangeHelpers.cs b/CryptoExchange.Net/ExchangeHelpers.cs index 6e35421..dd4fbb0 100644 --- a/CryptoExchange.Net/ExchangeHelpers.cs +++ b/CryptoExchange.Net/ExchangeHelpers.cs @@ -242,8 +242,7 @@ namespace CryptoExchange.Net /// /// Generate a long value /// - /// Max character length - /// + /// Max number of digits public static long RandomLong(int maxLength) { #if NETSTANDARD2_1_OR_GREATER || NET9_0_OR_GREATER @@ -259,6 +258,19 @@ namespace CryptoExchange.Net return value; } + public static long RandomLong(long minValue, long maxValue) + { +#if NET8_0_OR_GREATER + var buf = RandomNumberGenerator.GetBytes(8); +#else + byte[] buf = new byte[8]; + var random = new Random(); + random.NextBytes(buf); +#endif + long longRand = BitConverter.ToInt64(buf, 0); + return (Math.Abs(longRand % (maxValue - minValue)) + minValue); + } + /// /// Generate a random string of specified length ///