【C#】MD5 小程序编写

简介:

通过事件机制,增加了计算MD5过程提示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Linq;
using  System.Text;
using  System.Threading.Tasks;
using  System.Windows.Forms;
using  System.IO;
 
namespace  WindowsFormsApplication13
{
     public  partial  class  Form1 : Form
     {
         public  delegate  void  delegateMD( object  sender, EventArgs e);
         public  event  delegateMD openfileEvent;
         public  event  delegateMD finishEvent;
 
         public  Form1()
         {
             InitializeComponent();
         }
 
         private  void  Form1_Load( object  sender, EventArgs e)
         {
 
         }
 
         private  async  void  button1_Click( object  sender, EventArgs e)
         {
             openfileEvent += ( object  obj, EventArgs e1) => {
                 label1.Text =  "正在计算MD5值,请稍等。。。" ; };
 
             finishEvent += ( object  obj, EventArgs e1) =>
               {
                   label1.Text =  "" ;
               };
 
             using  (OpenFileDialog dialog =  new  OpenFileDialog())
             {
                 if  (dialog.ShowDialog() == DialogResult.OK)
                 {
                     openfileEvent( this , e);
                     String fileName = dialog.FileName;
                     this .textBox1.Text =  "" ;
                     //this.txtSH1.Text = "";
                     //
                     this .textBox1.Text = await getMD5HashAsync(fileName);
                     //this.txtSH1.Text = GetMD5Hash(fileName);
                     finishEvent( this , e);
                 }
             }
         }
         //计算文件的MD5码
         private  Task< string > getMD5HashAsync( string  pathName)
         {
             return  Task.Run< string >( () =>
             {
                 string  strResult =  "" ;
                 string  strHashData =  "" ;
 
                 byte [] arrbytHashValue;
                 System.IO.FileStream oFileStream =  null ;
 
                 System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher =
                            new  System.Security.Cryptography.MD5CryptoServiceProvider();
 
                 try
                 {
                     oFileStream =  new  FileStream(pathName, FileMode.Open,
                           FileAccess.Read,FileShare.ReadWrite);
                     arrbytHashValue = oMD5Hasher.ComputeHash(oFileStream); //计算指定Stream 对象的哈希值
                     oFileStream.Close();
                     //由以连字符分隔的十六进制对构成的String,其中每一对表示value 中对应的元素;例如“F-2C-4A”
                     strHashData = BitConverter.ToString(arrbytHashValue);
                     //替换-
                     strHashData = strHashData.Replace( "-" "" );
                     strResult = strHashData;
                 }
                 catch  (System.Exception ex)
                 {
                     MessageBox.Show(ex.Message);
                 }
 
                 return  strResult;
             });
         }
     }
 
     
}

异步执行代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Linq;
using  System.Text;
using  System.Threading.Tasks;
using  System.Windows.Forms;
using  System.IO;
 
namespace  WindowsFormsApplication13
{
     public  partial  class  Form1 : Form
     {
         public  Form1()
         {
             InitializeComponent();
         }
 
         private  void  Form1_Load( object  sender, EventArgs e)
         {
 
         }
 
         private  async  void  button1_Click( object  sender, EventArgs e)
         {
             using  (OpenFileDialog dialog =  new  OpenFileDialog())
             {
                 if  (dialog.ShowDialog() == DialogResult.OK)
                 {
                     String fileName = dialog.FileName;
                     this .textBox1.Text =  "" ;
                     //this.txtSH1.Text = "";
                     //
                     this .textBox1.Text = await getMD5HashAsync(fileName);
                     //this.txtSH1.Text = GetMD5Hash(fileName);
                 }
             }
         }
         //计算文件的MD5码
         private  Task< string > getMD5HashAsync( string  pathName)
         {
             return  Task.Run< string >( () =>
             {
                 string  strResult =  "" ;
                 string  strHashData =  "" ;
 
                 byte [] arrbytHashValue;
                 System.IO.FileStream oFileStream =  null ;
 
                 System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher =
                            new  System.Security.Cryptography.MD5CryptoServiceProvider();
 
                 try
                 {
                     oFileStream =  new  FileStream(pathName, FileMode.Open,
                           FileAccess.Read,FileShare.ReadWrite);
                     arrbytHashValue = oMD5Hasher.ComputeHash(oFileStream); //计算指定Stream 对象的哈希值
                     oFileStream.Close();
                     //由以连字符分隔的十六进制对构成的String,其中每一对表示value 中对应的元素;例如“F-2C-4A”
                     strHashData = BitConverter.ToString(arrbytHashValue);
                     //替换-
                     strHashData = strHashData.Replace( "-" "" );
                     strResult = strHashData;
                 }
                 catch  (System.Exception ex)
                 {
                     MessageBox.Show(ex.Message);
                 }
 
                 return  strResult;
             });
         }
     }
 
     
}



