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

95 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Security.Cryptography;
namespace SicUI
{
/// <summary>
/// WinDataView.xaml 的交互逻辑
/// </summary>
public partial class EditorPassCheckView : Window
{
private string _sPassword = "";
//
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()
{
string sPassword = txtPass.Password.Trim();
if (sPassword.Length > 0)
{
if (CheckPass(sPassword))
{
this.DialogResult = true;
this.Close();
}
else
{
MessageBox.Show("Wrong password!");
}
}
else
{
MessageBox.Show("Please intput password!");
}
}
private bool CheckPass(string sPassword)
{
MD5 m = MD5.Create();
byte[] bPass = System.Text.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 == this.Password;
}
private void txtPass_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
ToCheck();
}
}
}
}