How to get a usable hash code of an empty file for comparison purposes

This puzzled me for a while, but I have a solution now. Just create a hash from the file name because file contents are empty. In C#, you can use the below:


public static byte[] CreateMD5Hash(List<string> dataList)
        {
            using (MemoryStream fingerStream = new MemoryStream())
            {
                using (StreamWriter writer = new StreamWriter(fingerStream))
                {
                    foreach (string data in dataList)
                    {
                        writer.Write(item);
                    }
 
                    writer.Flush();
                    fingerStream.Position = 0;
 
                    MD5 md5 = new MD5CryptoServiceProvider();
                    return md5.ComputeHash(fingerStream);
                }
            }
        }


When you compare two empty files from two different servers, with the same name, they will match. If one file is not empty, because hash will be generated from the file contents the hash will be different.

So you get pretty much what you want.

Comments

Popular posts from this blog

Tutorial: Using Google Cloud Storage from C# and .NET

Late 2008 Macbook only giving 1.5 gb/s speed with 6 gb/s Intel SSD?

The ridiculous interview experience