[C#] 计算大文件的MD5的两种方式(直接调用方法计算,流计算-适用于大文件)

通过.NET中的默认类实现,但是采用不同类,针对不同的情况:

具体如下:

类:

/// <summary>
    /// 文件MD5操作类
    /// </summary>
    public class MD5Checker
    {
        /// <summary>
        /// 通过MD5CryptoServiceProvider类中的ComputeHash方法直接传入一个FileStream类实现计算MD5
        /// 操作简单,代码少,调用即可
        /// </summary>
        /// <param name="path">文件地址</param>
        /// <returns>MD5Hash</returns>
        public static string getMD5ByMD5CryptoService(string path)
        {
            if (!File.Exists(path))
                throw new ArgumentException(string.Format("<{0}>, 不存在", path));
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
            MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider();
            byte[] buffer = md5Provider.ComputeHash(fs);
            string resule = BitConverter.ToString(buffer);
            resule = resule.Replace("-", "");
            md5Provider.Clear();
            fs.Close();
            return resule;
        }

        /// <summary>
        /// 通过HashAlgorithm的TransformBlock方法对流进行叠加运算获得MD5
        /// 实现稍微复杂,但可使用与传输文件或接收文件时同步计算MD5值
        /// 可自定义缓冲区大小,计算速度较快
        /// </summary>
        /// <param name="path">文件地址</param>
        /// <returns>MD5Hash</returns>
        public static string getMD5ByHashAlgorithm(string path)
        {
            if (!File.Exists(path))
                throw new ArgumentException(string.Format("<{0}>, 不存在", path));
            int bufferSize = 1024 * 16;//自定义缓冲区大小16K
            byte[] buffer = new byte[bufferSize];
            Stream inputStream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
            HashAlgorithm hashAlgorithm = new MD5CryptoServiceProvider();
            int readLength = 0;//每次读取长度
            var output = new byte[bufferSize];
            while ((readLength = inputStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                //计算MD5
                hashAlgorithm.TransformBlock(buffer, 0, readLength, output, 0);
            }
            //完成最后计算,必须调用(由于上一部循环已经完成所有运算,所以调用此方法时后面的两个参数都为0)
            hashAlgorithm.TransformFinalBlock(buffer, 0, 0);
            string md5 = BitConverter.ToString(hashAlgorithm.Hash);            
            hashAlgorithm.Clear();
            inputStream.Close();
            md5 = md5.Replace("-", "");
            return md5;
        }
    }

测试:

static void Main(string[] args)
        {
            Console.WriteLine("第一种方式:");
            Console.WriteLine(DateTime.Now.Ticks);
            Console.WriteLine(DateTime.Now.ToString());
            Console.WriteLine(MD5Checker.getMD5ByMD5CryptoService(@"D:\ShareFiles\Softwares\Office2013.zip"));
            Console.WriteLine(DateTime.Now.Ticks);
            Console.WriteLine(DateTime.Now.ToString());
            Console.WriteLine("第二种方式:");
            Console.WriteLine(DateTime.Now.Ticks);
            Console.WriteLine(DateTime.Now.ToString());
            Console.WriteLine(MD5Checker.getMD5ByHashAlgorithm(@"D:\ShareFiles\Softwares\Office2013.zip"));
            Console.WriteLine(DateTime.Now.Ticks);
            Console.WriteLine(DateTime.Now.ToString());
            Console.ReadKey();
        }

结果:

[C#] 计算大文件的MD5的两种方式(直接调用方法计算,流计算-适用于大文件)_第1张图片


转载请注明出处!!

你可能感兴趣的:(.net,MD5,C#,大文件,流块)