Skip to content

Commit

Permalink
feat(HashingAlgorithm): provide public access via GetAlgorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Aug 8, 2019
1 parent f101c4d commit 2ef53e2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
21 changes: 20 additions & 1 deletion doc/articles/multihash.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,24 @@ data into a [CID](cid.md).

## Registry

The [hashing registry](xref:Ipfs.Registry.HashingAlgorithm) contains the metadata on a hashing algorithm. You can use
The [hashing registry](xref:Ipfs.Registry.HashingAlgorithm) contains the metadata on hashing algorithms. You can use
[Register](xref:Ipfs.Registry.HashingAlgorithm.Register*) to add a new hashing algorithm.

### Example

Using an hashing algorithm. Note that `ComputeHash` can take a byte array or a `Stream`.

```csharp
public void GetHasher()
{
using (var hasher = HashingAlgorithm.GetAlgorithm("sha3-256"))
{
Assert.IsNotNull(hasher);
var input = new byte[] { 0xe9 };
var expected = "f0d04dd1e6cfc29a4460d521796852f25d9ef8d28b44ee91ff5b759d72c1e6d6".ToHexBuffer();

var actual = hasher.ComputeHash(input);
CollectionAssert.AreEqual(expected, actual);
}
}
```
25 changes: 25 additions & 0 deletions src/Registry/HashingAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,30 @@ public static IEnumerable<HashingAlgorithm> All
get { return Names.Values; }
}

/// <summary>
/// Gets the <see cref="HashAlgorithm"/> with the specified IPFS multi-hash name.
/// </summary>
/// <param name="name">
/// The name of a hashing algorithm, see <see href="https://github.com/multiformats/multicodec/blob/master/table.csv"/>
/// for IPFS defined names.
/// </param>
/// <returns>
/// The hashing implementation associated with the <paramref name="name"/>.
/// </returns>
/// <exception cref="KeyNotFoundException">
/// When <paramref name="name"/> is not registered.
/// </exception>
public static HashAlgorithm GetAlgorithm(string name)
{
try
{
return HashingAlgorithm.Names[name].Hasher();
}
catch (KeyNotFoundException)
{
throw new KeyNotFoundException($"Hash algorithm '{name}' is not registered.");
}
}

}
}
20 changes: 20 additions & 0 deletions test/Registry/HashingAlgorithmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ namespace Ipfs.Registry
[TestClass]
public class HashingAlgorithmTest
{
[TestMethod]
public void GetHasher()
{
using (var hasher = HashingAlgorithm.GetAlgorithm("sha3-256"))
{
Assert.IsNotNull(hasher);
var input = new byte[] { 0xe9 };
var expected = "f0d04dd1e6cfc29a4460d521796852f25d9ef8d28b44ee91ff5b759d72c1e6d6".ToHexBuffer();

var actual = hasher.ComputeHash(input);
CollectionAssert.AreEqual(expected, actual);
}
}

[TestMethod]
public void GetHasher_Unknown()
{
ExceptionAssert.Throws<KeyNotFoundException>(() => HashingAlgorithm.GetAlgorithm("unknown"));
}

[TestMethod]
public void HashingAlgorithm_Bad_Name()
{
Expand Down

0 comments on commit 2ef53e2

Please sign in to comment.