Skip to main content

How to compute hash of a file in c#

This is a very simple code for calculating the hash of file in c# Namespace Required: using System.IO; using System.Security.Cryptography; Complete C# code
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Blog_CS
{
    class Program
    {
        private const string FileName = @"D:\output.txt";
        static void Main(string[] args)
        {
            string fileHash = GetHashOfFile(FileName);
            Console.WriteLine(fileHash);
        }

        private static string GetHashOfFile(string fileName)
        {
            using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                //Create HashAlgorithm (MD5,SHA1...)
                HashAlgorithm hash = HashAlgorithm.Create("MD5");
                byte[] hashByte = hash.ComputeHash(fileStream);
                return String.Join(",", hashByte);
            }
        }
    }
}

Comments

Popular posts from this blog