C#获取SHA1和MD5值的实现

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Security.Cryptography;
using System.IO;

namespace WFSHA1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected static string sha1hash = "";
        protected static string md5hash = "";
        protected static string pathName = "";
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.ShowDialog();
            pathName = ofd.FileName;

            WaitCallback wc = new WaitCallback(getResult);
            ThreadPool.QueueUserWorkItem(wc);
        }
        /// <summary>
        /// 计算结果
        /// </summary>
        /// <param name="pathName"></param>
        /// <returns></returns>
        public void getResult(object obj)
        {
            try
            {
                if (pathName != "" && System.IO.File.Exists(pathName))
                {
                    FileStream fs = new FileStream(pathName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
                    lock (sha1hash)
                    {
                        sha1hash = getSHA1(fs);
                    }
                    lock (md5hash)
                    {
                        md5hash = getMD5(fs);
                    }
                    fs.Close();
                    MessageBox.Show("计算完毕,请查看结果!");
                }

            }
            catch
            {
                MessageBox.Show("出错了!");
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = sha1hash;
            textBox2.Text = md5hash;
        }

        /// <summary>
        /// 计算md5
        /// </summary>
        /// <returns></returns>
        public string getMD5(FileStream oFileStream)
        {
           
            MD5 md5 = MD5.Create();
            byte[] result = md5.ComputeHash(oFileStream);
            string sResult = BitConverter.ToString(result).Replace("-","");
            return sResult;

        }

        /// <summary>
        /// 计算sha1
        /// </summary>
        /// <returns></returns>
        public string getSHA1(FileStream oFileStream)
        {
            string strResult = "";
            string strHashData = "";
            byte[] arrbytHashValue;

            System.Security.Cryptography.SHA1CryptoServiceProvider oSHA1Hasher = new System.Security.Cryptography.SHA1CryptoServiceProvider();
            arrbytHashValue = oSHA1Hasher.ComputeHash(oFileStream);

            strHashData = System.BitConverter.ToString(arrbytHashValue);
            strHashData = strHashData.Replace("-", "");
            strResult = strHashData;
            return strResult;
        }
    }
}

参考网上的实现,弄了个获取sha1 和 MD5值的小工具(待验证的文件理论上无大小限制)。

点击下载

posted @ 2011-06-28 17:26  moon.riverback  阅读(982)  评论(0编辑  收藏  举报