吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3629|回复: 2
收起左侧

[其他转载] 【笔记】C Sharp哈希值计算MD5

[复制链接]
sivycoffee 发表于 2015-11-23 14:24
本帖最后由 奋斗丶小Z 于 2015-11-23 18:21 编辑

[C#] 纯文本查看 复制代码
public delegate void AsyncCheckHeadler(AsyncCheckEventArgs e);

        public class MD5Checker
        {
                //支持所有哈希算法
                private static HashAlgorithm hashAlgorithm;

                //文件缓冲区
                private static byte[] buffer;

                //文件读取流
                private static Stream inputStream;

                public event AsyncCheckHeadler AsyncCheckProgress;

                /// <summary>
                /// 返回指定文件的MD5值
                /// </summary>
                /// <param name="path"></param>
                /// <returns></returns>
                public static string Check(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("-", "");
                        return resule;
                }

                public void AsyncCheck(string path)
                {
                        if (!File.Exists(path))
                                throw new ArgumentException(string.Format("<{0}>, 不存在", path));

                        int bufferSize = 1048576;//缓冲区大小,1MB

                        buffer = new byte[bufferSize];

                        //打开文件流
                        inputStream = File.Open(path, FileMode.Open);
                        hashAlgorithm = new MD5CryptoServiceProvider();

                        //异步读取数据到缓冲区
                        inputStream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(AsyncComputeHashCallback), null);
                }

                private void AsyncComputeHashCallback(IAsyncResult result)
                {
                        int bytesRead = inputStream.EndRead(result);

                        //检查是否到达流末尾
                        if (inputStream.Position < inputStream.Length)
                        {
                                //输出进度
                                string pro = string.Format("{0:P0}", (double)inputStream.Position / inputStream.Length);

                                if (null != AsyncCheckProgress)
                                        AsyncCheckProgress(new AsyncCheckEventArgs(AsyncCheckState.Checking, pro));

                                var output = new byte[buffer.Length];
                                //分块计算哈希值
                                hashAlgorithm.TransformBlock(buffer, 0, buffer.Length, output, 0);

                                //异步读取下一分块
                                inputStream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(AsyncComputeHashCallback), null);
                                return;
                        }
                        else
                        {
                                //计算最后分块哈希值
                                hashAlgorithm.TransformFinalBlock(buffer, 0, bytesRead);
                        }

                        string md5 = BitConverter.ToString(hashAlgorithm.Hash).Replace("-", "");

                        AsyncCheckProgress(new AsyncCheckEventArgs(AsyncCheckState.Checking, md5));

                        //关闭流
                        inputStream.Close();
                }
        }

        public enum AsyncCheckState
        {
                Completed,
                Checking
        }

        public class AsyncCheckEventArgs : EventArgs
        {
                public string Value { get; private set; }

                public AsyncCheckState State { get; private set; }

                public AsyncCheckEventArgs(AsyncCheckState state, string value)
                {
                        this.Value = value; this.State = state; 
                }
        }

免费评分

参与人数 1热心值 +1 收起 理由
Cizel + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩.

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

独孤鹏飞 发表于 2015-11-23 15:08
满满的看不懂  不过还是来支持下!
Cizel 发表于 2015-11-23 18:22
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-9-23 03:20

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表