Sic04/SicUI/Models/RecipeEditors/EditorPassCheckView.xaml.cs

91 lines
2.1 KiB
C#

using System;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Security.Cryptography;
namespace SicUI
{
/// <summary>
/// WinDataView.xaml 的交互逻辑
/// </summary>
public partial class EditorPassCheckView : Window
{
private string _sPassword = "";
public EditorPassCheckView()
{
}
//
public string Password { get
{
return _sPassword; }
set
{
_sPassword = value;
}
}
public EditorPassCheckView(Object obj)
{
InitializeComponent();
//
txtPass.Focus();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ToCheck();
}
private void ToCheck()
{
var sPassword = txtPass.Password;
if (sPassword.Length > 0)
{
if (CheckPass(sPassword))
{
DialogResult = true;
Close();
}
else
{
MessageBox.Show("Password incorrect!","Error", MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
else
{
MessageBox.Show("Please input password!","Error", MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
private bool CheckPass(string sPassword)
{
MD5 m = MD5.Create();
byte[] bPass = Encoding.UTF8.GetBytes(sPassword);
byte[] md5bPass = m.ComputeHash(bPass);
string sPass = "";
for(int i=0;i<md5bPass.Length;i++)
{
sPass += md5bPass[i].ToString("X2");
}
return sPass == Password;
}
private void txtPass_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
ToCheck();
}
}
}
}