Skip to content

Commit

Permalink
refactor: migrate from bouncycastle to onixlabs (#26)
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Brault <[email protected]>
  • Loading branch information
xaevik committed Aug 29, 2023
1 parent 0c65ed6 commit c466cb4
Show file tree
Hide file tree
Showing 7 changed files with 405 additions and 349 deletions.
28 changes: 12 additions & 16 deletions src/cuid.net/Cuid2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

using System.Buffers.Binary;
using System.Runtime.InteropServices;
using Org.BouncyCastle.Crypto.Digests;
using OnixLabs.Security.Cryptography;

/// <summary>
/// Represents a collision resistant unique identifier (CUID).
Expand Down Expand Up @@ -110,25 +110,21 @@ public override int GetHashCode()
public override string ToString()
{
Span<byte> data = stackalloc byte[16];
Span<byte> result = stackalloc byte[64];

BinaryPrimitives.WriteInt64LittleEndian(data[..8], _timestamp);
BinaryPrimitives.WriteUInt64LittleEndian(data[^8..], _counter);

Sha3Digest digest = new(512);

digest.BlockUpdate(data);
digest.BlockUpdate(_fingerprint);
digest.BlockUpdate(_random);

int bytesWritten = digest.DoFinal(result);

if ( bytesWritten != 64 )
{
return string.Empty;
}

return _prefix + Utils.Encode(result.ToArray())[..( _maxLength - 1 )];
byte[] result2 = new byte[data.Length + _fingerprint.Length + _random.Length];

Buffer.BlockCopy(data.ToArray(), 0, result2, 0, data.Length);
Buffer.BlockCopy(_fingerprint, 0, result2, data.Length, _fingerprint.Length);
Buffer.BlockCopy(_random, 0, result2, data.Length + _fingerprint.Length, _random.Length);

using Sha3Hash512 digest = new();

byte[] hash = digest.ComputeHash(result2);

return _prefix + Utils.Encode(hash)[..( _maxLength - 1 )];
}

private static class Context
Expand Down
2 changes: 1 addition & 1 deletion src/cuid.net/cuid.net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BouncyCastle.Cryptography" Version="2.2.1" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -44,6 +43,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="OnixLabs.Security.Cryptography" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
40 changes: 28 additions & 12 deletions src/cuid.net/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
"version": 1,
"dependencies": {
"net6.0": {
"BouncyCastle.Cryptography": {
"type": "Direct",
"requested": "[2.2.1, )",
"resolved": "2.2.1",
"contentHash": "A6Zr52zVqJKt18ZBsTnX0qhG0kwIQftVAjLmszmkiR/trSp8H+xj1gUOzk7XHwaKgyREMSV1v9XaKrBUeIOdvQ=="
},
"Microsoft.CodeAnalysis.NetAnalyzers": {
"type": "Direct",
"requested": "[7.0.4, )",
Expand All @@ -24,6 +18,15 @@
"Microsoft.SourceLink.Common": "1.1.1"
}
},
"OnixLabs.Security.Cryptography": {
"type": "Direct",
"requested": "[5.0.0, )",
"resolved": "5.0.0",
"contentHash": "wIzt0phTiztuBal5GKPe6+QuFj8MvsPVK6tOzSDs0iZ8dG8LwNXVpDZENNjhYUFD873tBoT1x66SBPkKmg5LiA==",
"dependencies": {
"OnixLabs.Core": "5.0.0"
}
},
"Microsoft.Build.Tasks.Git": {
"type": "Transitive",
"resolved": "1.1.1",
Expand All @@ -33,15 +36,14 @@
"type": "Transitive",
"resolved": "1.1.1",
"contentHash": "WMcGpWKrmJmzrNeuaEb23bEMnbtR/vLmvZtkAP5qWu7vQsY59GqfRJd65sFpBszbd2k/bQ8cs8eWawQKAabkVg=="
},
"OnixLabs.Core": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "Ia0z1wVZFSdfOlrlb+30pRMV0NdCjUnLdwJYtxhgmISS6nfOrOH67AQjBdZvs4h3+ajRq7SZbujYGxveCgogWw=="
}
},
"net7.0": {
"BouncyCastle.Cryptography": {
"type": "Direct",
"requested": "[2.2.1, )",
"resolved": "2.2.1",
"contentHash": "A6Zr52zVqJKt18ZBsTnX0qhG0kwIQftVAjLmszmkiR/trSp8H+xj1gUOzk7XHwaKgyREMSV1v9XaKrBUeIOdvQ=="
},
"Microsoft.CodeAnalysis.NetAnalyzers": {
"type": "Direct",
"requested": "[7.0.4, )",
Expand All @@ -58,6 +60,15 @@
"Microsoft.SourceLink.Common": "1.1.1"
}
},
"OnixLabs.Security.Cryptography": {
"type": "Direct",
"requested": "[5.0.0, )",
"resolved": "5.0.0",
"contentHash": "wIzt0phTiztuBal5GKPe6+QuFj8MvsPVK6tOzSDs0iZ8dG8LwNXVpDZENNjhYUFD873tBoT1x66SBPkKmg5LiA==",
"dependencies": {
"OnixLabs.Core": "5.0.0"
}
},
"Microsoft.Build.Tasks.Git": {
"type": "Transitive",
"resolved": "1.1.1",
Expand All @@ -67,6 +78,11 @@
"type": "Transitive",
"resolved": "1.1.1",
"contentHash": "WMcGpWKrmJmzrNeuaEb23bEMnbtR/vLmvZtkAP5qWu7vQsY59GqfRJd65sFpBszbd2k/bQ8cs8eWawQKAabkVg=="
},
"OnixLabs.Core": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "Ia0z1wVZFSdfOlrlb+30pRMV0NdCjUnLdwJYtxhgmISS6nfOrOH67AQjBdZvs4h3+ajRq7SZbujYGxveCgogWw=="
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/cuid.net.benchmarks/cuid.net.benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.4" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.7" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit c466cb4

Please sign in to comment.