Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup dotnet
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.x'
dotnet-version: '9.x'

- name: Check formatting
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
name: Setup dotnet
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.x'
dotnet-version: '9.x'

- if: ${{ fromJSON(steps.tag.outputs.create) }}
name: Pack
Expand Down
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,31 @@ jobs:
dotnet-version: |
6.x
8.x
9.x

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.x'

- name: Generate Go Test Data
run: |
go get -u all
go mod tidy
go run main.go > NATS.NKeys.Tests/test_data.json

- name: Build
run: dotnet build -c Debug

- name: Test Core
run: dotnet test -c Debug --no-build --logger:"console;verbosity=normal"

- name: Test Compare Go Impl
run: |
cd nkTestGen
dotnet run > test_data.json
go test

- name: Check Native AOT
shell: bash
run: |
Expand Down
2 changes: 1 addition & 1 deletion NATS.NKeys.Benchmarks/NKeysReference2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ internal class NKeysReference2Base32
{
public static byte[] Decode(ReadOnlySpan<char> input)
{
if (input == null || input.Length == 0)
if (input.IsEmpty)
{
throw new ArgumentNullException(nameof(input));
}
Expand Down
11 changes: 9 additions & 2 deletions NATS.NKeys.Tests/NATS.NKeys.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net462;net48;net6.0;net8.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">net6.0;net8.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net462;net48;net8.0;net9.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">net8.0;net9.0</TargetFrameworks>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
<PackageReference Include="System.Text.Json" Version="9.0.3" />
<PackageReference Include="xunit" Version="2.4.2"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down Expand Up @@ -36,4 +37,10 @@
</Compile>
</ItemGroup>

<ItemGroup>
<None Update="test_data.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
78 changes: 78 additions & 0 deletions NATS.NKeys.Tests/NKeysTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.Json.Nodes;
using NATS.NKeys.Benchmarks;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -29,6 +33,80 @@ public void XKey_from_seed()
Assert.Equal("Invalid curve key operation", exception.Message);
}

[Fact]
public void XKey_seal_open_compare_to_Go_nkeys_library()
{
// Read test data generated by Go nkeys library
var json = JsonNode.Parse(File.ReadAllText("test_data.json"));
foreach (var data in json!["xkeys"]!.AsArray())
{
var seed1 = data!["seed1"]!.GetValue<string>();
var pk1 = data!["pk1"]!.GetValue<string>();
var seed2 = data!["seed2"]!.GetValue<string>();
var pk2 = data!["pk2"]!.GetValue<string>();
var text1 = data!["text"]!.GetValue<string>();
var cypherText = data!["cypher_text"]!.GetValue<string>();
var openText = data!["open_text"]!.GetValue<string>();

var kp1 = KeyPair.FromSeed(seed1.ToCharArray());
var kp2 = KeyPair.FromSeed(seed2.ToCharArray());

// Double check data
Assert.Equal(text1, openText);
Assert.Equal(pk1, kp1.GetPublicKey());
Assert.Equal(pk2, kp2.GetPublicKey());

// Open cipher sealed by Go nkeys library
var open2 = kp2.Open(Convert.FromBase64String(cypherText), pk1);
var text2 = Convert.ToBase64String(open2);
Assert.Equal(text1, text2);

// Double check seal since we can't compare sealed data since
// it'd be different every time because of nonce
var seal = kp1.Seal(Convert.FromBase64String(text1), pk2);
var open3 = kp2.Open(seal, pk1);
var text3 = Convert.ToBase64String(open3);
Assert.Equal(text1, text3);
}
}

[Fact]
public void XKey_seal_open()
{
var kp1 = KeyPair.FromSeed("SXAD4F52S2XAJTJ3TGDJ4VXQVW7TU35XJUSVKF25ZRXIWCIUK6NLANRHVY".ToCharArray());
var pk1 = kp1.GetPublicKey();
output.WriteLine($"pk1: {pk1}");

var kp2 = KeyPair.FromSeed("SXAAPCLFDKJ3FOGPXVS5OT3ZRYT5CZHVRRJUEZKHHJNFQQI5IURJ3IN3OU".ToCharArray());
var pk2 = kp2.GetPublicKey();
output.WriteLine($"pk2: {pk2}");

var seal = kp1.Seal(Encoding.UTF8.GetBytes("hello"), pk2);

var seal64 = Convert.ToBase64String(seal);
output.WriteLine($"seal64: {seal64}");

foreach (var sealed64Text in new[]
{
seal64,

// generated using nkeys Go library:
"eGt2MchZLvgdJ5IKV4ZD/pAHnpwGMw2D2j7eA/vjRBhZzWmQAvbTzMTu8M1aevP4Gg==",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3

"eGt2MRpP/wq2ckUO5PMs8ao4EB2Vrb6uMdxspLnxsqQXT7HUoYk/WdhK49WVVbK70w==",
"eGt2MZ0ka1T50jsi8YcP/xYRW+gG/Cg0Mufkmlo7bpOHrCHEvkOmaRcIJ6xzpFtqww==",
"eGt2MUNXS9+hvSaH5uTWJ5xpDNJuGCfBUxTKm4BD9OdoPkYNEiv9mr63dUZTjcdmNQ==",
"eGt2MUNTNs7wYhKx9wJ+k/YXOAEhbrqHsRQXZpMb5WXtMqFxkDj7S1iRSCJme0Eubg==",
"eGt2Mekw7+a2WRuMxGVtRU7dAEg0iYqTtRiei0w+nUXwdKbWgtkCgJV4rlMXJycL+Q==",
"eGt2MSpwx9uW1pbYUFGPdfQ45sASe/mvUtSfXidTi92tr6YXzaq2dHnE5xr2qLDxuw==",
})
{
var open = kp2.Open(Convert.FromBase64String(sealed64Text), pk1);
var text = Encoding.UTF8.GetString(open);
Assert.Equal("hello", text);
output.WriteLine($"open: {text}");
}
}

[Fact]
public void XKey_create()
{
Expand Down
Loading