예제 #1
0
        public void Diff_is_not_same()
        {
            byte[] bytesA = new byte[32];
            new Random(42).NextBytes(bytesA);
            byte[] bytesB = new byte[32];
            Hash32 a      = new Hash32(bytesA);
            Hash32 b      = new Hash32(bytesB);

            Assert.AreNotEqual(a, b);
            Assert.False(a.Equals(b));
            Assert.False(b.Equals(a));
            Assert.False(a == b);
            Assert.False(!(a != b));
            Assert.False(a.Equals((object)b));
            Assert.False(a.Equals(b));
            Assert.AreNotEqual(a.GetHashCode(), b.GetHashCode());
            Assert.AreNotEqual(a.ToString(), b.ToString());
        }
예제 #2
0
        public void Same_is_same()
        {
            byte[] bytesA = new byte[32];
            new Random(42).NextBytes(bytesA);
            byte[] bytesB = new byte[32];
            bytesA.AsSpan().CopyTo(bytesB);
            Hash32 a = new Hash32(bytesA);
            Hash32 b = new Hash32(bytesB);

            Assert.AreEqual(a, b);
            Assert.True(a.Equals(b));
            Assert.True(b.Equals(a));
            Assert.True(a == b);
            Assert.True(!(a != b));
            Assert.True(a.Equals((object)b));
            Assert.True(a.Equals(b));
            Assert.AreEqual(a.GetHashCode(), b.GetHashCode());
            Assert.AreEqual(a.ToString(), b.ToString());
            Assert.AreEqual(0, a.CompareTo(b));
        }
예제 #3
0
        /// <summary>
        /// Check if 'leaf' at 'index' verifies against the Merkle 'root' and 'branch'
        /// </summary>
        public bool IsValidMerkleBranch(Hash32 leaf, IReadOnlyList<Hash32> branch, int depth, ulong index, Hash32 root)
        {
            Hash32 value = leaf;
            for (int testDepth = 0; testDepth < depth; testDepth++)
            {
                Hash32 branchValue = branch[testDepth];
                ulong indexAtDepth = index / ((ulong) 1 << testDepth);
                if (indexAtDepth % 2 == 0)
                {
                    // Branch on right
                    value = _cryptographyService.Hash(value, branchValue);
                }
                else
                {
                    // Branch on left
                    value = _cryptographyService.Hash(branchValue, value);
                }
            }

            return value.Equals(root);
        }