Get md5 string for a file - CSharp Security

CSharp examples for Security:MD5

Description

Get md5 string for a file

Demo Code


using System.Text;
using System.IO;//from   ww w .  j a v a 2  s.  c  o  m
using System;

public class Main{
        /// <summary>  
        /// Get md5 string for a file  
        /// </summary>  
        /// <param name="fileName">Source File</param>  
        /// <returns>md5 string</returns>  
        public static string GetMD5ForFile(string fileName)
        {
            string md5String = string.Empty;
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            FileStream objFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            byte[] bufferForFileStream = new byte[objFileStream.Length];

            objFileStream.Read(bufferForFileStream, 0, bufferForFileStream.Length);
            objFileStream.Close();
            md5String = BitConverter.ToString(md5.ComputeHash(bufferForFileStream)).Replace("-", string.Empty);

            return md5String;
        }
}

Related Tutorials