(2) 在命名空间中输入:
using System.IO;
using System.Security.Cryptography;
(3) 在static void Main(string[] args)方法中输入以下代码:
HashAlgorithm hash=HashAlgorithm.Create();
Console.WriteLine("Enter a File Name:");
string fileName=Console.ReadLine();
FileStream fs=new FileStream(fileName, FileMode.Open);
byte[] hashBytes=hash.ComputeHash(fs);
fs.Close();
//display the hash data
Console.Write("Hash:"+BitConverter.ToString(hashBytes));
Console.Read();
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
namespace HashGui
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//textBox1为输入密码的文本框
byte[] result = Encoding.Default.GetBytes(this.textBox1.Text.Trim());
/*MD5,SHA256,SHA384,SHA512),及验证码HMACSHA1
*/
//MD5
MD5 md5 = new MD5CryptoServiceProvider();
byte[] output = md5.ComputeHash(result);
//textBox2为输出加密文本的文本框
this.textBox2.Text = BitConverter.ToString(output);
//SHA256
HashAlgorithm hash256 = HashAlgorithm.Create("SHA256");
byte[] output256 = hash256.ComputeHash(result);
this.textBox3.Text = BitConverter.ToString(output256);
//SHA384
HashAlgorithm hash384 = HashAlgorithm.Create("SHA384");
byte[] output384 = hash384.ComputeHash(result);
this.textBox4.Text = BitConverter.ToString(output384);
//SHA521
HashAlgorithm hash512 = HashAlgorithm.Create("SHA512");
byte[] output512 = hash512.ComputeHash(result);
this.textBox5.Text = BitConverter.ToString(output512);
HMACSHA1 myHMACSHA1 = new HMACSHA1();
byte[] byteText = myHMACSHA1.ComputeHash(result);
this.textBox6.Text = BitConverter.ToString(output512);
}
private void label2_Click(object sender, EventArgs e)
{
}
}
}