Table of Contents

Class SecureMemory<T>

Namespace
LibSodium
Assembly
LibSodium.Net.dll

Provides a secure unmanaged memory buffer for unmanaged types, using libsodium for memory protection. This class encapsulates secure memory allocation, read-only protection, and zeroing.

public sealed class SecureMemory<T> : IDisposable where T : unmanaged

Type Parameters

T

The unmanaged type of elements in the secure memory buffer.

Inheritance
SecureMemory<T>
Implements
Inherited Members

Constructors

SecureMemory(int)

Initializes a new instance of the SecureMemory<T> class with the specified length.

public SecureMemory(int length)

Parameters

length int

The number of elements of type T to allocate.

Exceptions

LibSodiumException

Thrown if memory allocation fails.

Properties

IsDisposed

Gets a value indicating whether the object has been disposed.

public bool IsDisposed { get; }

Property Value

bool

IsReadOnly

Gets a value indicating whether the memory region is read-only.

public bool IsReadOnly { get; }

Property Value

bool

Length

Gets the length of the secure unmanaged memory buffer, in number of elements of type T.

public int Length { get; }

Property Value

int

Methods

AsMemory()

Gets a Memory<T> representing the secure unmanaged memory buffer.

public Memory<T> AsMemory()

Returns

Memory<T>

Exceptions

InvalidOperationException

AsReadOnlyMemory()

Gets a ReadOnlyMemory<T> representing the secure unmanaged memory buffer.

public ReadOnlyMemory<T> AsReadOnlyMemory()

Returns

ReadOnlyMemory<T>

Exceptions

ObjectDisposedException

AsReadOnlySpan()

Gets a ReadOnlySpan<T> representing the secure unmanaged memory buffer.

public ReadOnlySpan<T> AsReadOnlySpan()

Returns

ReadOnlySpan<T>

Remarks

While this method returns a new ReadOnlySpan<T> instance on each call, all returned spans represent the same underlying memory region.

Exceptions

ObjectDisposedException

Thrown if the object has been disposed.

AsSpan()

Gets a Span<T> representing the secure unmanaged memory buffer.

public Span<T> AsSpan()

Returns

Span<T>

Remarks

While this method returns a new Span<T> instance on each call, all returned spans represent the same underlying memory region. Modifications made through one span will be visible through any other span obtained from this instance.

Exceptions

ObjectDisposedException

Thrown if the object has been disposed.

InvalidOperationException

Thrown if the memory region is read-only.

Dispose()

Releases all resources used by the SecureMemory<T> object, including the allocated unmanaged secure memory.

public void Dispose()

~SecureMemory()

Finalizes an instance of the SecureMemory<T> class.

protected ~SecureMemory()

MemZero()

Fills the secure unmanaged memory buffer with zeros, effectively erasing its contents.

public void MemZero()

Exceptions

ObjectDisposedException

Thrown if the object has been disposed.

ProtectReadOnly()

Marks the secure unmanaged memory buffer as read-only.

public void ProtectReadOnly()

Exceptions

ObjectDisposedException

Thrown if the object has been disposed.

LibSodiumException

Thrown if setting the memory to read-only fails.

ProtectReadWrite()

Marks the secure unmanaged memory buffer as read-write.

public void ProtectReadWrite()

Exceptions

ObjectDisposedException

Thrown if the object has been disposed.

LibSodiumException

Thrown if setting the memory to read-write fails.