Ejemplo n.º 1
0
        public string CalculateProofOfWork()
        {
            string difficulty = DifficultyString();
            var    stopWatch  = new Stopwatch();

            stopWatch.Start();

            while (true)
            {
                var hashedData = Convert.ToBase64String(Hashing.ComputeHashSha256(Encoding.UTF8.GetBytes(Nonce + MyData)));

                if (hashedData.StartsWith(difficulty, StringComparison.Ordinal))
                {
                    stopWatch.Stop();
                    TimeSpan ts = stopWatch.Elapsed;

                    var elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);

                    Console.WriteLine($"Difficulty level {Difficulty} - Nonce = {Nonce} - Elapsed time: {elapsedTime} - {hashedData}");
                    return(hashedData);
                }

                Nonce++;
            }
        }
Ejemplo n.º 2
0
        public string CalculateBlockHash(string previousBlockHash)
        {
            var blockHeader = BlockNumber + CreatedDate.ToString() + previousBlockHash;
            var combined    = merkleTree.RootNode + blockHeader;

            return(Convert.ToBase64String(Hashing.ComputeHashSha256(Encoding.UTF8.GetBytes(combined))));
        }
Ejemplo n.º 3
0
        public bool IsValidChain(string prevBlockHash, bool verbose)
        {
            bool isValid        = true;
            bool validSignature = false;

            BuildMerkleTree();

            validSignature = KeyStore.VerifyBlock(BlockHash, BlockSignature);

            string newBlockHash = Convert.ToBase64String(Hashing.ComputeHashSha256(Encoding.UTF8.GetBytes(Nonce + CalculateBlockHash(prevBlockHash))));

            validSignature = KeyStore.VerifyBlock(newBlockHash, BlockSignature);

            if (newBlockHash != BlockHash)
            {
                isValid = false;
            }
            else
            {
                isValid |= PreviousBlockHash == prevBlockHash;
            }

            PrintVerificationMessage(verbose, isValid, validSignature);

            if (NextBlock != null)
            {
                return(NextBlock.IsValidChain(newBlockHash, verbose));
            }

            return(isValid);
        }
Ejemplo n.º 4
0
        public string CalculateProofOfWork(string blockHash)
        {
            string    difficulty = DifficultyString();
            Stopwatch stopWatch  = new Stopwatch();

            stopWatch.Start();

            while (true)
            {
                string hashedData = Convert.ToBase64String(Hashing.ComputeHashSha256(Encoding.UTF8.GetBytes(Nonce + blockHash)));

                if (hashedData.StartsWith(difficulty, StringComparison.Ordinal))
                {
                    stopWatch.Stop();
                    TimeSpan ts = stopWatch.Elapsed;

                    // Format and display the TimeSpan value.
                    string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);

                    Console.WriteLine("Difficulty Level " + Difficulty + " - Nonce = " + Nonce + " - Elapsed = " + elapsedTime + " - " + hashedData);
                    return(hashedData);
                }

                Nonce++;
            }
        }
Ejemplo n.º 5
0
        public void send_a_message_with_digital_signature_expect_intercepted_message_as_invalid()
        {
            // User generates a message, hashes the message and signs it with their PRIVATE key //
            var message = Encoding.UTF8.GetBytes("This is a message that will be changed after the digital signature.");

            // User sends the message //
            byte[] hashedMessage = Hashing.ComputeHashSha256(message);

            // Receiver calculates the hash of the encrypted data //
            var digitalSignature = new DigitalSignature();

            digitalSignature.AssignNewKey();

            // this signature is sent along with the message ? //
            var signature = digitalSignature.SignData(hashedMessage);

            // Message intercepted and altered //
            message       = Encoding.UTF8.GetBytes("This is a message that was intercepted after the signature.");
            hashedMessage = Hashing.ComputeHashSha256(message);

            // Receiver verifies the digital signature using the public key //
            var verified = digitalSignature.VerifySignature(hashedMessage, signature);

            // the message should be verified at this point//
            Assert.IsFalse(verified);
        }
Ejemplo n.º 6
0
        public string CalculateBlockHash(string previousBlockHash)
        {
            var    properties  = this.dataItem.ToString();
            string blockHeader = BlockNumber + CreatedOn.ToString() + previousBlockHash;
            string combined    = properties + blockHeader;

            return(Convert.ToBase64String(Hashing.ComputeHashSha256(Encoding.UTF8.GetBytes(combined))));
        }
Ejemplo n.º 7
0
        public string CalculateBlockHash(string previousBlockHash)
        {
            var tnxHash     = ClaimNumber + SettlementAmount + SettlementDate + CarRegistration + Mileage + ClaimType;
            var blockHeader = BlockNumber + CreatedDate.ToString() + previousBlockHash;
            var combined    = tnxHash + blockHeader;

            return(Convert.ToBase64String(Hashing.ComputeHashSha256(Encoding.UTF8.GetBytes(combined))));
        }
Ejemplo n.º 8
0
        public void Hash_Test_Pass()
        {
            var document = Encoding.UTF8.GetBytes("Document to Sign");

            byte[] hashedDocument = Hashing.ComputeHashSha256(document);
            string actual         = Encoding.UTF8.GetString(hashedDocument);

            Assert.AreNotEqual(document, actual);
        }
Ejemplo n.º 9
0
        public string CalculateBlockHash(string previousBlockHash)
        {
            var blockHeader = BlockNumber + CreatedDate.ToString() + previousBlockHash;
            var combined    = merkleTree.RootNode + blockHeader;

            string completeBlockHash;

            if (KeyStore == null)
            {
                completeBlockHash = Convert.ToBase64String(Hashing.ComputeHashSha256(Encoding.UTF8.GetBytes(combined)));
            }
            else
            {
                completeBlockHash = Convert.ToBase64String(HMAC.ComputeHmacSha256(Encoding.UTF8.GetBytes(combined), KeyStore.AuthenticatedHashKey));
            }

            return(completeBlockHash);
        }
Ejemplo n.º 10
0
        public void send_a_message_with_digital_signature_expect_message_validation()
        {
            // User generates a message, hashes the message and signs it with their PRIVATE key //
            var message = Encoding.UTF8.GetBytes("This is a message that will be verified through digital signature.");

            // User sends the message //
            byte[] hashedMessage = Hashing.ComputeHashSha256(message);

            // Receiver calculates the hash of the encrypted data //
            var digitalSignature = new DigitalSignature();

            digitalSignature.AssignNewKey();

            // Receiver verifies the digital signature using the public key //
            var signature = digitalSignature.SignData(hashedMessage);
            var verified  = digitalSignature.VerifySignature(hashedMessage, signature);

            // the message should be verified at this point//
            Assert.IsTrue(verified);
        }
Ejemplo n.º 11
0
        public string CalculateTransactionHash()
        {
            string txnHash = ClaimNumber + SettlementAmount + SettlementDate + CarRegistration + Mileage + ClaimType;

            return(Convert.ToBase64String(Hashing.ComputeHashSha256(Encoding.UTF8.GetBytes(txnHash))));
        }