示例#1
0
        public string CalculateHash()
        {
            string inputStr = fromAccount + toAccount + timestamp + amount;
            string hash     = ChainHelper.CalculateHash(inputStr);

            return(hash);
        }
示例#2
0
        public static string CalculateHash3(string data)
        {
            SHA256 sha256Hash = SHA256.Create();
            string hash       = ChainHelper.GetHash(sha256Hash, data);

            return(hash);
            //return crypto.createHash('sha256').update(this.fromAddress + this.toAddress + this.amount + this.timestamp).digest('hex');
        }
        /**
         * Loops over all the blocks in the chain and verify if they are properly
         * linked together and nobody has tampered with the hashes. By checking
         * the blocks it also verifies the (signed) transactions inside of them.
         *
         * @returns {boolean}
         */
        public bool IsChainValid(out string error)
        {
            // Check if the Genesis block hasn't been tampered with by comparing
            // the output of createGenesisBlock with the first block on our chain
            //var realGenesis = JsonConvert.SerializeObject(this.createGenesisBlock(), Formatting.Indented);

            //if (realGenesis != JsonConvert.SerializeObject(this.chain[0], Formatting.Indented))
            //{
            //    return false;
            //}

            // Check the remaining blocks on the chain to see if there hashes and
            // signatures are correct
            for (int i = 1; i < this.blocks.Count; i++)
            {
                var currentBlock = this.blocks[i];

                if (!currentBlock.HasValidTransactions())
                {
                    error = "Invalid transaction signature(s)";
                    return(false);
                }

                string hashStartWith = string.Empty;
                for (int k = 0; k < ChainHelper.NUMBER_ZEROES; k++)
                {
                    hashStartWith += "0";
                }

                if (!currentBlock.hash.StartsWith(hashStartWith))
                {
                    error = "Tampered block hash values";
                    return(false);
                }

                var rawData = currentBlock.index + currentBlock.previousHash + currentBlock.timestamp.ToString() + currentBlock.nonce + ChainHelper.FindMerkleRootHash(currentBlock.transactions);
                if (currentBlock.hash != ChainHelper.CalculateHash(ChainHelper.CalculateHash(rawData)))
                {
                    error = "Tampered block hash values";
                    return(false);
                }
            }
            error = null;
            return(true);
        }
        private void MineBlock(Block block)
        {
            if (block.transactions == null)
            {
                return;
            }

            var  merkleRootHash = ChainHelper.FindMerkleRootHash(block.transactions);
            long nonce          = -1;
            var  hash           = string.Empty;

            do
            {
                nonce++;
                var rowData = block.index + block.previousHash + block.timestamp.ToString() + nonce + merkleRootHash;
                hash = ChainHelper.CalculateHash(ChainHelper.CalculateHash(rowData));
            }while (!hash.StartsWith(hashStartWith));
            block.hash  = hash;
            block.nonce = nonce;
        }
        /**
         * Returns the SHA256 of this block (by processing all the data stored
         * inside this block)
         *
         * @returns {string}
         */
        public string CalculateHash()
        {
            string inputStr = this.previousHash + this.timestamp.ToString() + JsonConvert.SerializeObject(this.transactions, Formatting.Indented) + this.nonce;

            return(ChainHelper.CalculateHash(inputStr));
        }