From 0ace07647409bbb104b4ae037cfedb3a4c49569b Mon Sep 17 00:00:00 2001 From: Haik Date: Fri, 21 Jun 2024 20:47:31 +0400 Subject: [PATCH] RandomId generator logic change --- src/Pandatech.Crypto/Pandatech.Crypto.csproj | 6 +++--- src/Pandatech.Crypto/Random.cs | 14 ++++---------- test/Pandatech.Crypto.Tests/RandomTests.cs | 17 +++-------------- 3 files changed, 10 insertions(+), 27 deletions(-) diff --git a/src/Pandatech.Crypto/Pandatech.Crypto.csproj b/src/Pandatech.Crypto/Pandatech.Crypto.csproj index 90a61e3..f464668 100644 --- a/src/Pandatech.Crypto/Pandatech.Crypto.csproj +++ b/src/Pandatech.Crypto/Pandatech.Crypto.csproj @@ -8,12 +8,12 @@ MIT pandatech.png Readme.md - 2.4.1 + 2.5.0 Pandatech.Crypto Pandatech, library, encryption, hash, algorythms, security PandaTech.Crypto is a .NET library simplifying common cryptograhic functions. https://github.com/PandaTechAM/be-lib-pandatech-crypto - Aes256 empty string handle + RandomId generator logic change @@ -23,7 +23,7 @@ - + diff --git a/src/Pandatech.Crypto/Random.cs b/src/Pandatech.Crypto/Random.cs index f5b77c5..5f8d506 100644 --- a/src/Pandatech.Crypto/Random.cs +++ b/src/Pandatech.Crypto/Random.cs @@ -20,17 +20,11 @@ public static string GenerateAes256KeyString() return Convert.ToBase64String(buffer); } - public static long GeneratePandaId(long? previousId) + public static long GenerateIdWithVariableSequence(long previousId, int approximateSequenceVariability = 100) { - var random = GenerateBytes(4); - var randomValue = BitConverter.ToInt32(random, 0) & 0x7FFFFFFF; - var randomOffset = randomValue % 36 + 1; + var minimumRandRange = approximateSequenceVariability / 25; + var random = System.Random.Shared.NextInt64(minimumRandRange, approximateSequenceVariability + 1); - if (previousId is 0 or null) - { - return 1_000_000 + randomOffset; - } - - return (long)(previousId + randomOffset)!; + return (previousId + random); } } \ No newline at end of file diff --git a/test/Pandatech.Crypto.Tests/RandomTests.cs b/test/Pandatech.Crypto.Tests/RandomTests.cs index d1a67eb..370ccd0 100644 --- a/test/Pandatech.Crypto.Tests/RandomTests.cs +++ b/test/Pandatech.Crypto.Tests/RandomTests.cs @@ -11,18 +11,7 @@ public void Generate_ShouldReturnByteArray() Assert.NotNull(randomBytes); Assert.Equal(length, randomBytes.Length); } - - [Fact] - public void GeneratePandaId_WithZeroPreviousId_ReturnsValidId() - { - const long previousId = 0; - for (var i = 0; i < 1_000_000; ++i) - { - var newId = Random.GeneratePandaId(previousId); - - Assert.True(newId is > 1_000_000 and < 1_000_037); - } - } + [Fact] public void GeneratePandaId_WithNonZeroPreviousId_ReturnsIncrementedId() @@ -30,7 +19,7 @@ public void GeneratePandaId_WithNonZeroPreviousId_ReturnsIncrementedId() const long previousId = 1_000_000; for (var i = 0; i < 1_000_000; ++i) { - var newId = Random.GeneratePandaId(previousId); + var newId = Random.GenerateIdWithVariableSequence(previousId); Assert.True(newId > previousId); } @@ -43,7 +32,7 @@ public void GeneratePandaId_WithinReasonableIterations_DoesNotProduceDuplicates( for (var i = 0; i < 1_000_000; ++i) { - var id = Random.GeneratePandaId(previousId); + var id = Random.GenerateIdWithVariableSequence(previousId); Assert.NotEqual(previousId, id); previousId = id; }