From 44044eb0fb28b774773e3284fd147c91d59d64e3 Mon Sep 17 00:00:00 2001 From: vnugent Date: Fri, 18 Oct 2024 22:10:17 -0400 Subject: refactor: Wire up unit testing and refactor c# api --- .../src/NoscryptLibrary.cs | 56 +++++++++------------- 1 file changed, 23 insertions(+), 33 deletions(-) (limited to 'wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/NoscryptLibrary.cs') diff --git a/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/NoscryptLibrary.cs b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/NoscryptLibrary.cs index 35c6a49..2df63eb 100644 --- a/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/NoscryptLibrary.cs +++ b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/NoscryptLibrary.cs @@ -37,6 +37,7 @@ namespace VNLib.Utils.Cryptography.Noscrypt public unsafe sealed class NoscryptLibrary(SafeLibraryHandle Library, bool OwnsHandle) : VnDisposeable { public const string NoscryptDefaultLibraryName = "noscrypt"; + public const string NoscryptDllPathEnvName = "NOSCRYPT_DLL_PATH"; //Constant values match the noscrypt.h header public const int NC_SEC_KEY_SIZE = 0x20; @@ -172,33 +173,16 @@ namespace VNLib.Utils.Cryptography.Noscrypt } /// - /// Initializes a new NostrCrypto context wraper directly that owns the internal context. - /// This may be done once at app startup and is thread-safe for the rest of the - /// application lifetime. - /// - /// The heap to allocate the context from - /// The random entropy data to initialize the context with - /// The library wrapper handle - public NostrCrypto InitializeCrypto(IUnmangedHeap heap, ReadOnlySpan entropy32) - { - ArgumentNullException.ThrowIfNull(heap); - - //Create the crypto interface from the new context object - return new NostrCrypto( - context: Initialize(heap, entropy32), - ownsContext: true - ); - } - - /// - /// Initializes a new NostrCrypto context wraper directly that owns the internal context. - /// This may be done once at app startup and is thread-safe for the rest of the - /// application lifetime. + /// Initialize a new NCContext for use. This may be done once at app startup + /// and is thread-safe for the rest of the application lifetime. /// - /// The heap to allocate the context from - /// Random source used to generate context entropy - /// The library wrapper handle - public NostrCrypto InitializeCrypto(IUnmangedHeap heap, IRandomSource random) + /// + /// The 32byte random seed/nonce for the noscrypt context + /// The inialized context + /// + /// + /// + public NCContext Initialize(IUnmangedHeap heap, IRandomSource random) { ArgumentNullException.ThrowIfNull(random); @@ -206,13 +190,9 @@ namespace VNLib.Utils.Cryptography.Noscrypt Span entropy = stackalloc byte[NC_CTX_ENTROPY_SIZE]; random.GetRandomBytes(entropy); - NostrCrypto nc = InitializeCrypto(heap, entropy); - - MemoryUtil.InitializeBlock(entropy); - - return nc; + return Initialize(heap, entropy); } - + /// protected override void Free() { @@ -256,6 +236,16 @@ namespace VNLib.Utils.Cryptography.Noscrypt /// /// The loaded library instance /// - public static NoscryptLibrary LoadDefault() => Load(NoscryptDefaultLibraryName, DllImportSearchPath.SafeDirectories); + public static NoscryptLibrary LoadDefault() + { + string? libPath = Environment.GetEnvironmentVariable(NoscryptDllPathEnvName); + libPath ??= NoscryptDefaultLibraryName; + + Console.WriteLine("Loading library {0}", libPath); + + libPath = libPath.Replace("\"", ""); + + return Load(libPath); + } } } -- cgit