博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【C#】MD5 小程序编写
阅读量:6955 次
发布时间:2019-06-27

本文共 4128 字,大约阅读时间需要 13 分钟。

通过事件机制,增加了计算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;
            
});
        
}
    
}
 
     
}

参考链接:

      本文转自daniel8294 51CTO博客,原文链接:http://blog.51cto.com/acadia627/1931427,如需转载请自行联系原作者
你可能感兴趣的文章
干货!高德、VPGAME(老干爹)等MongoDB应用实践(暨MongoDB杭州用户会成立)
查看>>
【Android开发】多媒体应用开发-使用MediaPlayer播放音频
查看>>
1期:理解Docker容器的进程管理(原云栖速递)
查看>>
next_permutation函数//字典序
查看>>
翻翻git之---RecycleView的上拉,下拉刷新,样式切换,添加foot和header的强大库 RecyclerViewManager...
查看>>
Hive事物和锁管理
查看>>
curd库
查看>>
CODELF 发布了贴心小功能 - 算法字帖
查看>>
从IP数据包到端口发送究竟经历了什么?
查看>>
前端小白入门区块链系列04
查看>>
ShineButton-一个动效的按钮
查看>>
Java并发编程-锁及并发容器
查看>>
我怕了还不行嘛!
查看>>
element-UI change 事件传递自定义参数
查看>>
logback解析——Appender
查看>>
字符串、列表、字典
查看>>
Function 与 Classes 组件的区别在哪?
查看>>
jenkins 动态 slave
查看>>
[杭州/新加坡] imToken 招聘前端工程师
查看>>
Flutter视图基础简介--Widget、Element、RenderObject
查看>>