zoukankan      html  css  js  c++  java
  • C#文件MD5和SHA1值的类

    代码
    public class MD5
        {
            
    public MD5()
            {
                
            }

            
    private static byte[] ConvertStringToByteArray(string data)
            {
                
    return(new System.Text.UnicodeEncoding()).GetBytes(data);
            }

            
    private static System.IO.FileStream GetFileStream(string pathName)
            {
                
    return(new System.IO.FileStream(pathName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite));
            }

            
    public static string GetSHA1Hash(string pathName)
            {
                
    string strResult = "";
                
    string strHashData = "";

                
    byte[] arrbytHashValue;
                System.IO.FileStream oFileStream 
    = null;

                System.Security.Cryptography.SHA1CryptoServiceProvider oSHA1Hasher 
    = new System.Security.Cryptography.SHA1CryptoServiceProvider();

                
    try
                {
                    oFileStream 
    = GetFileStream(pathName);
                    arrbytHashValue 
    = oSHA1Hasher.ComputeHash(oFileStream);
                    oFileStream.Close();

                    strHashData 
    = System.BitConverter.ToString(arrbytHashValue);
                    strHashData 
    = strHashData.Replace("-""");
                    strResult 
    = strHashData;
                }
                
    catch(System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message, 
    "Error!", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error, System.Windows.Forms.MessageBoxDefaultButton.Button1);
                }

                
    return(strResult);
            }

            
    public static string GetMD5Hash(string pathName)
            {
                
    string strResult = "";
                
    string strHashData = "";

                
    byte[] arrbytHashValue;
                System.IO.FileStream oFileStream 
    = null;

                System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher 
    = new System.Security.Cryptography.MD5CryptoServiceProvider();

                
    try
                {
                    oFileStream 
    = GetFileStream(pathName);
                    arrbytHashValue 
    = oMD5Hasher.ComputeHash(oFileStream);
                    oFileStream.Close();

                    strHashData 
    = System.BitConverter.ToString(arrbytHashValue);
                    strHashData 
    = strHashData.Replace("-""");
                    strResult 
    = strHashData;
                }
                
    catch(System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message, 
    "Error!", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error, System.Windows.Forms.MessageBoxDefaultButton.Button1);
                }

                
    return(strResult);
            }

        }

    使用方法:

    MD5.GetMD5Hash(textBox1.Text);

    textBox1中为文件的路径

  • 相关阅读:
    mysql数据库优化课程---3、数据库设计是什么
    mysql数据库优化课程---2、命令其实也就是那几个单词
    mysql数据库优化课程---1、数据库的本质是什么
    php特级课---4、网站服务监控(常用网站服务监控软件有哪些)
    php特级课---5、网络数据转发原理
    php特级课---3、常用的网站加速技术有哪些
    php特级课---2、网站大数据如何存储
    php特级课---1、网站大访问量如何解决
    网络工程师课程---7、网络通信综合实验(做网络基础综合实验 用什么软件)
    Objective-C路成魔【2-Objective-C 规划】
  • 原文地址:https://www.cnblogs.com/wangbin5542/p/1747468.html
Copyright © 2011-2022 走看看