Skip to content

Instantly share code, notes, and snippets.

@FrqSalah
Last active January 27, 2024 20:41
Show Gist options
  • Save FrqSalah/fbb44d99e3d3c69b08e2e477bc7d8d44 to your computer and use it in GitHub Desktop.
Save FrqSalah/fbb44d99e3d3c69b08e2e477bc7d8d44 to your computer and use it in GitHub Desktop.
Block class, which will represent each block in the blockchain
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