SensitiveData Class

Provides factory methods for creating SensitiveData<T> instances.

public static class SensitiveData

Example

// Create sensitive data from a string
using var secret = SensitiveData.Create("my-password");

// Reveal the data when needed
string password = secret.RevealToString();
Assert.Equal("my-password", password);

// Create sensitive data from a byte array
byte[] key = [1, 2, 3, 4, 5];
using var sensitiveKey = SensitiveData.Create(key);
byte[] revealedKey = sensitiveKey.RevealToArray();
Assert.Equal(key, revealedKey);

Methods

NameDescription
Create(string)Creates an instance of SensitiveData<T> from a string.
Create<T>(ReadOnlySpan<T>)Creates an instance of SensitiveData<T> from a read-only span.
Create<T>(T[])Creates an instance of SensitiveData<T> from an array.
RevealToString(SensitiveData<char>)Reveals the contents of a SensitiveData<T> instance as a string.

Create overloads

Create(string)

SensitiveData.cs

Creates an instance of SensitiveData<T> from a string.

public static SensitiveData<char> Create(string value)
Parameters
value string
The sensitive string data to protect.
Returns

SensitiveData<char> A new SensitiveData<T> instance containing the string contents.

Create<T>(ReadOnlySpan<T>)

SensitiveData.cs

Creates an instance of SensitiveData<T> from a read-only span.

public static SensitiveData<T> Create<T>(ReadOnlySpan<T> buffer) where T : unmanaged
Type parameters
T
The unmanaged type of elements in the buffer.
Parameters
buffer ReadOnlySpan<T>
The read-only span containing sensitive data to protect.
Returns

SensitiveData<T> A new SensitiveData<T> instance containing a copy of the buffer contents.

Create<T>(T[])

SensitiveData.cs

Creates an instance of SensitiveData<T> from an array.

public static SensitiveData<T> Create<T>(T[] buffer) where T : unmanaged
Type parameters
T
The unmanaged type of elements in the buffer.
Parameters
buffer T[]
The buffer containing sensitive data to protect.
Returns

SensitiveData<T> A new SensitiveData<T> instance containing a copy of the buffer contents.

RevealToString(SensitiveData<char>)

SensitiveData.cs

Reveals the contents of a SensitiveData<T> instance as a string.

public static string RevealToString(this SensitiveData<char> secret)

Parameters

secret SensitiveData<char>
The sensitive data to reveal.

Returns

string A string containing the revealed sensitive data.

Exceptions

ObjectDisposedException
The instance has already been disposed.