Last active
January 27, 2024 20:41
-
-
Save FrqSalah/fbb44d99e3d3c69b08e2e477bc7d8d44 to your computer and use it in GitHub Desktop.
Block class, which will represent each block in the blockchain
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
public class Block | |
{ | |
public int Index { get; set; } | |
public DateTime Timestamp { get; set; } | |
public string Data { get; set; } | |
public string PreviousHash { get; set; } | |
public string Hash { get; set; } | |
public Block(int index, DateTime timestamp, string data, string previousHash) | |
{ | |
Index = index; | |
Timestamp = timestamp; | |
Data = data; | |
PreviousHash = previousHash; | |
Hash = CalculateHash(); | |
} | |
private string CalculateHash() | |
{ | |
using (var sha256 = SHA256.Create()) | |
{ | |
var inputBytes = Encoding.UTF8.GetBytes($"{Timestamp}-{Data}-{PreviousHash}"); | |
var outputBytes = sha256.ComputeHash(inputBytes); | |
return Convert.ToBase64String(outputBytes); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment