private bool VerifySha256Hash(SHA256 sha256Hash, Blok blok, string hash) { var hashOfInput = this.GetSha256(sha256Hash, blok); var compair = StringComparer.OrdinalIgnoreCase; if (0 == compair.Compare(hashOfInput, hash)) { return(true); } else { return(false); } }
public Blok NewBlock(Self self, int proof, string previousHash = null) { var blok = new Blok { Index = self.Cahin.Count + 1, Timestamp = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds, Transaction = self.CurrentTransactions, Proof = proof, PreviousHash = self.Hash(self.LastBlok()) }; self.CurrentTransactions = new List <Transaction>( ); self.Cahin.Add(blok); return(blok); }
public string Hash(Blok blok) { using (var sha256Hash = SHA256.Create()) { var hash = GetSha256(sha256Hash, blok); if (VerifySha256Hash(sha256Hash, blok, hash)) { return(hash); } else { return("not success"); } } }
private string GetSha256(SHA256 sha256Hash, Blok blok) { var formatter = new BinaryFormatter(); using (var stream = new MemoryStream()) { formatter.Serialize(stream, blok); byte [] data = stream.ToArray(); var hash = sha256Hash.ComputeHash(data); var sBuilter = new StringBuilder(); foreach (var item in hash) { sBuilter.Append(item.ToString("x2")); } return(sBuilter.ToString()); } }