ComputeHash() 공개 정적인 메소드

Generate the multihash for the specified data.
public static ComputeHash ( Stream data, string algorithmName = DefaultAlgorithmName ) : MultiHash
data Stream /// The containing the data to hash. ///
algorithmName string /// The name of the hashing algorithm to use; defaults to . ///
리턴 MultiHash
예제 #1
0
        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"));
        }
예제 #2
0
        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);
        }
예제 #3
0
 void ComputeHash()
 {
     using (var ms = new MemoryStream())
     {
         Write(ms);
         size        = ms.Position;
         ms.Position = 0;
         id          = MultiHash.ComputeHash(ms, hashAlgorithm);
     }
 }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
        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());
        }
예제 #7
0
        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()} |");
        }
예제 #8
0
        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());
        }
예제 #9
0
 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);
     }
 }
예제 #10
0
        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));
        }
예제 #11
0
 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.
         }
     }
 }
예제 #12
0
        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);
        }
예제 #13
0
        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));
        }