博客
关于我
C# 获取文件MD5与SHA1
阅读量:392 次
发布时间:2019-03-05

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

之前刚开始学习编程的时候,总想着自己写一些小软件小工具。

而这个就是经典的文件MD5校验,顺便加上了一个SHA1。

在网络上下载一些东西时,会有作者提供MD5值。

它的作用就在于我们可以在下载该软件后,对下载回来的文件做一次MD5校验,以确保我们获得的文件与该站点提供的文件为同一文件。

于是就需要一个MD5校验工具,那么就去百度上找,但是国内的下载站就不用说了...捆绑的东西防不胜防。

所以,作为一个程序员,就自己来写一个吧。

 

分析这个小工具的主要需求就是

1.根据路径找到文件

2.获取MD5

3.获取SHA1

 

非常简单的一个小工具,

第一个方法直接使用 FileInfo 类构造函数传入路径就可以了,因为是做控制台可以直接拖拽文件,这一点还是很方便的。

需要注意的是路径中有空格会报错。

该方法中 s 代表传入的文件路径

1 static void GetFile(string s) 2         { 3             try 4             { 5                 FileInfo fi = new FileInfo(s); 6                 Console.WriteLine("文件路径:{0}", s); 7                 Console.WriteLine("文件名称:{0}", fi.Name.ToString()); 8                 Console.WriteLine("文件类型:{0}", fi.Extension.ToString()); 9                 Console.WriteLine("文件大小:{0} K", fi.Length / 1024);10                 Console.WriteLine("文件创建时间:{0}", fi.CreationTime.ToString());11                 Console.WriteLine("上次访问时间:{0}", fi.LastAccessTime.ToString());12                 Console.WriteLine("上次写入时间:{0}", fi.LastWriteTime.ToString());13             }14             catch (Exception ex) 15             {16                 Console.WriteLine(ex.Message);17             }18         }

第二个方法获得MD5值,该方法中 s 代表传入的文件路径

MD5将整个文件当作一个大文本信息,通过其不可逆的字符串变换算法,产生了这个唯一的MD5信息摘要。

1 static void GetMD5(string s) 2         { 3             try 4             { 5                 FileStream file = new FileStream(s,FileMode.Open); 6                 MD5 md5 = new MD5CryptoServiceProvider(); 7                 byte[] retval = md5.ComputeHash(file); 8                 file.Close(); 9 10                 StringBuilder sc = new StringBuilder();11                 for (int i = 0 ; i

第三个方法,获得SHA1值,该方法中 s 代表传入的文件路径

SHA1名为安全哈希算法,对于长度小于2^64位的消息,SHA1会产生一个160位的消息摘要。

SHA1有如下特性:不可以从消息摘要中复原信息;两个不同的消息不会产生同样的消息摘要,(但会有1x10 ^ 48分之一的机率出现相同的消息摘要,一般使用时忽略)。

1 static void GetSHA1(string s) 2         { 3             try 4             { 5                 FileStream file = new FileStream(s, FileMode.Open); 6                 SHA1 sha1 = new SHA1CryptoServiceProvider(); 7                 byte[] retval = sha1.ComputeHash(file); 8                 file.Close(); 9 10                 StringBuilder sc = new StringBuilder();11                 for (int i = 0; i < retval.Length; i++)12                 {13                     sc.Append(retval[i].ToString("x2"));14                 }15                 Console.WriteLine("文件SHA1:{0}", sc);16             }17             catch (Exception ex)18             {19                 Console.WriteLine(ex.Message);20             }21         }

 

放出我的成品,用的是.NET Framework 4.0,各位电脑上应该都有。

(密码:XVi7MD)

https://share.weiyun.com/98d8c10869e693961fb0df10c9202624

 

转载地址:http://bgazz.baihongyu.com/

你可能感兴趣的文章
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>