⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmcomputehash.cs

📁 在设计程序时,需要经常计算一个字串的哈希值,得到加密文本,生成加密过的配置文件或都转换EXCEL文件到SQL数据库等等,根据自己习惯定制部分功能,由于时间关系,没有详细说明
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;

namespace LingangTools
{
    public partial class FrmComputeHash : Form
    {
        public FrmComputeHash()
        {
            InitializeComponent();
        }

        private bool isTextContent = true;

        private HashAlgorithm ha;

        private void btn_Compute_Click(object sender, EventArgs e)
        {
            byte[] hashByte;
            if (!string.IsNullOrEmpty(txt_Content.Text))
            {
                if (isTextContent)
                {
                    hashByte = ComputeHash(GetBytesFromString(txt_Content.Text));
                }
                else
                {
                    hashByte = ComputeHash(GetBytesFromFile(txt_Content.Text));
                }

                txt_Base64String.Text = System.Convert.ToBase64String(hashByte);
                System.Text.StringBuilder sb = new StringBuilder();
                for (int i = 0; i < hashByte.Length; i++)
                {
                    sb.Append(hashByte[i]);
                    if (i < hashByte.Length - 1)
                        sb.Append(",");
                }
                txt_Binary.Text = sb.ToString();
            }
            else
            {
                MessageBox.Show("不能进行摘要计算,请输入文本或选择文件后,再进行计算","内容为空",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
        }

        private void radioButton_File_CheckedChanged(object sender, EventArgs e)
        {
            isTextContent = radioButton_Text.Checked;
            btn_SelectFile.Visible = radioButton_File.Checked;
            txt_Content.Text = string.Empty;
        }

        private void btn_SelectFile_Click(object sender, EventArgs e)
        {
            if (ofg_SelectFile.ShowDialog() == DialogResult.OK)
            {
                txt_Content.Text = ofg_SelectFile.FileName;
            }
        }

        private byte[] GetBytesFromFile(string fileName)
        {
            if (!File.Exists(fileName))
            {
                MessageBox.Show(string.Format("<{0}>不存在,请确定文件是否存在后,再进行摘要计算", fileName), "文件不存在", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
            FileStream fs = new FileStream(fileName, FileMode.Open);
            BinaryReader br = new BinaryReader(fs);
            try
            {
                byte[] result;
                if (fs.Length < int.MaxValue)
                {
                    result = br.ReadBytes((int)fs.Length);
                }
                else
                {
                    byte[] b;
                    result = new byte[fs.Length];
                    long segment = fs.Length % int.MaxValue;
                    long segmentLastLength = fs.Length-segment * int.MaxValue;
                    for (long i = 0; i < segment; i++)
                    {
                        b = br.ReadBytes(int.MaxValue);
                        for (int j = 0; j < int.MaxValue; j++)
                            result[j+i*int.MaxValue] = b[j];
                    }
                    
                    b = br.ReadBytes((int)segmentLastLength);
                    for (int k = 0; k < segmentLastLength; k++)
                    {
                        result[int.MaxValue * segment + k] = b[k];
                    }                    
                }

                return result;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
            finally
            {
                br.Close();
                fs.Close();
            }
        }

        private byte[] GetBytesFromString(string content)
        {
            return System.Text.Encoding.Default.GetBytes(content);
        }

        private byte[] ComputeHash(byte[] input)
        {
            return ha.ComputeHash(input);
        }

        private void radioButton_SHA1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton_SHA1.Checked)
                ha = SHA1.Create();
            else if (radioButton_MD5.Checked)
                ha = MD5.Create();
            else if (radioButton_SHA256.Checked)
                ha = SHA256.Create();
            else if (radioButton_SHA384.Checked)
                ha = SHA384.Create();
            else if (radioButton_SHA512.Checked)
                ha = SHA512.Create();
        }

        private void FrmComputeHash_Load(object sender, EventArgs e)
        {
            ha = SHA1.Create();
        }

        private void txt_Base64String_DoubleClick(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txt_Base64String.Text))
                Clipboard.SetText(txt_Base64String.Text);
        }

        private void txt_Binary_DoubleClick(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txt_Binary.Text))
                Clipboard.SetText(txt_Binary.Text);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -