Repository navigation
OnixLabs.Security.Cryptography.IHashable
Defines a mechanism for computing the Hash of a data structure.
| Namespace | OnixLabs.Security.Cryptography |
| Assembly | OnixLabs.Security.Cryptography.dll (NuGet) |
public interface IHashable
{
Hash ComputeHash(HashAlgorithm algorithm);
}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.
| Member | Returns | Description |
|---|---|---|
ComputeHash(HashAlgorithm) |
Hash |
Computes the hash of the current object using the specified algorithm. |
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.
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 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 rootusing 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);ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions