Last active
August 16, 2018 14:37
-
-
Save asarnaout/33fb45d06f4dea4d27d012db27138ec3 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
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