参考链接:

http://blog.csdn.net/chuangxin/article/details/5333376






      本文转自daniel8294 51CTO博客,原文链接:http://blog.51cto.com/acadia627/1931427,如需转载请自行联系原作者



相关文章
|
25天前
|
开发框架 小程序 .NET
C#动态生成带参数的小程序二维码
C#动态生成带参数的小程序二维码
|
3月前
|
监控 小程序 前端开发
C#医院预约挂号小程序源码(前端+后台)
线上预约挂号系统构建了医院和患者的连接,通过改善患者院内的就医服务流程,以微信公众号、支付宝小程序为患者服务入口,为居民提供导诊、预约、支付、报告查询等线上线下一体化的就医服务,缩短患者就诊环节,提高医疗机构服务效率。
80 0
|
10月前
|
SQL 小程序 关系型数据库
使用C#写winform小程序入门
使用C#写winform小程序入门
128 0
|
小程序 前端开发 安全
【C#】 MVC4 开发小程序-实现人脸识别-本地和手机预览使用IP测试
小程序Camera组件拍照上传图片到指定的服务器(本地或者外网的IP服务器),然后C# MVC后台调用百度人脸识别接口,实现人脸识别功能呢
225 0
|
小程序 定位技术 C#
C#编程学习(01):北斗时转日历时的小程序
C#编程学习(01):北斗时转日历时的小程序
C#编程学习(01):北斗时转日历时的小程序
|
小程序 C# Python
【优化】C#小程序集成实现python定时段批量下载电子邮箱附件的bug排除
【优化】C#小程序集成实现python定时段批量下载电子邮箱附件的bug排除
96 0
|
小程序 C# 数据安全/隐私保护
C#小程序执行后及时清空控件中的数据
C#小程序执行后及时清空控件中的数据
97 0
C#小程序执行后及时清空控件中的数据
|
编解码 小程序 C#
python实现电子邮件附件指定时间段,批量下载以及C#小程序集成实现(二)
根据网上相关资料,使用python实现邮箱附件批量下载,然后打包生成exe后,虽是脱离了python环境便于她使用,可对于她们这些没有接触编程的人员来说,cmd命令行调用exe的操作不便于理解和操作。为此,界面化程序开发选择了C#,从而实现了C#调用python开发的exe来完成邮箱附件批量下载功能。
260 0
python实现电子邮件附件指定时间段,批量下载以及C#小程序集成实现(二)
|
编解码 小程序 C#
python实现电子邮件附件指定时间段,批量下载以及C#小程序集成实现(一)
根据网上相关资料,使用python实现邮箱附件批量下载,然后打包生成exe后,虽是脱离了python环境便于她使用,可对于她们这些没有接触编程的人员来说,cmd命令行调用exe的操作不便于理解和操作。为此,界面化程序开发选择了C#,从而实现了C#调用python开发的exe来完成邮箱附件批量下载功能。
278 0
python实现电子邮件附件指定时间段,批量下载以及C#小程序集成实现(一)
|
C# 小程序
C# 定时关机小程序
C# 定时关机小程序 1.打开VS2019,创建界面和按钮 2. 代码如下: private void button1_Click(object sender, EventArgs e) { downpc(txttime.
1425 0