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中为文件的路径

  • 相关阅读:
    备注下Windows可能会用到的运行命令
    SQL2008R2 收缩数据库问题
    转:SQL Server服务器名称与默认实例名不一致的修复方法
    mac下初始化eclipse的安卓开发ndk开发环境
    eclipse android ndk 提示Type 'JNIEnv' could not be resolved 等信息解决办法
    eclipse ndk 配置和简单开发demo
    ubuntu15.10运行android studio出错unable to run mksdcard sdk tool
    Pavilion M4-1016TX 加装固态硬盘(SSD)+UEFI+GPT安装WIN8.1
    package.json和package-lock.json的区别
    Vue生命周期中mounted和created的区别
  • 原文地址:https://www.cnblogs.com/wangbin5542/p/1747468.html
Copyright © 2011-2022 走看看