From ec4fd3eaa6933a2a973ed8636d71ebd9dd6716b1 Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Thu, 22 Aug 2024 19:07:59 +0800 Subject: [PATCH] update --- OTP/Form1.Designer.cs | 81 +++++++++------- OTP/Form1.cs | 83 +++++++--------- OTP/Form1.resx | 4 +- OTP/Form2.Designer.cs | 8 +- OTP/Form2.cs | 85 +++++++--------- OTP/Form3.Designer.cs | 71 ++------------ OTP/Form3.cs | 218 ++++++------------------------------------ OTP/Form3.resx | 4 +- OTP/Form4.Designer.cs | 118 ----------------------- OTP/Form4.cs | 81 ---------------- OTP/Form4.resx | 120 ----------------------- OTP/OTP.csproj | 13 +-- 12 files changed, 164 insertions(+), 722 deletions(-) delete mode 100644 OTP/Form4.Designer.cs delete mode 100644 OTP/Form4.cs delete mode 100644 OTP/Form4.resx diff --git a/OTP/Form1.Designer.cs b/OTP/Form1.Designer.cs index 663eb90..ed88346 100644 --- a/OTP/Form1.Designer.cs +++ b/OTP/Form1.Designer.cs @@ -36,27 +36,28 @@ label2 = new System.Windows.Forms.Label(); button3 = new System.Windows.Forms.Button(); button4 = new System.Windows.Forms.Button(); - button5 = new System.Windows.Forms.Button(); progressBar1 = new System.Windows.Forms.ProgressBar(); label3 = new System.Windows.Forms.Label(); + label4 = new System.Windows.Forms.Label(); + label5 = new System.Windows.Forms.Label(); SuspendLayout(); // // listBox1 // - listBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + listBox1.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0); listBox1.FormattingEnabled = true; - listBox1.ItemHeight = 20; + listBox1.ItemHeight = 27; listBox1.Location = new System.Drawing.Point(49, 44); listBox1.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); listBox1.Name = "listBox1"; - listBox1.Size = new System.Drawing.Size(551, 404); + listBox1.Size = new System.Drawing.Size(551, 409); listBox1.TabIndex = 0; - listBox1.SelectedIndexChanged += listBox1_SelectedIndexChanged; + listBox1.SelectedIndexChanged += ListBox1_SelectedIndexChanged; // // label1 // - label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point); - label1.Location = new System.Drawing.Point(640, 68); + label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Bold); + label1.Location = new System.Drawing.Point(640, 56); label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); label1.Name = "label1"; label1.Size = new System.Drawing.Size(230, 43); @@ -67,32 +68,32 @@ // button1 // button1.Enabled = false; - button1.Location = new System.Drawing.Point(640, 339); + button1.Location = new System.Drawing.Point(640, 354); button1.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); button1.Name = "button1"; button1.Size = new System.Drawing.Size(107, 27); button1.TabIndex = 3; button1.Text = "Generate"; button1.UseVisualStyleBackColor = true; - button1.Click += button1_Click; + button1.Click += Button1_Click; // // button2 // button2.Enabled = false; - button2.Location = new System.Drawing.Point(755, 339); + button2.Location = new System.Drawing.Point(755, 354); button2.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); button2.Name = "button2"; - button2.Size = new System.Drawing.Size(115, 128); + button2.Size = new System.Drawing.Size(115, 94); button2.TabIndex = 3; button2.Text = "Copy"; button2.UseVisualStyleBackColor = true; - button2.Click += button2_Click; + button2.Click += Button2_Click; // // label2 // label2.BackColor = System.Drawing.Color.Transparent; - label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - label2.Location = new System.Drawing.Point(640, 285); + label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F); + label2.Location = new System.Drawing.Point(640, 297); label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); label2.Name = "label2"; label2.Size = new System.Drawing.Size(230, 23); @@ -102,40 +103,29 @@ // // button3 // - button3.Location = new System.Drawing.Point(640, 373); + button3.Location = new System.Drawing.Point(640, 388); button3.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); button3.Name = "button3"; button3.Size = new System.Drawing.Size(107, 27); button3.TabIndex = 7; button3.Text = "Manage"; button3.UseVisualStyleBackColor = true; - button3.Click += button3_Click; + button3.Click += Button3_Click; // // button4 // - button4.Location = new System.Drawing.Point(640, 406); + button4.Location = new System.Drawing.Point(640, 421); button4.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); button4.Name = "button4"; button4.Size = new System.Drawing.Size(107, 27); button4.TabIndex = 8; button4.Text = "Refresh"; button4.UseVisualStyleBackColor = true; - button4.Click += button4_Click; - // - // button5 - // - button5.Location = new System.Drawing.Point(640, 441); - button5.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); - button5.Name = "button5"; - button5.Size = new System.Drawing.Size(107, 27); - button5.TabIndex = 9; - button5.Text = "Troubleshooting"; - button5.UseVisualStyleBackColor = true; - button5.Click += button5_Click; + button4.Click += Button4_Click; // // progressBar1 // - progressBar1.Location = new System.Drawing.Point(640, 165); + progressBar1.Location = new System.Drawing.Point(640, 177); progressBar1.Maximum = 30; progressBar1.Name = "progressBar1"; progressBar1.Size = new System.Drawing.Size(230, 51); @@ -143,22 +133,43 @@ // // label3 // - label3.Font = new System.Drawing.Font("Segoe UI", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - label3.Location = new System.Drawing.Point(640, 219); + label3.Font = new System.Drawing.Font("Segoe UI", 24F); + label3.Location = new System.Drawing.Point(640, 231); label3.Name = "label3"; label3.Size = new System.Drawing.Size(230, 45); label3.TabIndex = 11; label3.Text = "1"; label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // + // label4 + // + label4.AutoSize = true; + label4.Font = new System.Drawing.Font("Segoe UI", 12F); + label4.Location = new System.Drawing.Point(640, 129); + label4.Name = "label4"; + label4.Size = new System.Drawing.Size(45, 21); + label4.TabIndex = 12; + label4.Text = "Next:"; + // + // label5 + // + label5.AutoSize = true; + label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold); + label5.Location = new System.Drawing.Point(713, 130); + label5.Name = "label5"; + label5.Size = new System.Drawing.Size(92, 20); + label5.TabIndex = 13; + label5.Text = "Next Code"; + // // Form1 // AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; ClientSize = new System.Drawing.Size(904, 508); + Controls.Add(label5); + Controls.Add(label4); Controls.Add(label3); Controls.Add(progressBar1); - Controls.Add(button5); Controls.Add(button4); Controls.Add(button3); Controls.Add(label2); @@ -175,6 +186,7 @@ Load += Form1_Load; VisibleChanged += Form1_VisibleChanged; ResumeLayout(false); + PerformLayout(); } #endregion @@ -186,9 +198,10 @@ private System.Windows.Forms.Label label2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; - private System.Windows.Forms.Button button5; private System.Windows.Forms.ProgressBar progressBar1; private System.Windows.Forms.Label label3; + private System.Windows.Forms.Label label4; + private System.Windows.Forms.Label label5; } } diff --git a/OTP/Form1.cs b/OTP/Form1.cs index e6041d9..1c9b9f7 100644 --- a/OTP/Form1.cs +++ b/OTP/Form1.cs @@ -12,8 +12,8 @@ namespace OTP private byte[] secretKey; private Totp totp; private Timer timer; - private string connectionString = "Data Source=key.db;Version=3;"; - private string embeddedDatabaseResource = "OTP.empty_key.db"; + private const string connectionString = "Data Source=key.db;Version=3;"; + private const string embeddedDatabaseResource = "OTP.empty_key.db"; private int runonce = 0; public Form1() @@ -23,8 +23,10 @@ namespace OTP //secretKey = Base32Encoding.ToBytes("JBSWY3DPEHPK3PXP"); // 示例的 base32 编码 //totp = new Totp(secretKey); - timer = new Timer(); - timer.Interval = 1000; // 设置计时器间隔为 1 秒 + timer = new Timer + { + Interval = 1000 // 设置计时器间隔为 1 秒 + }; timer.Tick += Timer_Tick; } public class TitleInfo @@ -54,14 +56,16 @@ namespace OTP { // 生成新的 TOTP 密码 string newTotpCode = totp.ComputeTotp(); + string newNextTotpCode = totp.ComputeTotp(DateTime.UtcNow.AddSeconds(30)); // 在标签上显示新生成的密码 // 使用Invoke确保在UI线程上更新UI元素 this.Invoke((System.Windows.Forms.MethodInvoker)(() => label1.Text = newTotpCode)); + this.Invoke((System.Windows.Forms.MethodInvoker)(() => label5.Text = newNextTotpCode)); } - private void button1_Click(object sender, EventArgs e) + private void Button1_Click(object sender, EventArgs e) { button1.Enabled = false; // 禁用生成按钮 button2.Enabled = true; // 启用复制按钮 @@ -83,18 +87,14 @@ namespace OTP } - private void ExtractEmbeddedDatabase() + private static void ExtractEmbeddedDatabase() { // 从嵌入的资源中复制数据库文件到同目录下 try { - using (Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedDatabaseResource)) - { - using (FileStream fileStream = new FileStream("key.db", FileMode.Create)) - { - resourceStream.CopyTo(fileStream); - } - } + using Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedDatabaseResource); + using FileStream fileStream = new("key.db", FileMode.Create); + resourceStream.CopyTo(fileStream); } catch (Exception ex) { @@ -106,32 +106,26 @@ namespace OTP { try { - using (SQLiteConnection connection = new SQLiteConnection(connectionString)) + using SQLiteConnection connection = new(connectionString); + connection.Open(); + + using SQLiteCommand command = new("SELECT Id, Title, SecretKey FROM TotpData", connection); + using SQLiteDataReader reader = command.ExecuteReader(); + listBox1.Items.Clear(); // 清空现有数据 + while (reader.Read()) { - connection.Open(); + int id = reader.GetInt32(0); + string title = reader.GetString(1); + string secretKey = reader.GetString(2); - using (SQLiteCommand command = new SQLiteCommand("SELECT Id, Title, SecretKey FROM TotpData", connection)) + TitleInfo titleInfo = new() { - using (SQLiteDataReader reader = command.ExecuteReader()) - { - listBox1.Items.Clear(); // 清空现有数据 - while (reader.Read()) - { - int id = reader.GetInt32(0); - string title = reader.GetString(1); - string secretKey = reader.GetString(2); + Id = id, + Title = title, + SecretKey = secretKey + }; - TitleInfo titleInfo = new TitleInfo - { - Id = id, - Title = title, - SecretKey = secretKey - }; - - listBox1.Items.Add(titleInfo); - } - } - } + listBox1.Items.Add(titleInfo); } } catch (Exception ex) @@ -140,12 +134,11 @@ namespace OTP } } - private void listBox1_SelectedIndexChanged(object sender, EventArgs e) + private void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { if (listBox1.SelectedIndex >= 0) // Ensure an item is selected { - TitleInfo selectedTitleInfo = listBox1.SelectedItem as TitleInfo; - if (selectedTitleInfo != null) + if (listBox1.SelectedItem is TitleInfo selectedTitleInfo) { if (runonce == 0) { @@ -170,15 +163,15 @@ namespace OTP } - private void button2_Click(object sender, EventArgs e) + private void Button2_Click(object sender, EventArgs e) { Clipboard.SetText(label1.Text); } - private void button3_Click(object sender, EventArgs e) + private void Button3_Click(object sender, EventArgs e) { // 创建 Form2 实例 - Form2 form2 = new Form2(); + Form2 form2 = new(); // 显示新创建的 Form2 实例 form2.Show(); @@ -206,6 +199,7 @@ namespace OTP LoadTitlesToListBox(); runonce = 0; label1.Text = "TOTP Code"; + label1.Text = "Next Code"; button1.Enabled = false; button2.Enabled = false; label3.Text = "time"; @@ -217,15 +211,10 @@ namespace OTP } } - private void button4_Click(object sender, EventArgs e) + private void Button4_Click(object sender, EventArgs e) { LoadTitlesToListBox(); } - private void button5_Click(object sender, EventArgs e) - { - Form4 form4 = new Form4(); - form4.Show(); - } } } diff --git a/OTP/Form1.resx b/OTP/Form1.resx index e9cc343..5e7e6b5 100644 --- a/OTP/Form1.resx +++ b/OTP/Form1.resx @@ -1,7 +1,7 @@  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - \ No newline at end of file diff --git a/OTP/OTP.csproj b/OTP/OTP.csproj index 30c9236..e3f4f5a 100644 --- a/OTP/OTP.csproj +++ b/OTP/OTP.csproj @@ -23,7 +23,7 @@ true - none + full OTP.Program @@ -38,7 +38,7 @@ False - none + full @@ -59,25 +59,16 @@ - - - - - - - - -