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