Last active
August 2, 2019 13:09
-
-
Save bjoerntx/effb806f5677e165273347193c83f980 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// generates a new hash for the block including the time stamp, | |
// the previous block has, the data and the nonce. | |
internal string GenerateBlockHash() | |
{ | |
SHA256 sha256 = SHA256.Create(); | |
byte[] bInput = | |
Encoding.ASCII.GetBytes($"{TimeStamp}-{PreviousBlockHash ?? ""}-{Data}-{Nonce}"); | |
byte[] bOutput = sha256.ComputeHash(bInput); | |
return Convert.ToBase64String(bOutput); | |
} | |
// this method is generating hashes until a new hash with a specific | |
// number of leading zeros is created | |
public void Mine(int difficulty) | |
{ | |
string sLeadingZeros = new string('0', difficulty); | |
while (this.BlockHash == null | |
|| this.BlockHash.Substring(0, difficulty) != sLeadingZeros) | |
{ | |
this.Nonce++; | |
this.BlockHash = this.GenerateBlockHash(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment