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值的小工具(待验证的文件理论上无大小限制)。

点击下载

转载于:https://www.cnblogs.com/riverback-moon/archive/2011/06/28/2092485.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值