控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

C#获取SHA1和MD5值的实现

作者:佚名 出处:互联网 2011年07月01日 阅读:

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;
        }
    }
}

 

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统