Class MultiHash
A protocol for differentiating outputs from various well-established cryptographic hash functions, addressing size + encoding considerations.
Implements
Inherited Members
Namespace: Ipfs
Assembly: Ipfs.Core.dll
Syntax
[JsonConverter(typeof(MultiHash.Json))]
public class MultiHash : IEquatable<MultiHash>
Remarks
See the HashingAlgorithm for supported algorithms.
Constructors
| Improve this Doc View SourceMultiHash(CodedInputStream)
Creates a new instance of the MultiHash class from the specified Google.Protobuf.CodedInputStream.
Declaration
public MultiHash(CodedInputStream stream)
Parameters
Type | Name | Description |
---|---|---|
Google.Protobuf.CodedInputStream | stream | A Google.Protobuf.CodedInputStream containing the binary representation of the MultiHash. |
Remarks
Reads the binary representation of MultiHash from the stream
.
The binary representation is a Varint of the Code, Varint of the DigestSize followed by the Digest.
When an unknown Code is encountered a new hashing algorithm is Register(String, Int32, Int32, Func<HashAlgorithm>). This new algorithm does not support matching nor computing a hash. This behaviour allows parsing of any well formed MultiHash even when the hashing algorithm is unknown.
MultiHash(Byte[])
Creates a new instance of the MultiHash class from the specified byte array.
Declaration
public MultiHash(byte[] buffer)
Parameters
Type | Name | Description |
---|---|---|
Byte[] | buffer | A sequence of bytes containing the binary representation of the MultiHash. |
Remarks
Reads the binary representation of MultiHash from the buffer
.
The binary representation is a Varint of the Code, Varint of the DigestSize followed by the Digest.
When an unknown Code is encountered a new hashing algorithm is Register(String, Int32, Int32, Func<HashAlgorithm>). This new algorithm does not support matching nor computing a hash. This behaviour allows parsing of any well formed MultiHash even when the hashing algorithm is unknown.
See Also
| Improve this Doc View SourceMultiHash(Stream)
Declaration
public MultiHash(Stream stream)
Parameters
Type | Name | Description |
---|---|---|
Stream | stream | A Stream containing the binary representation of the MultiHash. |
Remarks
Reads the binary representation of MultiHash from the stream
.
The binary representation is a Varint of the Code, Varint of the DigestSize followed by the Digest.
When an unknown Code is encountered a new hashing algorithm is Register(String, Int32, Int32, Func<HashAlgorithm>). This new algorithm does not support matching nor computing a hash. This behaviour allows parsing of any well formed MultiHash even when the hashing algorithm is unknown.
MultiHash(String)
Declaration
public MultiHash(string s)
Parameters
Type | Name | Description |
---|---|---|
String | s | A Base58 encoded MultiHash. |
Remarks
When an unknown Code is encountered a new hashing algorithm is Register(String, Int32, Int32, Func<HashAlgorithm>). This new algorithm does not support matching nor computing a hash. This behaviour allows parsing of any well formed MultiHash even when the hashing algorithm is unknown.
See Also
| Improve this Doc View SourceMultiHash(String, Byte[])
Creates a new instance of the MultiHash class with the specified HashingAlgorithm and Digest value.
Declaration
public MultiHash(string algorithmName, byte[] digest)
Parameters
Type | Name | Description |
---|---|---|
String | algorithmName | A valid IPFS hashing algorithm name, e.g. "sha2-256" or "sha2-512". |
Byte[] | digest | The digest value as a byte array. |
Fields
| Improve this Doc View SourceDefaultAlgorithmName
The default hashing algorithm is "sha2-256".
Declaration
public const string DefaultAlgorithmName = "sha2-256"
Field Value
Type | Description |
---|---|
String |
UnknownHashingAlgorithm
Occurs when an unknown hashing algorithm number is parsed.
Declaration
public static EventHandler<UnknownHashingAlgorithmEventArgs> UnknownHashingAlgorithm
Field Value
Type | Description |
---|---|
EventHandler<UnknownHashingAlgorithmEventArgs> |
Properties
| Improve this Doc View SourceAlgorithm
The hashing algorithm.
Declaration
public HashingAlgorithm Algorithm { get; }
Property Value
Type | Description |
---|---|
HashingAlgorithm | Details on the hashing algorithm. |
Digest
The hashing algorithm's digest value.
Declaration
public byte[] Digest { get; }
Property Value
Type | Description |
---|---|
Byte[] | The output of the hashing algorithm. |
IsIdentityHash
Determines if the identity hash algorithm is in use.
Declaration
public bool IsIdentityHash { get; }
Property Value
Type | Description |
---|---|
Boolean | true if the identity hash algorithm is used; otherwise, false. |
Remarks
The identity hash is used to inline a small amount of data into a Cid. When true, the Digest is also the content.
Methods
| Improve this Doc View SourceComputeHash(Byte[], String)
Generate the multihash for the specified byte array.
Declaration
public static MultiHash ComputeHash(byte[] data, string algorithmName = "sha2-256")
Parameters
Type | Name | Description |
---|---|---|
Byte[] | data | The byte array containing the data to hash. |
String | algorithmName | The name of the hashing algorithm to use; defaults to DefaultAlgorithmName. |
Returns
Type | Description |
---|---|
MultiHash | A MultiHash for the |
ComputeHash(Stream, String)
Generate the multihash for the specified Stream.
Declaration
public static MultiHash ComputeHash(Stream data, string algorithmName = "sha2-256")
Parameters
Type | Name | Description |
---|---|---|
Stream | data | The Stream containing the data to hash. |
String | algorithmName | The name of the hashing algorithm to use; defaults to DefaultAlgorithmName. |
Returns
Type | Description |
---|---|
MultiHash | A MultiHash for the |
Equals(MultiHash)
Declaration
public bool Equals(MultiHash that)
Parameters
Type | Name | Description |
---|---|---|
MultiHash | that |
Returns
Type | Description |
---|---|
Boolean |
Equals(Object)
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
Object | obj |
Returns
Type | Description |
---|---|
Boolean |
Overrides
| Improve this Doc View SourceGetHashAlgorithm(String)
Gets the HashAlgorithm with the specified IPFS multi-hash name.
Declaration
public static HashAlgorithm GetHashAlgorithm(string name = "sha2-256")
Parameters
Type | Name | Description |
---|---|---|
String | name | The name of a hashing algorithm, see https://github.com/multiformats/multicodec/blob/master/table.csv for IPFS defined names. |
Returns
Type | Description |
---|---|
HashAlgorithm | The hashing implementation associated with the |
Exceptions
Type | Condition |
---|---|
KeyNotFoundException | When |
GetHashAlgorithmName(Int32)
Gets the name of hashing algorithm name with the specified code.
Declaration
public static string GetHashAlgorithmName(int code)
Parameters
Type | Name | Description |
---|---|---|
Int32 | code | The code of a hashing algorithm, see https://github.com/multiformats/multicodec/blob/master/table.csv for IPFS defined codes. |
Returns
Type | Description |
---|---|
String | The name assigned to |
Exceptions
Type | Condition |
---|---|
KeyNotFoundException | When |
GetHashCode()
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
Int32 |
Overrides
| Improve this Doc View SourceMatches(Byte[])
Determines if the data matches the hash.
Declaration
public bool Matches(byte[] data)
Parameters
Type | Name | Description |
---|---|---|
Byte[] | data | The data to check. |
Returns
Type | Description |
---|---|
Boolean | true if the data matches the MultiHash; otherwise, false. |
Remarks
Matches is used to ensure data integrity.
Matches(Stream)
Determines if the stream data matches the hash.
Declaration
public bool Matches(Stream data)
Parameters
Type | Name | Description |
---|---|---|
Stream | data | The Stream containing the data to check. |
Returns
Type | Description |
---|---|
Boolean | true if the data matches the MultiHash; otherwise, false. |
Remarks
Matches is used to ensure data integrity.
ToArray()
Returns the IPFS binary representation as a byte array.
Declaration
public byte[] ToArray()
Returns
Type | Description |
---|---|
Byte[] | A byte array. |
Remarks
The binary representation is a sequence of MultiHash.
ToBase32()
Declaration
public string ToBase32()
Returns
Type | Description |
---|---|
String |
ToBase58()
Declaration
public string ToBase58()
Returns
Type | Description |
---|---|
String |
ToString()
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
String | A base-58 representaton of the MultiHash. |
Overrides
See Also
| Improve this Doc View SourceWrite(CodedOutputStream)
Writes the binary representation of the multihash to the specified Google.Protobuf.CodedOutputStream.
Declaration
public void Write(CodedOutputStream stream)
Parameters
Type | Name | Description |
---|---|---|
Google.Protobuf.CodedOutputStream | stream | The Google.Protobuf.CodedOutputStream to write to. |
Remarks
The binary representation is a Varint of the Code, Varint of the DigestSize followed by the Digest.
Write(Stream)
Writes the binary representation of the multihash to the specified Stream.
Declaration
public void Write(Stream stream)
Parameters
Type | Name | Description |
---|---|---|
Stream | stream | The Stream to write to. |
Remarks
The binary representation is a 1-byte Code, 1-byte DigestSize followed by the Digest.
Operators
| Improve this Doc View SourceEquality(MultiHash, MultiHash)
Value equality.
Declaration
public static bool operator ==(MultiHash a, MultiHash b)
Parameters
Type | Name | Description |
---|---|---|
MultiHash | a | |
MultiHash | b |
Returns
Type | Description |
---|---|
Boolean |
Implicit(String to MultiHash)
Declaration
public static implicit operator MultiHash(string s)
Parameters
Type | Name | Description |
---|---|---|
String | s | A Base58 encoded MultiHash. |
Returns
Type | Description |
---|---|
MultiHash | A new MultiHash. |
Remarks
Equivalent to
new MultiHash(s)
Inequality(MultiHash, MultiHash)
Value inequality.
Declaration
public static bool operator !=(MultiHash a, MultiHash b)
Parameters
Type | Name | Description |
---|---|---|
MultiHash | a | |
MultiHash | b |
Returns
Type | Description |
---|---|
Boolean |