Skip to content

OnixLabs.Security.Cryptography.IHashable

matthew edited this page May 27, 2026 · 1 revision

IHashable Interface

Defines a mechanism for computing the Hash of a data structure.

Namespace OnixLabs.Security.Cryptography
Assembly OnixLabs.Security.Cryptography.dll (NuGet)

Definition

public interface IHashable
{
    Hash ComputeHash(HashAlgorithm algorithm);
}

Remarks

IHashable is a single-member interface. It deliberately accepts a HashAlgorithm rather than naming a fixed algorithm so that the hashing policy is decided by the caller: the same object can be hashed with SHA-256, SHA-512, or any other HashAlgorithm-derived type without the implementing type needing to change.

The return type is Hash, the library's own cryptographic-hash value type, which itself implements ICryptoPrimitive<Hash>. This means the computed hash inherits value semantics and binary conversion automatically.

Any user-defined type that can meaningfully produce a hash of its state — a message, a document, a composite object — can implement IHashable to expose that capability through a uniform API. In particular, IHashable is the required constraint on the type parameter T of MerkleTree<T>, making it the gateway for participating in Merkle tree construction.

The two interfaces IHashable and ICryptoPrimitive<T> are independent: a type may implement IHashable without implementing ICryptoPrimitive<T>, and vice versa.

Members

Member Returns Description
ComputeHash(HashAlgorithm) Hash Computes the hash of the current object using the specified algorithm.

ComputeHash(HashAlgorithm)

Hash ComputeHash(HashAlgorithm algorithm);

Computes the Hash of the current object.

Parameter Type Description
algorithm HashAlgorithm The HashAlgorithm used to compute the hash. The caller controls algorithm selection, keeping the interface algorithm-agnostic.

Returns — The computed Hash of the current object.

Examples

Implementing IHashable on a custom record

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

public record Transaction(string From, string To, decimal Amount) : IHashable
{
    public Hash ComputeHash(HashAlgorithm algorithm)
    {
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes($"{From}|{To}|{Amount}");
        return Hash.Compute(algorithm, bytes);
    }
}

Using IHashable in a Merkle tree

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

Transaction[] transactions =
[
    new Transaction("alice", "bob",   10m),
    new Transaction("bob",   "carol",  5m),
    new Transaction("carol", "alice",  3m),
];

using HashAlgorithm sha256 = SHA256.Create();

// MerkleTree<T> requires T : IHashable
MerkleTree<Transaction> tree = MerkleTree.Create(transactions, sha256);
Console.WriteLine(tree.Hash); // Merkle root

Calling ComputeHash directly

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

Transaction tx = new Transaction("alice", "bob", 10m);

using HashAlgorithm sha256 = SHA256.Create();
Hash txHash = tx.ComputeHash(sha256);
Console.WriteLine(txHash);

See Also

ONIXLabs .NET Library


Clone this wiki locally