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++; } }
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)))); }
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); }
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++; } }
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); }
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)))); }
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)))); }
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); }
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); }
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); }
public string CalculateTransactionHash() { string txnHash = ClaimNumber + SettlementAmount + SettlementDate + CarRegistration + Mileage + ClaimType; return(Convert.ToBase64String(Hashing.ComputeHashSha256(Encoding.UTF8.GetBytes(txnHash)))); }