Files
2026-01-28 16:44:19 +08:00

160 lines
5.8 KiB
C#

using System;
using System.Windows.Forms;
namespace .DATA
{
/// <summary>
/// 密码生成器 - 用于生成正确的密码哈希
/// </summary>
public class PasswordGenerator : Form
{
private TextBox txtPassword;
private TextBox txtSalt;
private TextBox txtHash;
private Button btnGenerate;
private Label lblPassword;
private Label lblSalt;
private Label lblHash;
private Button btnGenerateSalt;
private TextBox txtSqlUpdate;
private Label lblSql;
public PasswordGenerator()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.lblPassword = new Label();
this.txtPassword = new TextBox();
this.lblSalt = new Label();
this.txtSalt = new TextBox();
this.btnGenerateSalt = new Button();
this.lblHash = new Label();
this.txtHash = new TextBox();
this.btnGenerate = new Button();
this.lblSql = new Label();
this.txtSqlUpdate = new TextBox();
this.SuspendLayout();
// lblPassword
this.lblPassword.Location = new System.Drawing.Point(20, 20);
this.lblPassword.Size = new System.Drawing.Size(100, 20);
this.lblPassword.Text = "密码:";
// txtPassword
this.txtPassword.Location = new System.Drawing.Point(120, 20);
this.txtPassword.Size = new System.Drawing.Size(300, 25);
// lblSalt
this.lblSalt.Location = new System.Drawing.Point(20, 60);
this.lblSalt.Size = new System.Drawing.Size(100, 20);
this.lblSalt.Text = "盐值:";
// txtSalt
this.txtSalt.Location = new System.Drawing.Point(120, 60);
this.txtSalt.Size = new System.Drawing.Size(300, 25);
this.txtSalt.ReadOnly = true;
// btnGenerateSalt
this.btnGenerateSalt.Location = new System.Drawing.Point(430, 58);
this.btnGenerateSalt.Size = new System.Drawing.Size(100, 28);
this.btnGenerateSalt.Text = "生成盐值";
this.btnGenerateSalt.Click += BtnGenerateSalt_Click;
// lblHash
this.lblHash.Location = new System.Drawing.Point(20, 100);
this.lblHash.Size = new System.Drawing.Size(100, 20);
this.lblHash.Text = "密码哈希:";
// txtHash
this.txtHash.Location = new System.Drawing.Point(120, 100);
this.txtHash.Size = new System.Drawing.Size(410, 25);
this.txtHash.ReadOnly = true;
// btnGenerate
this.btnGenerate.Location = new System.Drawing.Point(220, 140);
this.btnGenerate.Size = new System.Drawing.Size(120, 35);
this.btnGenerate.Text = "生成哈希";
this.btnGenerate.Click += BtnGenerate_Click;
// lblSql
this.lblSql.Location = new System.Drawing.Point(20, 190);
this.lblSql.Size = new System.Drawing.Size(100, 20);
this.lblSql.Text = "SQL更新语句:";
// txtSqlUpdate
this.txtSqlUpdate.Location = new System.Drawing.Point(20, 220);
this.txtSqlUpdate.Size = new System.Drawing.Size(510, 100);
this.txtSqlUpdate.Multiline = true;
this.txtSqlUpdate.ReadOnly = true;
this.txtSqlUpdate.ScrollBars = ScrollBars.Vertical;
// PasswordGenerator
this.ClientSize = new System.Drawing.Size(560, 350);
this.Controls.Add(this.lblPassword);
this.Controls.Add(this.txtPassword);
this.Controls.Add(this.lblSalt);
this.Controls.Add(this.txtSalt);
this.Controls.Add(this.btnGenerateSalt);
this.Controls.Add(this.lblHash);
this.Controls.Add(this.txtHash);
this.Controls.Add(this.btnGenerate);
this.Controls.Add(this.lblSql);
this.Controls.Add(this.txtSqlUpdate);
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.StartPosition = FormStartPosition.CenterScreen;
this.Text = "密码哈希生成器";
this.ResumeLayout(false);
this.PerformLayout();
}
private void BtnGenerateSalt_Click(object sender, EventArgs e)
{
txtSalt.Text = PasswordHelper.GenerateSalt();
}
private void BtnGenerate_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtPassword.Text))
{
MessageBox.Show("请输入密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrEmpty(txtSalt.Text))
{
MessageBox.Show("请先生成盐值!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
string hash = PasswordHelper.HashPassword(txtPassword.Text, txtSalt.Text);
txtHash.Text = hash;
// 生成SQL更新语句
string sql = $@"-- 更新用户密码
UPDATE sys_users
SET password_hash = '{hash}',
salt = '{txtSalt.Text}'
WHERE username = 'admin'; -- 修改为对应的用户名";
txtSqlUpdate.Text = sql;
MessageBox.Show("密码哈希生成成功!\n\n可以复制SQL语句到数据库执行。", "成功",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//[STAThread]
//static void Main()
//{
// Application.EnableVisualStyles();
// Application.SetCompatibleTextRenderingDefault(false);
// Application.Run(new PasswordGenerator());
//}
}
}