Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Last active August 2, 2019 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/effb806f5677e165273347193c83f980 to your computer and use it in GitHub Desktop.
Save bjoerntx/effb806f5677e165273347193c83f980 to your computer and use it in GitHub Desktop.
// 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