Skip to content

Instantly share code, notes, and snippets.

@asarnaout
Last active August 16, 2018 14:37
Show Gist options
  • Save asarnaout/33fb45d06f4dea4d27d012db27138ec3 to your computer and use it in GitHub Desktop.
Save asarnaout/33fb45d06f4dea4d27d012db27138ec3 to your computer and use it in GitHub Desktop.
class TestBlock
{
public string Hash { get; set; }
private int Nonce { get; set; }
private int Index { get; }
private string Data { get; }
private TestBlockChain PreviousBlock { get; }
private string Difficulty { get; }
public TestBlockChain(int index, TestBlockChain previousBlock, string difficulty)
{
Index = index;
PreviousBlock = previousBlock;
Difficulty = difficulty;
Hash = $"{Guid.NewGuid()}";
}
public void Mine()
{
while(!string.Equals(Hash.Substring(0, Difficulty.Length), Difficulty))
{
Hash = Sha256(Hash + Data + PreviousBlock.Hash + Nonce + Index);
++Nonce;
}
}
public static string Sha256(string randomString)
{
var crypt = new SHA256Managed();
string hash = String.Empty;
byte[] crypto = crypt.ComputeHash(Encoding.ASCII.GetBytes(randomString));
foreach (byte theByte in crypto)
{
hash += theByte.ToString("x2");
}
return hash;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment