Ejemplo n.º 1
0
 protected void ComputeHash()
 {
     Hash = RightNode == null ?
            LeftNode.Hash : //MerkleHash.Create(LeftNode.Hash.Value.Concat(LeftNode.Hash.Value).ToArray()) :
            MerkleHash.Create(LeftNode.Hash.Value.Concat(RightNode.Hash.Value).ToArray());
     Parent?.ComputeHash(); // 부모가 있다면 부모노드로 가서 또 ComputeHash를 수행.
 }
Ejemplo n.º 2
0
        /// <summary>
        /// For demo / debugging purposes, we return the pairs of hashes used to verify the audit proof.
        /// </summary>
        public static List <Tuple <MerkleHash, MerkleHash> > AuditHashPairs(MerkleHash leafHash, List <MerkleProofHash> auditTrail)
        {
            Contract(() => auditTrail.Count > 0, "Audit trail cannot be empty.");
            var        auditPairs = new List <Tuple <MerkleHash, MerkleHash> >();
            MerkleHash testHash   = leafHash;

            // TODO: Inefficient - compute hashes directly.
            foreach (MerkleProofHash auditHash in auditTrail)
            {
                switch (auditHash.Direction)
                {
                case MerkleProofHash.Branch.Left:
                    auditPairs.Add(new Tuple <MerkleHash, MerkleHash>(testHash, auditHash.Hash));
                    testHash = MerkleHash.Create(testHash.Value.Concat(auditHash.Hash.Value).ToArray());
                    break;

                case MerkleProofHash.Branch.Right:
                    auditPairs.Add(new Tuple <MerkleHash, MerkleHash>(auditHash.Hash, testHash));
                    testHash = MerkleHash.Create(auditHash.Hash.Value.Concat(testHash.Value).ToArray());
                    break;
                }
            }

            return(auditPairs);
        }
Ejemplo n.º 3
0
        public static bool VerifyAudit(MerkleHash rootHash, MerkleHash leafHash, List <MerkleProofHash> auditTrail)
        {
            Contract(() => auditTrail.Count > 0, "Audit trail cannot be empty.");
            MerkleHash testHash = leafHash;

            // TODO: Inefficient - compute hashes directly.
            foreach (MerkleProofHash auditHash in auditTrail)
            {
                testHash = auditHash.Direction == MerkleProofHash.Branch.Left ?
                           MerkleHash.Create(testHash.Value.Concat(auditHash.Hash.Value).ToArray()) :
                           MerkleHash.Create(auditHash.Hash.Value.Concat(testHash.Value).ToArray());
            }

            return(rootHash == testHash);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 검증
        /// </summary>
        /// <returns></returns>
        public bool VerifyHash()
        {
            //왼쪽, 오른쪽 노드 둘다 Null이면 true
            if (LeftNode == null && RightNode == null)
            {
                return(true);
            }
            //오른쪽 노드가 널이라면
            if (RightNode == null)
            {
                //부모의 해쉬와 왼쪽노드의 해쉬가 같다면 true
                return(Hash.Equals(LeftNode.Hash));
            }

            MerkleTree.Contract(() => LeftNode != null, "Left branch must be a node if right branch is a node.");
            MerkleHash leftRightHash = MerkleHash.Create(LeftNode.Hash, RightNode.Hash);

            //왼쪽, 오른쪽 노드의 해쉬 Sum 시킨값이 이 노드의 Hash와 같다면 true.
            return(Hash.Equals(leftRightHash));
        }
Ejemplo n.º 5
0
        public MerkleHash ComputeHash(byte[] buffer)
        {
            Hash = MerkleHash.Create(buffer);

            return(Hash);
        }
Ejemplo n.º 6
0
 public static MerkleHash ComputeHash(MerkleHash left, MerkleHash right)
 {
     return(MerkleHash.Create(left.Value.Concat(right.Value).ToArray()));
 }