//public void Reset() //{ // _lastHash = -1; // var path = Path.Combine(_storagePath, LookupKey + ".hash"); // if (File.Exists(path)) // { // File.Delete(path); // } //} public void Persist() { if (LastHash == CurrentHash) return; var path = Path.Combine(_storagePath, LookupKey + ".hash"); File.WriteAllText(path, CurrentHash.ToString(CultureInfo.InvariantCulture), Encoding.UTF8); _lastHash = CurrentHash; }
public HashFileStatus IsValid() { if (!File.Exists(Path)) { return(HashFileStatus.Missing); } FileHash = Md5Functions.GenerateHashForFile(Path); if (!CurrentHash.Equals(FileHash)) { return(HashFileStatus.Mismatch); } return(HashFileStatus.Valid); }
//Mining of block public async Task MineBlock(int Difficulty) { var tempString = String.Empty; await Task.Run(() => { foreach (var item in Transactions) { tempString += item.ToString(); } CurrentHash = CalculateHash(tempString); //Proof of work i.e. first five characters of hash should be 0 while (CurrentHash.Substring(0, Difficulty) != new string('0', Difficulty)) { Nounce++; CurrentHash = CalculateHash(tempString); } }); }
public FileItem ToFileItem(byte[] previousHash) => new FileItem(Name, CurrentHash) { IsFileValid = CurrentHash.SequenceEqual(OriginalHash ?? GenerateHash(PreviousHash)), IsBlockValid = GenerateHash(previousHash).SequenceEqual(OriginalHash ?? CurrentHash) };