private void CreateNewHash(FileHash newHash) { CloseCurrentHash(); fileHash = newHash; fileMd5 = MD5.Create(); }
private void CloseCurrentHash() { if (fileHash == null) { return; } fileMd5.TransformFinalBlock(new byte[0], 0, 0); fileHash.Hash = fileMd5.Hash; fileMd5.Dispose(); fileMd5 = null; fileHash = null; }
public override void Write(byte[] buffer, int offset, int count) { if (count % Utilities.SectorSize != 0) { throw new NotSupportedException("Only write full sectors, and one sector at a time"); } streamMd5.TransformBlock(buffer, offset, count, null, 0); long parts = count / Utilities.SectorSize; for (long i = 0; i < parts; i++) { FileHash currentHash = FindCurrentHash(); if ((fileHash != null && !fileHash.Equals(currentHash))) // Close the filehash { CloseCurrentHash(); } if (currentHash != null) { if (fileHash == null) // Different hash!!! { CreateNewHash(currentHash); } if (fileHash != null) { // Now, find out how many bytes we have to write, only copy the required bytes. // Probably, the rest of the buffer can be thrown away, unless count > Utilities.SectorSize long posInFile = _position - (fileHash.StartSector * Utilities.SectorSize); int amount = (int)Math.Min(Utilities.SectorSize, fileHash.Length - posInFile); fileMd5.TransformBlock(buffer, offset, amount, null, 0); } } offset += (int)Utilities.SectorSize; _position += Utilities.SectorSize; } }
public bool Equals(FileHash fh) { return(fh != null && fh.StartSector == StartSector); }