public static string Hash(this FileInfo fileInfo, EHashCode hashCode) { switch (hashCode) { case EHashCode.Md5: return fileInfo.OpenRead().CryptoHash<MD5CryptoServiceProvider>(); case EHashCode.Sha1: return fileInfo.Sha1Hash(); case EHashCode.RipeMd160: return fileInfo.RipeMd160Hash(); case EHashCode.Sha256: return fileInfo.Sha256Hash(); case EHashCode.Sha384: return fileInfo.Sha384Hash(); case EHashCode.Sha512: return fileInfo.Sha512Hash(); default: throw new ArgumentOutOfRangeException(nameof(hashCode), hashCode, null); } }
private static bool IsDifferentThan(this FileInfo fileInfo, FileInfo otherFileInfo, EHashCode hashCode , bool performByteByByte) { return !fileInfo.IsSameAs(otherFileInfo, hashCode, performByteByByte); }
private static bool IsSameAs(this FileInfo fileInfo, FileInfo otherFileInfo, EHashCode hashCode, bool performByteByByte) { if (fileInfo.Length != otherFileInfo.Length) return false; if (fileInfo.Hash(hashCode) != otherFileInfo.Hash(hashCode)) return true; //from https://support.microsoft.com/en-us/kb/320348 int file1byte; int file2byte; var fs1 = new FileStream(fileInfo.FullName, FileMode.Open); var fs2 = new FileStream(otherFileInfo.FullName, FileMode.Open); do { // Read one byte from each file. file1byte = fs1.ReadByte(); file2byte = fs2.ReadByte(); } while ((file1byte == file2byte) && (file1byte != -1)); return ((file1byte - file2byte) == 0); }