feat: autocompute sha on text changed

* rm button for manual hash computation
* add method for computing hash from string
* add method for deciding whether hash of the msg in msg box equals hash
of msg in the file (if loaded) and show a warning label when msg changes
This commit is contained in:
citizen-VM 2021-01-10 21:58:48 +01:00
parent 4d4c756c6a
commit 8d5c8e59fb
Signed by: wanderer
GPG Key ID: 6391444A736EEE7E
4 changed files with 77 additions and 25 deletions

View File

@ -9,7 +9,7 @@ namespace KRY_0x04
{
class Dsapls
{
internal string sha256sum(string path)
internal string sha256sum_from_path(string path)
{
string sha256sum = "";
try
@ -24,8 +24,25 @@ namespace KRY_0x04
}
catch (Exception e)
{
MessageBox.Show("https://xkcd.com/2200 \nerror: " + e.ToString(), "this should never have happened",
MessageBoxButtons.OK, MessageBoxIcon.Error);
show_exc_msgbox(e.ToString());
}
return sha256sum;
}
internal string sha256sum_from_string(string text)
{
string sha256sum = "";
try
{
SHA256Managed sha256_provider = new SHA256Managed();
byte[] bytes = Encoding.UTF8.GetBytes(text);
byte[] hash = sha256_provider.ComputeHash(bytes);
sha256sum = BitConverter.ToString(hash).Replace("-", string.Empty).ToLower();
}
catch (Exception e)
{
show_exc_msgbox(e.ToString());
}
return sha256sum;
}
@ -57,5 +74,11 @@ namespace KRY_0x04
}
return decrypted_text;
}
private void show_exc_msgbox(string e)
{
MessageBox.Show("https://xkcd.com/2200 \nerror: " + e, "this should never have happened", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}

30
Form1.Designer.cs generated
View File

@ -31,7 +31,6 @@
this.msgbox = new System.Windows.Forms.RichTextBox();
this.button1 = new System.Windows.Forms.Button();
this.msgpathbox = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
@ -46,6 +45,7 @@
this.privkeypathbox = new System.Windows.Forms.TextBox();
this.pubkeypathbox = new System.Windows.Forms.TextBox();
this.msgdetailsbox = new System.Windows.Forms.TextBox();
this.msgs_differ_label = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// msgbox
@ -55,6 +55,7 @@
this.msgbox.Size = new System.Drawing.Size(437, 102);
this.msgbox.TabIndex = 5;
this.msgbox.Text = "";
this.msgbox.TextChanged += new System.EventHandler(this.msgbox_TextChanged);
//
// button1
//
@ -74,16 +75,6 @@
this.msgpathbox.Size = new System.Drawing.Size(437, 20);
this.msgpathbox.TabIndex = 2;
//
// button2
//
this.button2.Location = new System.Drawing.Point(467, 203);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(115, 23);
this.button2.TabIndex = 6;
this.button2.Text = "compute sha256sum";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.compute_sha256sum_button_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(467, 342);
@ -217,11 +208,25 @@
this.msgdetailsbox.Size = new System.Drawing.Size(437, 51);
this.msgdetailsbox.TabIndex = 25;
//
// msgs_differ_label
//
this.msgs_differ_label.AutoSize = true;
this.msgs_differ_label.BackColor = System.Drawing.SystemColors.HighlightText;
this.msgs_differ_label.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.msgs_differ_label.ForeColor = System.Drawing.Color.Firebrick;
this.msgs_differ_label.Location = new System.Drawing.Point(464, 204);
this.msgs_differ_label.Name = "msgs_differ_label";
this.msgs_differ_label.Size = new System.Drawing.Size(198, 16);
this.msgs_differ_label.TabIndex = 26;
this.msgs_differ_label.Text = "msg changed but not saved";
this.msgs_differ_label.Visible = false;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(814, 496);
this.Controls.Add(this.msgs_differ_label);
this.Controls.Add(this.msgdetailsbox);
this.Controls.Add(this.pubkeypathbox);
this.Controls.Add(this.privkeypathbox);
@ -236,7 +241,6 @@
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.msgpathbox);
this.Controls.Add(this.button1);
this.Controls.Add(this.msgbox);
@ -257,7 +261,6 @@
private System.Windows.Forms.RichTextBox msgbox;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox msgpathbox;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
@ -272,6 +275,7 @@
private System.Windows.Forms.TextBox privkeypathbox;
private System.Windows.Forms.TextBox pubkeypathbox;
private System.Windows.Forms.TextBox msgdetailsbox;
private System.Windows.Forms.Label msgs_differ_label;
}
}

View File

@ -25,11 +25,6 @@ namespace KRY_0x04
lh.save_msg(msgbox);
}
private void compute_sha256sum_button_Click(object sender, EventArgs e)
{
/* compute sha256 */
lh.compute_sha256sum(msgpathbox, sha256sumbox);
}
private void load_privkey_button_Click(object sender, EventArgs e)
{
@ -78,5 +73,10 @@ namespace KRY_0x04
privkeybox.Text = "";
pubkeybox.Text = "";
}
private void msgbox_TextChanged(object sender, EventArgs e)
{
lh.do_msgs_differ(msgbox.Text, msgpathbox.Text, msgs_differ_label, sha256sumbox);
}
}
}

View File

@ -61,11 +61,36 @@ namespace KRY_0x04
}
internal void do_msgs_differ(string msg, string path, Label do_they_differ, TextBox sha256sumbox)
{
if (msg != "" && path != "")
{
string msg_hash = dsapls.sha256sum_from_string(msg);
string file_msg_hash = dsapls.sha256sum_from_path(path);
if (msg_hash.Equals(file_msg_hash))
{
do_they_differ.Visible = false;
sha256sumbox.Text = msg_hash;
}
else
{
do_they_differ.Visible = true;
sha256sumbox.Text = msg_hash;
}
} else if (msg != "")
{
do_they_differ.Visible = false;
string msg_hash = dsapls.sha256sum_from_string(msg);
sha256sumbox.Text = msg_hash;
}
}
internal void compute_sha256sum(TextBox msgpathbox, TextBox sha256sumbox)
{
if (msgpathbox.Text != "")
{
sha256sumbox.Text = dsapls.sha256sum(msgpathbox.Text);
sha256sumbox.Text = dsapls.sha256sum_from_path(msgpathbox.Text);
}
else
{
@ -287,15 +312,15 @@ namespace KRY_0x04
}
}
}
if (dsapls.sha256sum(orig_message) == verified)
if (dsapls.sha256sum_from_path(orig_message) == verified)
{
MessageBox.Show($"{dsapls.sha256sum(orig_message)} == {verified}", "we good");
MessageBox.Show($"{dsapls.sha256sum_from_path(orig_message)} == {verified}", "we good");
}
else
{
Console.WriteLine($"orig message: {orig_message}");
Console.WriteLine($"verif message: {verified}");
MessageBox.Show($"something is seriously wrong\n{dsapls.sha256sum(orig_message)} != {verified}", "checksums differ!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
MessageBox.Show($"something is seriously wrong\n{dsapls.sha256sum_from_path(orig_message)} != {verified}", "checksums differ!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}