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

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

MD5和SHA1校验工具

作为一名程序员,我曾经也想过自己动手编写一些小工具。MD5和SHA1校验工具就是我其中一个项目。这个工具可以帮助用户快速验证下载文件的完整性,确保文件与站点提供的文件一致。

工具需求

  • 文件路径获取
    工具需要能够识别用户提供的文件路径,支持 drag-and-drop 功能。
  • MD5校验
    使用MD5算法对文件内容生成唯一的哈希值,用于验证文件完整性。
  • SHA1校验
    同样使用SHA1算法生成文件的哈希值,提供双重验证保障。
  • 文件信息获取

    工具的第一部分是获取文件信息。通过FileInfo类,可以轻松获取文件的路径、名称、扩展名、大小和时间戳等详细信息。这里需要注意的是,文件路径中如果包含空格,会导致异常处理,所以需要确保路径的规范性。

    static void GetFile(string s){    try    {        FileInfo fi = new FileInfo(s);        Console.WriteLine("文件路径:{0}", s);        Console.WriteLine("文件名称:{0}", fi.Name);        Console.WriteLine("文件类型:{0}", fi.Extension);        Console.WriteLine("文件大小:{0} K", fi.Length / 1024);        Console.WriteLine("文件创建时间:{0}", fi.CreationTime);        Console.WriteLine("上次访问时间:{0}", fi.LastAccessTime);        Console.WriteLine("上次写入时间:{0}", fi.LastWriteTime);    }    catch (Exception ex)    {        Console.WriteLine(ex.Message);    }}

    MD5校验

    MD5算法将文件视为大文本信息,通过不可逆的字符串变换生成唯一的MD5摘要。以下是实现MD5校验的代码片段:

    static void GetMD5(string s){    try    {        FileStream file = new FileStream(s, FileMode.Open);        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();        byte[] retval = md5.ComputeHash(file);        file.Close();        StringBuilder sc = new StringBuilder();        for (int i = 0; i < retval.Length; i++)        {            sc.Append(retval[i].ToString("x2"));        }        Console.WriteLine("文件MD5:{0}", sc);    }    catch (Exception ex)    {        Console.WriteLine(ex.Message);    }}

    SHA1校验

    SHA1算法生成160位的消息摘要,用于进一步验证文件的完整性。以下是SHA1校验的实现代码:

    static void GetSHA1(string s){    try    {        FileStream file = new FileStream(s, FileMode.Open);        SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();        byte[] retval = sha1.ComputeHash(file);        file.Close();        StringBuilder sc = new StringBuilder();        for (int i = 0; i < retval.Length; i++)        {            sc.Append(retval[i].ToString("x2"));        }        Console.WriteLine("文件SHA1:{0}", sc);    }    catch (Exception ex)    {        Console.WriteLine(ex.Message);    }}

    工具发布

    本工具使用.NET Framework 4.0编写,适用于大多数Windows系统。通过drag-and-drop功能,用户可以轻松添加文件进行校验,操作简便且高效。

    如需下载工具,请访问以下链接:

    下载链接

    如有任何问题或建议,欢迎在评论区留言。

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

    你可能感兴趣的文章
    npm和package.json那些不为常人所知的小秘密
    查看>>
    npm和yarn清理缓存命令
    查看>>
    npm和yarn的使用对比
    查看>>
    npm如何清空缓存并重新打包?
    查看>>
    npm学习(十一)之package-lock.json
    查看>>
    npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
    查看>>
    npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
    查看>>
    npm安装教程
    查看>>
    npm报错Cannot find module ‘webpack‘ Require stack
    查看>>
    npm报错Failed at the node-sass@4.14.1 postinstall script
    查看>>
    npm报错fatal: Could not read from remote repository
    查看>>
    npm报错File to import not found or unreadable: @/assets/styles/global.scss.
    查看>>
    npm报错TypeError: this.getOptions is not a function
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
    查看>>
    npm版本过高问题
    查看>>
    npm的“--force“和“--legacy-peer-deps“参数
    查看>>
    npm的安装和更新---npm工作笔记002
    查看>>
    npm的常用操作---npm工作笔记003
    查看>>
    npm的常用配置项---npm工作笔记004
    查看>>