When downloading from a remote location, many time we need to know whether the file downloaded properly or not. There are many techniques to know about it but among them, one of the approach is to check the hash value of the file. The Cryptography APIs of .NET can help you to calculate the same.

 

Today in this post, we will learn how to calculate the hash value of file stored in local resource. Continue reading to learn more about the steps. Code has been shared.

 

How to compute the Hash value of a file using C#? (www.kunal-chowdhury.com)

 

Microsoft has provided few Cryptographic APIs to calculate the hash code of any given file, provided the file is available in local storage. The abstract class HashAlgorithm available under System.Security.Cryptography namespace allows you to compute the hash by using the ComputeHash method.

 

There are five different kinds of Crypto Service Providers are available to help you to do this job. They are MD5CryptoServiceProvider, SHA1CryptoServiceProvider, SHA256CryptoServiceProvider, SHA384CryptoServiceProvider and SHA512CryptoServiceProvider. In general, people uses MD5 or SHA1 algorithm, but in case you want more accurate result, you should opt out for a higher cryptography algorithm like SHA256, SHA384 or SHA512. The higher the algorithm mechanism, the higher the length of the hash value.

 

 

Here is the simple implementation of the method, for you to use. You need to pass the absolute path of the local file and the instance of the Crypto Service Provider that you want to use to calculate the hash code. The code is quite self-explanatory.

 

internal static string GetHashCode(string filePath, HashAlgorithm cryptoService)
{
    // create or use the instance of the crypto service provider
    // this can be either MD5, SHA1, SHA256, SHA384 or SHA512
    using (cryptoService)
    {
        using (var fileStream = new FileStream(filePath, 
                                               FileMode.Open, 
                                               FileAccess.Read, 
                                               FileShare.ReadWrite))
        {
            var hash = cryptoService.ComputeHash(fileStream);
            var hashString = Convert.ToBase64String(hash);
            return hashString.TrimEnd('=');
        }
    }
}

 

Now based on your requirement, you create the instance of the crypto service provider that you want to use to calculate the hash code of the file. Here is how you can call the above method depending on your algorithm and business logic:

 

 

WriteLine("MD5 Hash Code   : {0}", GetHashCode(FilePath, new MD5CryptoServiceProvider()));
WriteLine("SHA1 Hash Code  : {0}", GetHashCode(FilePath, new SHA1CryptoServiceProvider()));
WriteLine("SHA256 Hash Code: {0}", GetHashCode(FilePath, new SHA256CryptoServiceProvider()));
WriteLine("SHA384 Hash Code: {0}", GetHashCode(FilePath, new SHA384CryptoServiceProvider()));
WriteLine("SHA512 Hash Code: {0}", GetHashCode(FilePath, new SHA512CryptoServiceProvider()));

 

 

This will result the following output, if you pass the path of the Notepad.exe on Windows 10. It could be different in your case if the version differs. If you pass a different file here, you will get a different hash code but specific to that file.

 

How to generate HashCode of File (www.kunal-chowdhury.com)

 

 

I hope you will like this post and also hope that, it will help you to understand the logic behind creating the hash code of file. Now once you download a file from a remote location and the SHA hash code is available for that file, you can also check and verify the integrity of the downloaded file. If you are a developer and want to host a file in your server, you can calculate the hash of the file and mention it to public, so that, they can check the same too (if they are professional).

 

That’s for today. Connect with me over twitter, facebook and/or google+ to get all the updates that I share over there. Subscribe to my blog’s RSS feed and email newsletter to get the immediate notification of new posts directly delivered to your inbox. Check out my other posts and the Windows 10 Tips & Tricks series to get familiar with the all new operating system. Happy coding!!!

 

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.