Computes the MD5 checksum for a single file. : MD5 « Security « C# / C Sharp






Computes the MD5 checksum for a single file.

 


using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Dropboxifier
{
    static public class FileUtils
    {
        
        /// <summary>
        /// Computes the MD5 checksum for a single file.
        /// </summary>
        public static string ComputeMD5ForFile(string file)
        {
            FileInfo finfo = new FileInfo(file);
            BinaryReader br = new BinaryReader(finfo.OpenRead());
            byte[] fileBytes = br.ReadBytes((int)finfo.Length);
            br.Close();

            MD5 hasher = MD5.Create();
            byte[] hash = hasher.ComputeHash(fileBytes);

            StringBuilder hashString = new StringBuilder();
            foreach (byte b in hash)
            {
                hashString.Append(b.ToString("x2"));
            }
            return hashString.ToString();
        }
    }
}

   
  








Related examples in the same category

1.MD5 encode
2.MD5 Encrypt
3.Create MD5 Hash
4.Create ASCII MD5 Hash
5.Encrypt MD5
6.MD5 Hash
7.MD5 Hash
8.Get MD5 Hash
9.Makes a MD5 hash from a string
10.Get MD5 Hash (2)
11.Get MD5 for a string
12.Get and verify MD5 Hash
13.Md5 Hash (2)