Skip to content

OnixLabs.Security.Cryptography.ReadOnlySpanExtensions

matthew edited this page May 27, 2026 · 1 revision

ReadOnlySpanExtensions Class

Provides extension methods for ReadOnlySpan<byte> instances, offering conversions into cryptographic primitive types defined in OnixLabs.Security.Cryptography.

Namespace OnixLabs.Security.Cryptography
Assembly OnixLabs.Security.Cryptography.dll (NuGet)
Inheritance object → ReadOnlySpanExtensions

Definition

[EditorBrowsable(EditorBrowsableState.Never)]
public static class ReadOnlySpanExtensions

Remarks

ReadOnlySpanExtensions is a public static extension container decorated with [EditorBrowsable(EditorBrowsableState.Never)], which keeps the class itself out of IntelliSense lists while its extension methods remain fully usable on ReadOnlySpan<byte> receivers. Bring the methods into scope with using OnixLabs.Security.Cryptography;.

The class uses C# 14 extension blocks (extension(ReadOnlySpan<byte> receiver) { ... }), so the receiver type appears in the block declaration rather than as a this parameter. The signatures below are the call-site signatures as consumed by user code.

The conversion methods delegate to the implicit conversion operators defined on Hash and Secret respectively, providing a fluent, method-chain-friendly alternative to an explicit cast.

Note This ReadOnlySpanExtensions class is the one in the OnixLabs.Security.Cryptography namespace and contains only the two cryptographic-primitive conversion methods below. It is distinct from any similarly named extension class in OnixLabs.Core.

Methods

Method Returns Description
ToHash() Hash Converts the current ReadOnlySpan<byte> into a new Hash instance.
ToSecret() Secret Converts the current ReadOnlySpan<byte> into a new Secret instance.

ToHash()

public Hash ToHash()

Converts the current ReadOnlySpan<byte> into a new Hash instance by implicitly converting the span.

Returns — A new Hash instance created from the current ReadOnlySpan<byte>.

using OnixLabs.Security.Cryptography;

ReadOnlySpan<byte> bytes = [0xde, 0xad, 0xbe, 0xef];
Hash hash = bytes.ToHash();

ToSecret()

public Secret ToSecret()

Converts the current ReadOnlySpan<byte> into a new Secret instance by implicitly converting the span.

Returns — A new Secret instance created from the current ReadOnlySpan<byte>.

using OnixLabs.Security.Cryptography;

ReadOnlySpan<byte> keyMaterial = [0x00, 0x01, 0x02, 0x03];
Secret secret = keyMaterial.ToSecret();

Examples

Using ToHash() in a hash-computation pipeline

using System.Security.Cryptography;
using OnixLabs.Security.Cryptography;

byte[] raw = System.Text.Encoding.UTF8.GetBytes("Hello, World!");
using HashAlgorithm sha256 = SHA256.Create();

Hash hash = sha256.ComputeHash((ReadOnlySpan<byte>)raw).AsSpan().ToHash();
Console.WriteLine(hash);

Protecting a key material span as a Secret

using OnixLabs.Security.Cryptography;

byte[] keyBytes = DeriveKeyMaterial();
Secret secret = ((ReadOnlySpan<byte>)keyBytes).ToSecret();

See Also

ONIXLabs .NET Library


Clone this wiki locally