From 5c7c8de5d89c0397192b9d45352e835e83557cd9 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sun, 4 Oct 2026 12:58:32 +0200 Subject: [PATCH] RandomNumberGenerator: retarget to .NET 10, add GetHexString and GetString helpers, fix range tests - App and Tests move from net7.0 to net10.0 - BenchmarkDotNet 0.15.8, Microsoft.NET.Test.Sdk 18.10.1, xunit 2.9.3, xunit.runner.visualstudio 4.0.0, coverlet.collector 10.1.0 - Add GenerateHexKey and GenerateToken (.NET 8 GetHexString and GetString) with tests - The range tests now assert maxValue - 1: GetInt32 and Random.Next exclude the upper bound, and Assert.InRange is inclusive, so the old assertion passed for 100 --- .../App/App.csproj | 4 +-- .../App/CryptographicHelpers.cs | 6 +++++ .../Tests/CryptographicHelperUnitTest.cs | 26 +++++++++++++++++-- .../Tests/Tests.csproj | 10 +++---- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/dotnet-base-libraries/CryptographyRandomNumberGenerator/App/App.csproj b/dotnet-base-libraries/CryptographyRandomNumberGenerator/App/App.csproj index 3fb178ac66..30230c9005 100644 --- a/dotnet-base-libraries/CryptographyRandomNumberGenerator/App/App.csproj +++ b/dotnet-base-libraries/CryptographyRandomNumberGenerator/App/App.csproj @@ -2,13 +2,13 @@ Exe - net7.0 + net10.0 enable enable - + diff --git a/dotnet-base-libraries/CryptographyRandomNumberGenerator/App/CryptographicHelpers.cs b/dotnet-base-libraries/CryptographyRandomNumberGenerator/App/CryptographicHelpers.cs index 4a1d957d5f..886c767ef0 100644 --- a/dotnet-base-libraries/CryptographyRandomNumberGenerator/App/CryptographicHelpers.cs +++ b/dotnet-base-libraries/CryptographyRandomNumberGenerator/App/CryptographicHelpers.cs @@ -9,4 +9,10 @@ public static class CryptographicHelpers public static int GenerateSecureRandomInteger(int minValue, int maxValue) => RandomNumberGenerator.GetInt32(minValue, maxValue); public static int GenerateGeneralRandomInteger(int minValue, int maxValue) => Random.Shared.Next(minValue, maxValue); + + public static string GenerateHexKey(int length) => + RandomNumberGenerator.GetHexString(length, lowercase: true); + + public static string GenerateToken(int length) => + RandomNumberGenerator.GetString("ABCDEFGHJKLMNPQRSTUVWXYZ23456789", length); } diff --git a/dotnet-base-libraries/CryptographyRandomNumberGenerator/Tests/CryptographicHelperUnitTest.cs b/dotnet-base-libraries/CryptographyRandomNumberGenerator/Tests/CryptographicHelperUnitTest.cs index c503af1fd6..99e71bfdb0 100644 --- a/dotnet-base-libraries/CryptographyRandomNumberGenerator/Tests/CryptographicHelperUnitTest.cs +++ b/dotnet-base-libraries/CryptographyRandomNumberGenerator/Tests/CryptographicHelperUnitTest.cs @@ -20,7 +20,7 @@ public void WhenRandomNumberRangeIsProvided_ThenRandomNumberGeneratedShouldBeInT { var randomNumber = CryptographicHelpers.GenerateSecureRandomInteger(minValue, maxValue); - Assert.InRange(randomNumber, minValue, maxValue); + Assert.InRange(randomNumber, minValue, maxValue - 1); } [Theory] @@ -29,6 +29,28 @@ public void WhenPseudoRandomNumberRangeIsProvided_ThenRandomNumberGeneratedShoul { var randomNumber = CryptographicHelpers.GenerateGeneralRandomInteger(minValue, maxValue); - Assert.InRange(randomNumber, minValue, maxValue); + Assert.InRange(randomNumber, minValue, maxValue - 1); + } + + [Theory] + [InlineData(32)] + public void WhenHexKeyIsGenerated_ThenItHasTheRequestedLengthAndOnlyLowercaseHexCharacters(int length) + { + var hexKey = CryptographicHelpers.GenerateHexKey(length); + + Assert.Equal(length, hexKey.Length); + Assert.Matches("^[0-9a-f]+$", hexKey); + } + + [Theory] + [InlineData(16)] + public void WhenTokenIsGenerated_ThenItHasTheRequestedLengthAndOnlyCharactersFromTheAlphabet(int length) + { + const string alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; + + var token = CryptographicHelpers.GenerateToken(length); + + Assert.Equal(length, token.Length); + Assert.All(token, character => Assert.Contains(character, alphabet)); } } \ No newline at end of file diff --git a/dotnet-base-libraries/CryptographyRandomNumberGenerator/Tests/Tests.csproj b/dotnet-base-libraries/CryptographyRandomNumberGenerator/Tests/Tests.csproj index 6fadd025bc..860f2f5213 100644 --- a/dotnet-base-libraries/CryptographyRandomNumberGenerator/Tests/Tests.csproj +++ b/dotnet-base-libraries/CryptographyRandomNumberGenerator/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net10.0 enable enable @@ -10,13 +10,13 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all