Skip to content

Commit

Permalink
task: migrate to NSec.Cryptography (#30)
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Brault <alan.brault@visus.io>
  • Loading branch information
xaevik authored Oct 21, 2023
1 parent 5176ccb commit b1f15d6
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 754 deletions.
7 changes: 0 additions & 7 deletions cuid.net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "cuid.net", "src\cuid.net\cu
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "cuid.net.tests", "tests\cuid.net.tests\cuid.net.tests.csproj", "{A7660FD5-9354-4E65-A572-CFA3486F4EE8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "cuid.net.benchmarks", "tests\cuid.net.benchmarks\cuid.net.benchmarks.csproj", "{34E7094D-63C5-4321-96DA-9CB68520A689}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{D68C1229-0EB5-4E02-82D2-55B05D12BFFE}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{7039220C-882B-4D44-8B6D-5A691A0A3835}"
Expand Down Expand Up @@ -40,13 +38,8 @@ Global
{A7660FD5-9354-4E65-A572-CFA3486F4EE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A7660FD5-9354-4E65-A572-CFA3486F4EE8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A7660FD5-9354-4E65-A572-CFA3486F4EE8}.Release|Any CPU.Build.0 = Release|Any CPU
{34E7094D-63C5-4321-96DA-9CB68520A689}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{34E7094D-63C5-4321-96DA-9CB68520A689}.Debug|Any CPU.Build.0 = Debug|Any CPU
{34E7094D-63C5-4321-96DA-9CB68520A689}.Release|Any CPU.ActiveCfg = Release|Any CPU
{34E7094D-63C5-4321-96DA-9CB68520A689}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{34E7094D-63C5-4321-96DA-9CB68520A689} = {D68C1229-0EB5-4E02-82D2-55B05D12BFFE}
{A7660FD5-9354-4E65-A572-CFA3486F4EE8} = {D68C1229-0EB5-4E02-82D2-55B05D12BFFE}
{7A501B42-B686-42C7-ACAB-82B973106716} = {7039220C-882B-4D44-8B6D-5A691A0A3835}
EndGlobalSection
Expand Down
31 changes: 18 additions & 13 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 OnixLabs.Security.Cryptography;
using NSec.Cryptography;

/// <summary>
/// Represents a collision resistant unique identifier (CUID).
Expand Down Expand Up @@ -109,22 +109,27 @@ public override int GetHashCode()
/// <returns>The value of this <see cref="Cuid2" />.</returns>
public override string ToString()
{
Span<byte> data = stackalloc byte[16];

BinaryPrimitives.WriteInt64LittleEndian(data[..8], _timestamp);
BinaryPrimitives.WriteUInt64LittleEndian(data[^8..], _counter);
Span<byte> buffer = stackalloc byte[16];
Span<byte> result = stackalloc byte[64];

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

byte[] result2 = new byte[data.Length + _fingerprint.Length + _random.Length];
byte[] data = new byte[buffer.Length + _fingerprint.Length + _random.Length];

Buffer.BlockCopy(buffer.ToArray(), 0, data, 0, buffer.Length);
Buffer.BlockCopy(_fingerprint, 0, data, buffer.Length, _fingerprint.Length);
Buffer.BlockCopy(_random, 0, data, buffer.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);
Sha512 sha512 = new();

using Sha3Hash512 digest = new();
sha512.Hash(data, result);
if ( !sha512.Verify(data, result) )
{
return string.Empty;
}

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

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

private static class Context
Expand Down
5 changes: 3 additions & 2 deletions src/cuid.net/cuid.net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<PropertyGroup>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IsTrimmable>true</IsTrimmable>
<IncludeSymbols>true</IncludeSymbols>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand All @@ -30,7 +31,7 @@
<RepositoryUrl>https://github.com/visus-io/cuid.net</RepositoryUrl>
</PropertyGroup>

<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<PropertyGroup Condition="'$(CI)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>

Expand All @@ -43,7 +44,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="OnixLabs.Security.Cryptography" Version="5.0.0" />
<PackageReference Include="NSec.Cryptography" Version="22.4.0" />
</ItemGroup>

<ItemGroup>
Expand Down
40 changes: 20 additions & 20 deletions src/cuid.net/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@
"Microsoft.SourceLink.Common": "1.1.1"
}
},
"OnixLabs.Security.Cryptography": {
"NSec.Cryptography": {
"type": "Direct",
"requested": "[5.0.0, )",
"resolved": "5.0.0",
"contentHash": "wIzt0phTiztuBal5GKPe6+QuFj8MvsPVK6tOzSDs0iZ8dG8LwNXVpDZENNjhYUFD873tBoT1x66SBPkKmg5LiA==",
"requested": "[22.4.0, )",
"resolved": "22.4.0",
"contentHash": "lEntcPYd7h3aZ8xxi/y/4TML7o8w0GEGqd+w4L1omqFLbdCBmhxJAeO2YBmv/fXbJKgKCQLm7+TD4bR605PEUQ==",
"dependencies": {
"OnixLabs.Core": "5.0.0"
"libsodium": "[1.0.18.2, 1.0.19)"
}
},
"libsodium": {
"type": "Transitive",
"resolved": "1.0.18.2",
"contentHash": "flArHoVdscSzyV8ZdPV+bqqY2TTFlaN+xZf/vIqsmHI51KVcD/mOdUPaK3n/k/wGKz8dppiktXUqSmf3AXFgig=="
},
"Microsoft.Build.Tasks.Git": {
"type": "Transitive",
"resolved": "1.1.1",
Expand All @@ -36,11 +41,6 @@
"type": "Transitive",
"resolved": "1.1.1",
"contentHash": "WMcGpWKrmJmzrNeuaEb23bEMnbtR/vLmvZtkAP5qWu7vQsY59GqfRJd65sFpBszbd2k/bQ8cs8eWawQKAabkVg=="
},
"OnixLabs.Core": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "Ia0z1wVZFSdfOlrlb+30pRMV0NdCjUnLdwJYtxhgmISS6nfOrOH67AQjBdZvs4h3+ajRq7SZbujYGxveCgogWw=="
}
},
"net7.0": {
Expand All @@ -60,15 +60,20 @@
"Microsoft.SourceLink.Common": "1.1.1"
}
},
"OnixLabs.Security.Cryptography": {
"NSec.Cryptography": {
"type": "Direct",
"requested": "[5.0.0, )",
"resolved": "5.0.0",
"contentHash": "wIzt0phTiztuBal5GKPe6+QuFj8MvsPVK6tOzSDs0iZ8dG8LwNXVpDZENNjhYUFD873tBoT1x66SBPkKmg5LiA==",
"requested": "[22.4.0, )",
"resolved": "22.4.0",
"contentHash": "lEntcPYd7h3aZ8xxi/y/4TML7o8w0GEGqd+w4L1omqFLbdCBmhxJAeO2YBmv/fXbJKgKCQLm7+TD4bR605PEUQ==",
"dependencies": {
"OnixLabs.Core": "5.0.0"
"libsodium": "[1.0.18.2, 1.0.19)"
}
},
"libsodium": {
"type": "Transitive",
"resolved": "1.0.18.2",
"contentHash": "flArHoVdscSzyV8ZdPV+bqqY2TTFlaN+xZf/vIqsmHI51KVcD/mOdUPaK3n/k/wGKz8dppiktXUqSmf3AXFgig=="
},
"Microsoft.Build.Tasks.Git": {
"type": "Transitive",
"resolved": "1.1.1",
Expand All @@ -78,11 +83,6 @@
"type": "Transitive",
"resolved": "1.1.1",
"contentHash": "WMcGpWKrmJmzrNeuaEb23bEMnbtR/vLmvZtkAP5qWu7vQsY59GqfRJd65sFpBszbd2k/bQ8cs8eWawQKAabkVg=="
},
"OnixLabs.Core": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "Ia0z1wVZFSdfOlrlb+30pRMV0NdCjUnLdwJYtxhgmISS6nfOrOH67AQjBdZvs4h3+ajRq7SZbujYGxveCgogWw=="
}
}
}
Expand Down
117 changes: 0 additions & 117 deletions tests/cuid.net.benchmarks/Program.cs

This file was deleted.

20 changes: 0 additions & 20 deletions tests/cuid.net.benchmarks/cuid.net.benchmarks.csproj

This file was deleted.

Loading

0 comments on commit b1f15d6

Please sign in to comment.