Skip to content

Commit

Permalink
feat(HashingAlgorithm): get metadata, closes #101
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Aug 17, 2019
1 parent 76ca1a4 commit 6f5ef80
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Registry/HashingAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,28 @@ public static IEnumerable<HashingAlgorithm> All
/// When <paramref name="name"/> is not registered.
/// </exception>
public static HashAlgorithm GetAlgorithm(string name)
{
return GetAlgorithmMetadata(name).Hasher();
}

/// <summary>
/// Gets the metadata 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 metadata associated with the hashing <paramref name="name"/>.
/// </returns>
/// <exception cref="KeyNotFoundException">
/// When <paramref name="name"/> is not registered.
/// </exception>
public static HashingAlgorithm GetAlgorithmMetadata(string name)
{
try
{
return HashingAlgorithm.Names[name].Hasher();
return HashingAlgorithm.Names[name];
}
catch (KeyNotFoundException)
{
Expand Down
28 changes: 28 additions & 0 deletions test/Registry/HashingAlgorithmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,34 @@ public void GetHasher_Unknown()
ExceptionAssert.Throws<KeyNotFoundException>(() => HashingAlgorithm.GetAlgorithm("unknown"));
}

[TestMethod]
public void GetMetadata()
{
var info = HashingAlgorithm.GetAlgorithmMetadata("sha3-256");
Assert.IsNotNull(info);
Assert.AreEqual("sha3-256", info.Name);
Assert.AreEqual(0x16, info.Code);
Assert.AreEqual(256 /8, info.DigestSize);
Assert.IsNotNull(info.Hasher);
}

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

[TestMethod]
public void GetMetadata_Alias()
{
var info = HashingAlgorithm.GetAlgorithmMetadata("id");
Assert.IsNotNull(info);
Assert.AreEqual("identity", info.Name);
Assert.AreEqual(0, info.Code);
Assert.AreEqual(0, info.DigestSize);
Assert.IsNotNull(info.Hasher);
}

[TestMethod]
public void HashingAlgorithm_Bad_Name()
{
Expand Down

0 comments on commit 6f5ef80

Please sign in to comment.