Skip to content

Commit

Permalink
Merge pull request #22 from PandaTechAM/development
Browse files Browse the repository at this point in the history
RandomId generator logic change
  • Loading branch information
HaikAsatryan authored Jun 21, 2024
2 parents ab23260 + 0ace076 commit 62a959e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/Pandatech.Crypto/Pandatech.Crypto.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
<Copyright>MIT</Copyright>
<PackageIcon>pandatech.png</PackageIcon>
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<Version>2.4.1</Version>
<Version>2.5.0</Version>
<Title>Pandatech.Crypto</Title>
<PackageTags>Pandatech, library, encryption, hash, algorythms, security</PackageTags>
<Description>PandaTech.Crypto is a .NET library simplifying common cryptograhic functions.</Description>
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-pandatech-crypto</RepositoryUrl>
<PackageReleaseNotes>Aes256 empty string handle</PackageReleaseNotes>
<PackageReleaseNotes>RandomId generator logic change</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand All @@ -23,7 +23,7 @@

<ItemGroup>
<PackageReference Include="BouncyCastle.NetCore" Version="2.2.1" />
<PackageReference Include="Konscious.Security.Cryptography.Argon2" Version="1.3.0" />
<PackageReference Include="Konscious.Security.Cryptography.Argon2" Version="1.3.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Pandatech.RegexBox" Version="1.2.4" />
</ItemGroup>
Expand Down
14 changes: 4 additions & 10 deletions src/Pandatech.Crypto/Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
17 changes: 3 additions & 14 deletions test/Pandatech.Crypto.Tests/RandomTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,15 @@ 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()
{
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);
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 62a959e

Please sign in to comment.