NCode.CryptoMemory 1.0.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package NCode.CryptoMemory --version 1.0.0                
NuGet\Install-Package NCode.CryptoMemory -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NCode.CryptoMemory" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NCode.CryptoMemory --version 1.0.0                
#r "nuget: NCode.CryptoMemory, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NCode.CryptoMemory as a Cake Addin
#addin nuget:?package=NCode.CryptoMemory&version=1.0.0

// Install NCode.CryptoMemory as a Cake Tool
#tool nuget:?package=NCode.CryptoMemory&version=1.0.0                

ci

NCode.CryptoMemory

Provides the ability to manage the lifetime of memory by pinning buffers to prevent duplicate copies in ram and securely zeroing sensitive data when no longer needed.

API

CryptoPool

namespace NCode.CryptoMemory;

/// <summary>
/// Provides a resource pool that enables reusing instances of byte arrays that are pinned during their lifetime and securely zeroed when returned.
/// </summary>
public static class CryptoPool
{
    /// <summary>
    /// Retrieves a buffer that is at least the requested length.
    /// </summary>
    /// <param name="minBufferSize">The minimum length of the buffer needed.</param>
    /// <returns>
    /// An <see cref="IMemoryOwner{T}"/> that manages the lifetime of the lease.
    /// </returns>
    public static IMemoryOwner<byte> Rent(int minBufferSize);

    /// <summary>
    /// Retrieves a buffer that is at least the requested length.
    /// </summary>
    /// <param name="minBufferSize">The minimum length of the buffer needed.</param>
    /// <param name="isSensitive">Indicates whether the buffer should be pinned during it's lifetime and securely zeroed when returned.
    /// When <c>false></c>, this implementation delegates to <c>MemoryPool&lt;byte&gt;.Shared.Rent</c>.</param>
    /// <returns>
    /// An <see cref="IMemoryOwner{T}"/> that manages the lifetime of the lease.
    /// </returns>
    public static IMemoryOwner<byte> Rent(int minBufferSize, bool isSensitive);

    /// <summary>
    /// Retrieves a buffer that is at least the requested length.
    /// </summary>
    /// <param name="minBufferSize">The minimum length of the buffer needed.</param>
    /// <param name="buffer">When this method returns, contains the buffer with the exact requested size.</param>
    /// <returns>
    /// An <see cref="IMemoryOwner{T}"/> that manages the lifetime of the lease.
    /// </returns>
    public static IMemoryOwner<byte> Rent(int minBufferSize, out Span<byte> buffer);

    /// <summary>
    /// Retrieves a buffer that is at least the requested length.
    /// </summary>
    /// <param name="minBufferSize">The minimum length of the buffer needed.</param>
    /// <param name="isSensitive">Indicates whether the buffer should be pinned during it's lifetime and securely zeroed when returned.
    /// When <c>false></c>, this implementation delegates to <c>MemoryPool&lt;byte&gt;.Shared.Rent</c>.</param>
    /// <param name="buffer">When this method returns, contains the buffer with the exact requested size.</param>
    /// <returns>
    /// An <see cref="IMemoryOwner{T}"/> that manages the lifetime of the lease.
    /// </returns>
    public static IMemoryOwner<byte> Rent(int minBufferSize, bool isSensitive, out Span<byte> buffer);

    /// <summary>
    /// Retrieves a buffer that is at least the requested length.
    /// </summary>
    /// <param name="minBufferSize">The minimum length of the buffer needed.</param>
    /// <param name="buffer">When this method returns, contains the buffer with the exact requested size.</param>
    /// <returns>
    /// An <see cref="IMemoryOwner{T}"/> that manages the lifetime of the lease.
    /// </returns>
    public static IMemoryOwner<byte> Rent(int minBufferSize, out Memory<byte> buffer);

    /// <summary>
    /// Retrieves a buffer that is at least the requested length.
    /// </summary>
    /// <param name="minBufferSize">The minimum length of the buffer needed.</param>
    /// <param name="isSensitive">Indicates whether the buffer should be pinned during it's lifetime and securely zeroed when returned.
    /// When <c>false></c>, this implementation delegates to <c>MemoryPool&lt;byte&gt;.Shared.Rent</c>.</param>
    /// <param name="buffer">When this method returns, contains the buffer with the exact requested size.</param>
    /// <returns>
    /// An <see cref="IMemoryOwner{T}"/> that manages the lifetime of the lease.
    /// </returns>
    public static IMemoryOwner<byte> Rent(int minBufferSize, bool isSensitive, out Memory<byte> buffer);
}

HeapMemoryManager

namespace NCode.CryptoMemory;

/// <summary>
/// Provides an implementation of <see cref="MemoryManager{T}"/> that allocates a byte buffer from the heap and
/// securely zeroes the allocated memory when the managed is disposed.
/// </summary>
public class HeapMemoryManager : MemoryManager<byte>
{
    /// <summary>
    /// Initializes a new instance of the <see cref="HeapMemoryManager"/> class.
    /// </summary>
    /// <param name="length">The length in bytes of the allocated memory.</param>
    /// <param name="zeroOnDispose">Specifies whether the allocated memory should be zeroed when the manager is disposed.</param>
    public HeapMemoryManager(int length, bool zeroOnDispose = true);

    /// <summary>
    /// Gets the memory block handled by this <see cref="MemoryManager{T}"/>.
    /// </summary>
    public Memory<byte> Memory { get; }

    /// <summary>
    /// Returns a memory span that wraps the underlying memory buffer.
    /// </summary>
    public Span<byte> GetSpan();

    /// <summary>
    /// Returns a handle to the memory that has been pinned and whose address can be taken.
    /// </summary>
    /// <param name="elementIndex">The offset to the element in the memory buffer at which the returned <see cref="MemoryHandle"/> points.</param>
    public MemoryHandle Pin(int elementIndex = 0);

    /// <summary>
    /// Unpins pinned memory so that the garbage collector is free to move it.
    /// </summary>
    /// <remarks>
    /// Since this <see cref="MemoryManager{T}"/> uses memory from the heap which is already pinned,
    /// this implemtation does nothing.
    /// </remarks>
    public void Unpin();
}

Release Notes

  • v1.0.0 - Initial release
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 212 7/23/2023
1.0.0 165 7/23/2023

Built on 2023-07-23 19:36:59Z