From d8d379a04fe6535672b237d867e22c38b182f350 Mon Sep 17 00:00:00 2001 From: Jaex Date: Sat, 2 Jan 2016 15:24:14 +0200 Subject: [PATCH] Text conversion improvements --- ShareX.HelpersLib/Cryptographic/Translator.cs | 70 +++++++++++-------- ShareX.HelpersLib/Forms/HashCheckForm.cs | 69 ++++++++++-------- ShareX.HelpersLib/Forms/HashCheckForm.resx | 22 +++--- 3 files changed, 90 insertions(+), 71 deletions(-) diff --git a/ShareX.HelpersLib/Cryptographic/Translator.cs b/ShareX.HelpersLib/Cryptographic/Translator.cs index 2130bfe6f..f66c72aef 100644 --- a/ShareX.HelpersLib/Cryptographic/Translator.cs +++ b/ShareX.HelpersLib/Cryptographic/Translator.cs @@ -39,7 +39,12 @@ public string BinaryText { get { - return Binary.Join(); + if (Binary != null && Binary.Length > 0) + { + return Binary.Join(); + } + + return null; } } @@ -50,7 +55,12 @@ public string HexadecimalText { get { - return Hexadecimal.Join().ToUpperInvariant(); + if (Hexadecimal != null && Hexadecimal.Length > 0) + { + return Hexadecimal.Join().ToUpperInvariant(); + } + + return null; } } @@ -61,7 +71,12 @@ public string ASCIIText { get { - return ASCII.Join(); + if (ASCII != null && ASCII.Length > 0) + { + return ASCII.Join(); + } + + return null; } } @@ -85,10 +100,20 @@ public string ASCIIText // http://en.wikipedia.org/wiki/RIPEMD public string RIPEMD160 { get; private set; } + public void Clear() + { + Text = Base64 = CRC32 = MD5 = SHA1 = SHA256 = SHA384 = SHA512 = RIPEMD160 = null; + Binary = null; + Hexadecimal = null; + ASCII = null; + } + public bool EncodeText(string text) { try { + Clear(); + if (!string.IsNullOrEmpty(text)) { Text = text; @@ -117,13 +142,14 @@ public bool DecodeBinary(string binary) { try { - string result = TranslatorHelper.BinaryToText(binary); - return EncodeText(result); + Text = TranslatorHelper.BinaryToText(binary); + return !string.IsNullOrEmpty(Text); } catch { } + Text = null; return false; } @@ -131,13 +157,14 @@ public bool DecodeHex(string hex) { try { - string result = TranslatorHelper.HexadecimalToText(hex); - return EncodeText(result); + Text = TranslatorHelper.HexadecimalToText(hex); + return !string.IsNullOrEmpty(Text); } catch { } + Text = null; return false; } @@ -145,13 +172,14 @@ public bool DecodeASCII(string ascii) { try { - string result = TranslatorHelper.ASCIIToText(ascii); - return EncodeText(result); + Text = TranslatorHelper.ASCIIToText(ascii); + return !string.IsNullOrEmpty(Text); } catch { } + Text = null; return false; } @@ -159,33 +187,17 @@ public bool DecodeBase64(string base64) { try { - string result = TranslatorHelper.Base64ToText(base64); - return EncodeText(result); + Text = TranslatorHelper.Base64ToText(base64); + return !string.IsNullOrEmpty(Text); } catch { } + Text = null; return false; } - public static bool Test() - { - bool result = true; - Translator translator = new Translator(); - string text = Helpers.GetAllCharacters(); - translator.EncodeText(text); - translator.DecodeBinary(translator.BinaryText); - result &= translator.Text == text; - translator.DecodeHex(translator.HexadecimalText); - result &= translator.Text == text; - //translator.DecodeASCII(translator.ASCIIText); - //result &= translator.Text == text; - translator.DecodeBase64(translator.Base64); - result &= translator.Text == text; - return result; - } - public string HashToString() { StringBuilder sb = new StringBuilder(); @@ -204,7 +216,7 @@ public override string ToString() StringBuilder sb = new StringBuilder(); sb.AppendLine($"Text: {Text}"); sb.AppendLine($"Binary: {BinaryText}"); - sb.AppendLine($"Hex: {HexadecimalText}"); + sb.AppendLine($"Hexadecimal: {HexadecimalText}"); sb.AppendLine($"ASCII: {ASCIIText}"); sb.AppendLine($"Base64: {Base64}"); sb.Append(HashToString()); diff --git a/ShareX.HelpersLib/Forms/HashCheckForm.cs b/ShareX.HelpersLib/Forms/HashCheckForm.cs index ddc706cc0..e3666f956 100644 --- a/ShareX.HelpersLib/Forms/HashCheckForm.cs +++ b/ShareX.HelpersLib/Forms/HashCheckForm.cs @@ -47,10 +47,6 @@ public HashCheckForm() hashCheck.FileCheckCompleted += fileCheck_FileCheckCompleted; translator = new Translator(); - -#if DEBUG - if (!Translator.Test()) MessageBox.Show("Text conversion failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); -#endif } #region File hash check @@ -143,21 +139,35 @@ private void txtFilePath_DragDrop(object sender, DragEventArgs e) #region Text conversions private void FillConversionInfo() + { + FillConversionInfo(translator.Text); + } + + private void FillConversionInfo(string text) { if (translator != null) { - txtHashCheckText.Text = translator.Text; - txtHashCheckBinary.Text = translator.BinaryText; - txtHashCheckHex.Text = translator.HexadecimalText; - txtHashCheckASCII.Text = translator.ASCIIText; - txtHashCheckBase64.Text = translator.Base64; - txtHashCheckHash.Text = translator.HashToString(); + if (!string.IsNullOrEmpty(text)) + { + translator.EncodeText(text); + txtHashCheckText.Text = translator.Text; + txtHashCheckBinary.Text = translator.BinaryText; + txtHashCheckHex.Text = translator.HexadecimalText; + txtHashCheckASCII.Text = translator.ASCIIText; + txtHashCheckBase64.Text = translator.Base64; + txtHashCheckHash.Text = translator.HashToString(); + } + else + { + translator.Clear(); + txtHashCheckText.Text = txtHashCheckBinary.Text = txtHashCheckHex.Text = txtHashCheckASCII.Text = txtHashCheckBase64.Text = txtHashCheckHash.Text = ""; + } } } private void btnHashCheckCopyAll_Click(object sender, EventArgs e) { - if (translator != null && !string.IsNullOrEmpty(translator.Text)) + if (translator != null) { string text = translator.ToString(); @@ -170,42 +180,39 @@ private void btnHashCheckCopyAll_Click(object sender, EventArgs e) private void btnHashCheckEncodeText_Click(object sender, EventArgs e) { - if (translator.EncodeText(txtHashCheckText.Text)) - { - FillConversionInfo(); - } + FillConversionInfo(txtHashCheckText.Text); } private void btnHashCheckDecodeBinary_Click(object sender, EventArgs e) { - if (translator.DecodeBinary(txtHashCheckBinary.Text)) - { - FillConversionInfo(); - } + string binary = txtHashCheckBinary.Text; + translator.DecodeBinary(binary); + FillConversionInfo(); + txtHashCheckBinary.Text = binary; } private void btnHashCheckDecodeHex_Click(object sender, EventArgs e) { - if (translator.DecodeHex(txtHashCheckHex.Text)) - { - FillConversionInfo(); - } + string hex = txtHashCheckHex.Text; + translator.DecodeHex(hex); + FillConversionInfo(); + txtHashCheckHex.Text = hex; } private void btnHashCheckDecodeASCII_Click(object sender, EventArgs e) { - if (translator.DecodeASCII(txtHashCheckASCII.Text)) - { - FillConversionInfo(); - } + string ascii = txtHashCheckASCII.Text; + translator.DecodeASCII(ascii); + FillConversionInfo(); + txtHashCheckASCII.Text = ascii; } private void btnHashCheckDecodeBase64_Click(object sender, EventArgs e) { - if (translator.DecodeBase64(txtHashCheckBase64.Text)) - { - FillConversionInfo(); - } + string base64 = txtHashCheckBase64.Text; + translator.DecodeBase64(base64); + FillConversionInfo(); + txtHashCheckBase64.Text = base64; } #endregion Text conversions diff --git a/ShareX.HelpersLib/Forms/HashCheckForm.resx b/ShareX.HelpersLib/Forms/HashCheckForm.resx index a9dbaa78d..4cdabdcea 100644 --- a/ShareX.HelpersLib/Forms/HashCheckForm.resx +++ b/ShareX.HelpersLib/Forms/HashCheckForm.resx @@ -195,7 +195,7 @@ True - 5, 104 + 5, 136 40, 13 @@ -222,7 +222,7 @@ True - 5, 152 + 5, 184 41, 13 @@ -249,7 +249,7 @@ True - 528, 76 + 432, 110 21, 13 @@ -273,7 +273,7 @@ 11 - 120, 70 + 8, 104 128, 24 @@ -300,7 +300,7 @@ 8, 72 - 104, 21 + 128, 21 4 @@ -318,10 +318,10 @@ 7 - 256, 70 + 144, 104 - 264, 24 + 280, 24 7 @@ -339,7 +339,7 @@ 6 - 8, 120 + 8, 152 552, 20 @@ -360,7 +360,7 @@ 4 - 8, 168 + 8, 200 552, 20 @@ -733,13 +733,13 @@ 5, 120 - 29, 13 + 71, 13 6 - Hex: + Hexadecimal: lblHashCheckHex