Table of Contents

Class CryptoSha256

Namespace
LibSodium
Assembly
LibSodium.Net.dll

Provides one‑shot and streaming SHA‑256 hashing helpers built on libsodium’s crypto_hash_sha256 API.

public static class CryptoSha256
Inheritance
CryptoSha256
Inherited Members

Fields

HashLen

Hash length in bytes (32).

public const int HashLen = 32

Field Value

int

Methods

ComputeHash(Span<byte>, Stream)

Computes a SHA‑256 hash over the entire contents of the supplied Stream.

public static void ComputeHash(Span<byte> hash, Stream input)

Parameters

hash Span<byte>

Destination buffer (32 bytes) that receives the final hash.

input Stream

The input stream to read and hash. The stream is read until its end.

Remarks

The method processes the stream in buffered chunks of 8 KiB, keeping memory usage low even for very large inputs.

Exceptions

ArgumentNullException

Thrown if input is null.

ArgumentException

Thrown if hash is not exactly 32 bytes.

LibSodiumException

Thrown if the underlying libsodium call fails.

ComputeHash(Span<byte>, ReadOnlySpan<byte>)

Computes a SHA‑256 hash of message and stores the result in hash.

public static void ComputeHash(Span<byte> hash, ReadOnlySpan<byte> message)

Parameters

hash Span<byte>

Destination buffer (32 bytes).

message ReadOnlySpan<byte>

Message to hash.

Exceptions

ArgumentException

If hash length ≠ 32.

LibSodiumException

If the native function returns non‑zero.

ComputeHashAsync(Memory<byte>, Stream, CancellationToken)

Asynchronously computes a SHA‑256 hash over the supplied Stream, writing the result into hash.

public static Task ComputeHashAsync(Memory<byte> hash, Stream input, CancellationToken cancellationToken = default)

Parameters

hash Memory<byte>

Destination memory buffer (32 bytes) that receives the final hash.

input Stream

The input stream to read and hash. The stream is read until its end.

cancellationToken CancellationToken

Token that can be used to cancel the asynchronous operation.

Returns

Task

A task that completes when the hash has been fully computed and written.

Remarks

The method reads the stream in buffered chunks of 8 KiB and is fully asynchronous, making it suitable for hashing network streams or large files without blocking the calling thread.

Exceptions

ArgumentNullException

Thrown if input is null.

ArgumentException

Thrown if hash is not exactly 32 bytes.

LibSodiumException

Thrown if the underlying libsodium call fails.

CreateIncrementalHash()

Creates a new instance of an incremental hash computation object using the SHA-512 algorithm.

public static ICryptoIncrementalOperation CreateIncrementalHash()

Returns

ICryptoIncrementalOperation

An ICryptoIncrementalOperation instance that allows incremental computation of a SHA-512 hash.

Remarks

This method provides an object for computing a hash incrementally, which is useful for processing large data streams or when the data to be hashed is not available all at once.

Exceptions

LibSodiumException

Thrown if the underlying libsodium call fails.