public static ComputeHash ( Stream data, string algorithmName = DefaultAlgorithmName ) : |
||
data | Stream |
/// The |
algorithmName | string |
/// The name of the hashing algorithm to use; defaults to |
리턴 |
public void Compute_Not_Implemented_Hash_Array() { MultiHash.HashingAlgorithm.Register("not-implemented", 0x0F, 32); var hello = Encoding.UTF8.GetBytes("Hello, world."); ExceptionAssert.Throws <NotImplementedException>(() => MultiHash.ComputeHash(hello, "not-implemented")); }
public void Compute_Hash_Array() { var hello = Encoding.UTF8.GetBytes("Hello, world."); var mh = MultiHash.ComputeHash(hello); Assert.AreEqual(MultiHash.DefaultAlgorithmName, mh.Algorithm.Name); Assert.IsNotNull(mh.Digest); }
void ComputeHash() { using (var ms = new MemoryStream()) { Write(ms); size = ms.Position; ms.Position = 0; id = MultiHash.ComputeHash(ms, hashAlgorithm); } }
public void Compute_Hash_Stream() { var hello = new MemoryStream(Encoding.UTF8.GetBytes("Hello, world.")); hello.Position = 0; var mh = MultiHash.ComputeHash(hello); Assert.AreEqual(MultiHash.DefaultAlgorithmName, mh.Algorithm.Name); Assert.IsNotNull(mh.Digest); }
public void MultiHash_is_Cid_V1() { var hello = Encoding.UTF8.GetBytes("Hello, world."); var mh = MultiHash.ComputeHash(hello, "sha2-512"); Cid cid = mh; Assert.AreEqual(1, cid.Version); Assert.AreEqual("dag-pb", cid.ContentType); Assert.AreEqual("base32", cid.Encoding); Assert.AreSame(mh, cid.Hash); }
public void Encode_Upgrade_to_V1_Hash() { var hello = Encoding.UTF8.GetBytes("Hello, world."); var mh = MultiHash.ComputeHash(hello, "sha2-512"); var cid = new Cid { Hash = mh }; Assert.AreEqual(1, cid.Version); Assert.AreEqual("zBunRFxZVcKeu8wAbUg92z2JK6UukiL7EnPR1D6TZaQCsPpRe7KzcmioKFyi2oEZd9ffwpbKTib1pucMQrDyRnAdaqwbB", cid.Encode()); }
public void Example() { var hello = Encoding.UTF8.GetBytes("Hello world"); var mh = MultiHash.ComputeHash(hello, "sha2-256"); Console.WriteLine($"| hash code | 0x{mh.Algorithm.Code.ToString("x")} |"); Console.WriteLine($"| digest length | 0x{mh.Digest.Length.ToString("x")} |"); Console.WriteLine($"| digest value | {mh.Digest.ToHexString()} |"); Console.WriteLine($"| binary | {mh.ToArray().ToHexString()} |"); Console.WriteLine($"| base 58 | {mh.ToBase58()} |"); Console.WriteLine($"| base 32 | {mh.ToBase32()} |"); }
public void Encode_Upgrade_to_V1_Hash() { var hello = Encoding.UTF8.GetBytes("Hello, world."); var mh = MultiHash.ComputeHash(hello, "sha2-512"); var cid = new Cid { Hash = mh }; Assert.AreEqual(1, cid.Version); Assert.AreEqual("base32", cid.Encoding); Assert.AreEqual("bafybgqfnbq34ghljwmk7hka7cpem3zybbffnsfzfxinq3qyztsuxcntbxaua23xx42hrgptcchrolkndcucelv3pc4eoarjbwdxagtylboxsm", cid.Encode()); }
public void CheckMultiHash() { foreach (var v in TestVectors) { if (v.Ignore) { continue; } var bytes = Encoding.UTF8.GetBytes(v.Input); var mh = MultiHash.ComputeHash(bytes, v.Algorithm); Assert.AreEqual(v.Output, mh.ToArray().ToHexString(), v.Algorithm); } }
public void Matches_Stream() { var hello = new MemoryStream(Encoding.UTF8.GetBytes("Hello, world.")); var hello1 = new MemoryStream(Encoding.UTF8.GetBytes("Hello, world")); hello.Position = 0; var mh = MultiHash.ComputeHash(hello); hello.Position = 0; Assert.IsTrue(mh.Matches(hello)); hello1.Position = 0; Assert.IsFalse(mh.Matches(hello1)); }
public void Compute_Hash_All_Algorithms() { foreach (var alg in HashingAlgorithm.All) { try { var mh = MultiHash.ComputeHash(new byte[0], alg.Name); Assert.IsNotNull(mh, alg.Name); Assert.AreEqual(alg.Code, mh.Algorithm.Code, alg.Name); Assert.AreEqual(alg.Name, mh.Algorithm.Name, alg.Name); Assert.AreEqual(alg.DigestSize, mh.Algorithm.DigestSize, alg.Name); Assert.AreEqual(alg.DigestSize, mh.Digest.Length, alg.Name); } catch (NotImplementedException) { // If NYI then can't test it. } } }
public void IdentityHash() { var hello = Encoding.UTF8.GetBytes("Hello, world."); var mh = MultiHash.ComputeHash(hello); Assert.IsFalse(mh.IsIdentityHash); CollectionAssert.AreNotEqual(hello, mh.Digest); mh = MultiHash.ComputeHash(hello, "identity"); Assert.IsTrue(mh.IsIdentityHash); CollectionAssert.AreEqual(hello, mh.Digest); var mh1 = new MultiHash(mh.ToBase58()); Assert.AreEqual(mh, mh1); mh = MultiHash.ComputeHash(hello, "id"); Assert.IsTrue(mh.IsIdentityHash); CollectionAssert.AreEqual(hello, mh.Digest); mh1 = new MultiHash(mh.ToBase58()); Assert.AreEqual(mh, mh1); }
public void Matches_Array() { var hello = Encoding.UTF8.GetBytes("Hello, world."); var hello1 = Encoding.UTF8.GetBytes("Hello, world"); var mh = MultiHash.ComputeHash(hello); Assert.IsTrue(mh.Matches(hello)); Assert.IsFalse(mh.Matches(hello1)); var mh1 = MultiHash.ComputeHash(hello, "sha1"); Assert.IsTrue(mh1.Matches(hello)); Assert.IsFalse(mh1.Matches(hello1)); var mh2 = MultiHash.ComputeHash(hello, "sha2-512"); Assert.IsTrue(mh2.Matches(hello)); Assert.IsFalse(mh2.Matches(hello1)); var mh3 = MultiHash.ComputeHash(hello, "sha3-512"); Assert.IsTrue(mh3.Matches(hello)); Assert.IsFalse(mh3.Matches(hello1)); }