Skip to content

Commit

Permalink
Added internal RandomId generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Haik committed Oct 31, 2023
1 parent dda55f4 commit 4bcee44
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 12 deletions.
10 changes: 0 additions & 10 deletions Pandatech.Crypto.Tests/Aes256Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@ namespace Pandatech.Crypto.Tests;

public class Aes256Tests
{
[Fact]
public void Generate_ShouldReturnByteArray()
{
const int length = 16;
var randomBytes = Random.GenerateBytes(length);

Assert.NotNull(randomBytes);
Assert.Equal(length, randomBytes.Length);
}

[Fact]
public void EncryptDecryptWithParameter_ShouldReturnOriginalString()
{
Expand Down
51 changes: 51 additions & 0 deletions Pandatech.Crypto.Tests/RandomTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace Pandatech.Crypto.Tests;

public class RandomTests
{
[Fact]
public void Generate_ShouldReturnByteArray()
{
const int length = 16;
var randomBytes = Random.GenerateBytes(length);

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);

Assert.True(newId > previousId);
}
}

[Fact]
public void GeneratePandaId_WithinReasonableIterations_DoesNotProduceDuplicates()
{
long previousId = 0;

for (var i = 0; i < 1_000_000; ++i)
{
var id = Random.GeneratePandaId(previousId);
Assert.NotEqual(previousId, id);
previousId = id;
}
}
}
4 changes: 2 additions & 2 deletions Pandatech.Crypto/Pandatech.Crypto.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.1.1</Version>
<Version>2.1.2</Version>
<Title>Pandatech.Crypto</Title>
<Authors>Pandatech</Authors>
<PackageIcon>pandatech.png</PackageIcon>
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<Description>PandaTech.Crypto is a .NET library simplifying common cryptograhic functions.</Description>
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-pandatech-crypto</RepositoryUrl>
<PackageReleaseNotes>AES256 encrypted byte array sequence changed</PackageReleaseNotes>
<PackageReleaseNotes>Added Random Id Generator for internal use</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions Pandatech.Crypto/Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,18 @@ public static string GenerateAes256KeyString()
rng.GetBytes(buffer);
return Convert.ToBase64String(buffer);
}

public static long GeneratePandaId(long? previousId)
{
var random = GenerateBytes(4);
var randomValue = BitConverter.ToInt32(random, 0) & 0x7FFFFFFF;
var randomOffset = randomValue % 36 + 1;

if (previousId is 0 or null)
{
return 1_000_000 + randomOffset;
}

return (long)(previousId + randomOffset)!;
}
}

0 comments on commit 4bcee44

Please sign in to comment.