Table of Contents

Class SecureMemory

Namespace
LibSodium
Assembly
LibSodium.Net.dll

Provides methods for secure memory management using libsodium. These methods help protect sensitive data from being swapped to disk or accessed by other processes.

public static class SecureMemory
Inheritance
SecureMemory
Inherited Members

Methods

Create<T>(int)

Creates a SecureMemory<T> holding the specified number of items of given type

public static SecureMemory<T> Create<T>(int length) where T : unmanaged

Parameters

length int

The number of items

Returns

SecureMemory<T>

Type Parameters

T

The type of items

Equals(ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Compares two byte buffers for equality in constant time.

public static bool Equals(ReadOnlySpan<byte> b1, ReadOnlySpan<byte> b2)

Parameters

b1 ReadOnlySpan<byte>

First buffer to compare.

b2 ReadOnlySpan<byte>

Second buffer to compare.

Returns

bool

True if the buffers are equal, false otherwise.

IsZero(ReadOnlySpan<byte>)

Checks if the given byte buffer is zero.

public static bool IsZero(ReadOnlySpan<byte> b)

Parameters

b ReadOnlySpan<byte>

The byte buffer to check.

Returns

bool

True if the byte buffer is zero, false otherwise.

IsZero<T>(ReadOnlySpan<T>)

Checks if the given buffer is zero.

public static bool IsZero<T>(ReadOnlySpan<T> b) where T : unmanaged

Parameters

b ReadOnlySpan<T>

The byte buffer to check.

Returns

bool

True if the byte buffer is zero, false otherwise.

Type Parameters

T

IsZero<T>(Span<T>)

Checks if the given buffer is zero.

public static bool IsZero<T>(Span<T> b) where T : unmanaged

Parameters

b Span<T>

The byte buffer to check.

Returns

bool

True if the byte buffer is zero, false otherwise.

Type Parameters

T

IsZero<T>(T[])

Checks if the given buffer is zero.

public static bool IsZero<T>(T[] b) where T : unmanaged

Parameters

b T[]

The byte buffer to check.

Returns

bool

True if the byte buffer is zero, false otherwise.

Type Parameters

T

MemLock(ReadOnlySpan<byte>)

Locks an unmanaged memory buffer, preventing it from being swapped to disk.

public static void MemLock(ReadOnlySpan<byte> buffer)

Parameters

buffer ReadOnlySpan<byte>

The span representing the unmanaged memory to lock.

Exceptions

LibSodiumException

Thrown if locking the memory fails.

MemLock<T>(ReadOnlySpan<T>)

Locks an unmanaged memory buffer, preventing it from being swapped to disk.

public static void MemLock<T>(ReadOnlySpan<T> buffer) where T : unmanaged

Parameters

buffer ReadOnlySpan<T>

The span representing the unmanaged memory to lock.

Type Parameters

T

Exceptions

LibSodiumException

Thrown if locking the memory fails.

MemUnlock(ReadOnlySpan<byte>)

Unlocks an unmanaged memory buffer, allowing it to be swapped to disk if necessary.

public static void MemUnlock(ReadOnlySpan<byte> buffer)

Parameters

buffer ReadOnlySpan<byte>

The span of bytes to unlock.

Exceptions

LibSodiumException

Thrown if unlocking the memory fails.

MemUnlock<T>(ReadOnlySpan<T>)

Unlocks an unmanaged memory buffer, allowing it to be swapped to disk if necessary.

public static void MemUnlock<T>(ReadOnlySpan<T> buffer) where T : unmanaged

Parameters

buffer ReadOnlySpan<T>

The span of bytes to unlock.

Type Parameters

T

Exceptions

LibSodiumException

Thrown if unlocking the memory fails.

MemZero(byte[])

Fills a buffer with zeros, effectively erasing its contents.

public static void MemZero(byte[] buffer)

Parameters

buffer byte[]

The span of bytes to zero out.

MemZero(Span<byte>)

Fills a buffer with zeros, effectively erasing its contents.

public static void MemZero(Span<byte> buffer)

Parameters

buffer Span<byte>

The span of bytes to zero out.

MemZero<T>(Span<T>)

Fills a buffer with zeros, effectively erasing its contents.

public static void MemZero<T>(Span<T> buffer) where T : unmanaged

Parameters

buffer Span<T>

The span of bytes to zero out.

Type Parameters

T

MemZero<T>(ref T)

Overwrites the memory of the specified buffer with zeros.

public static void MemZero<T>(ref T buffer) where T : unmanaged

Parameters

buffer T

A reference to the buffer whose memory will be cleared.

Type Parameters

T

The type of the buffer. Must be an unmanaged type.

Remarks

This method ensures that the memory of the provided buffer is securely overwritten with zeros. It is typically used to clear sensitive data, such as cryptographic keys, from memory. The method requires the library to be initialized before use.

MemZero<T>(T[])

Fills a buffer with zeros, effectively erasing its contents.

public static void MemZero<T>(T[] buffer) where T : unmanaged

Parameters

buffer T[]

The span of bytes to zero out.

Type Parameters

T