開発環境 |
Microsoft Visual C# 2010 Express (SP1) |
実行環境 |
Microsoft Windows XP Home Edition (SP3) |
プロジェクトの種類 |
Windows フォーム アプリケーション |
プロジェクト名 |
HashChecker |
プロパティ
Form1 |
AcceptButton |
Check |
|
AllowDrop |
True |
|
MaximizeBox |
False |
|
Text |
Hash Checker |
Label |
Text |
File Path |
TextBox |
(Name) |
FilePath |
|
Enabled |
False |
Label |
Text |
MD5 |
TextBox |
(Name) |
MD5Out |
|
Enabled |
False |
TextBox |
(Name) |
MD5In |
Label |
Text |
SHA-1 |
TextBox |
(Name) |
SHA1Out |
|
Enabled |
False |
TextBox |
(Name) |
SHA1In |
Label |
Text |
File Size |
TextBox |
(Name) |
FileSizeOut |
|
Enabled |
False |
TextBox |
(Name) |
FileSizeIn |
Button |
(Name) |
Check |
|
Text |
Check |
参考
Form1.cs
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
namespace HashChecker
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
string[] filePathArray = (string[])e.Data.GetData(DataFormats.FileDrop, false);
string filePath = filePathArray[0];
FilePath.Text = filePath;
// File Size
FileInfo fi = new FileInfo(filePath);
FileSizeOut.Text = fi.Length.ToString();
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
// MD5
using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
{
byte[] bs = md5.ComputeHash(fs);
StringBuilder result = new StringBuilder();
foreach (byte b in bs)
{
result.Append(b.ToString("x2"));
}
MD5Out.Text = result.ToString();
}
fs.Seek(0, SeekOrigin.Begin);
// SHA-1
using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
{
byte[] bs = sha1.ComputeHash(fs);
StringBuilder result = new StringBuilder();
foreach (byte b in bs)
{
result.Append(b.ToString("x2"));
}
SHA1Out.Text = result.ToString();
}
fs.Close();
}
private void Check_Click(object sender, EventArgs e)
{
StringBuilder message = new StringBuilder();
int error = 0;
// File Size
message.Append("File Size: ");
string fileSize = FileSizeIn.Text.Replace(",", "");
if (FileSizeOut.Text == fileSize)
{
message.Append("OK");
}
else
{
message.Append("NG");
error++;
}
// MD5
message.Append("\nMD5: ");
if (String.Compare(MD5Out.Text, MD5In.Text, true) == 0)
{
message.Append("OK");
}
else
{
message.Append("NG");
error++;
}
// SHA-1
message.Append("\nSHA-1: ");
if (String.Compare(SHA1Out.Text, SHA1In.Text, true) == 0)
{
message.Append("OK");
}
else
{
message.Append("NG");
error++;
}
MessageBoxIcon icon;
switch (error)
{
case 0:
icon = MessageBoxIcon.Information;
break;
case 3:
icon = MessageBoxIcon.Error;
break;
default:
icon = MessageBoxIcon.Warning;
break;
}
MessageBox.Show(message.ToString(), "Hash Checker", MessageBoxButtons.OK, icon);
}
}
}
最終更新:2013年04月24日 13:50