Search code examples
filecomparisonverificationcrc

Fast file integrity verification


Is there any fast algorithm that allows to compare two files (for verification purpose) without need to read the entire contents?


Solution

  • You could use a MD5 hash on both files and compare them that way. However it does technically read the whole file. You won't be able to have 100% certainty without checking I don't think.

    In C# one would do this in the following way (sorry, you didn't mention a specific language):

    protected string GetMD5HashFromFile(string fileName)
    {
        byte[] retVal = { };
    
        using (FileStream file = new FileStream(fileName, FileMode.Open))
        using (MD5 md5 = new MD5CryptoServiceProvider())
        {
            retVal = md5.ComputeHash(file);
        }
    
        if (retVal.Length > 0)
        {
            StringBuilder sb = new StringBuilder();
    
            for (int i = 0; i < retVal.Length; i++)
            {
                sb.Append(retVal[i].ToString("x2"));
            }
    
            return sb.ToString();
        }
        else
        {
            return string.Empty;
        }
    }
    
    bool CompareFiles(string fileName1, string fileName2)
    {
        return (GetMD5HashFromFile(fileName1) == GetMD5HashFromFile(fileName2));
    }