From 53168286dedfc6bd38a2d794a050d5e3a918ca7a Mon Sep 17 00:00:00 2001 From: Jaex Date: Tue, 29 Dec 2015 03:36:10 +0200 Subject: [PATCH] fixed #1213: Added text conversions (binary, hex, ascii, base64, hash) to hash check window --- ShareX.HelpersLib/Cryptographic/Translator.cs | 118 ++-- .../Forms/HashCheckForm.Designer.cs | 224 +++++- ShareX.HelpersLib/Forms/HashCheckForm.cs | 83 ++- ShareX.HelpersLib/Forms/HashCheckForm.resx | 663 ++++++++++++++++-- 4 files changed, 953 insertions(+), 135 deletions(-) diff --git a/ShareX.HelpersLib/Cryptographic/Translator.cs b/ShareX.HelpersLib/Cryptographic/Translator.cs index 26360f96f..2130bfe6f 100644 --- a/ShareX.HelpersLib/Cryptographic/Translator.cs +++ b/ShareX.HelpersLib/Cryptographic/Translator.cs @@ -68,6 +68,9 @@ public string ASCIIText // http://en.wikipedia.org/wiki/Base64 public string Base64 { get; private set; } + // https://en.wikipedia.org/wiki/Cyclic_redundancy_check + public string CRC32 { get; private set; } + // http://en.wikipedia.org/wiki/MD5 public string MD5 { get; private set; } @@ -84,20 +87,27 @@ public string ASCIIText public bool EncodeText(string text) { - if (!string.IsNullOrEmpty(text)) + try + { + if (!string.IsNullOrEmpty(text)) + { + Text = text; + Binary = TranslatorHelper.TextToBinary(text); + Hexadecimal = TranslatorHelper.TextToHexadecimal(text); + ASCII = TranslatorHelper.TextToASCII(text); + Base64 = TranslatorHelper.TextToBase64(text); + CRC32 = TranslatorHelper.TextToHash(text, HashType.CRC32, true); + MD5 = TranslatorHelper.TextToHash(text, HashType.MD5, true); + SHA1 = TranslatorHelper.TextToHash(text, HashType.SHA1, true); + SHA256 = TranslatorHelper.TextToHash(text, HashType.SHA256, true); + SHA384 = TranslatorHelper.TextToHash(text, HashType.SHA384, true); + SHA512 = TranslatorHelper.TextToHash(text, HashType.SHA512, true); + RIPEMD160 = TranslatorHelper.TextToHash(text, HashType.RIPEMD160, true); + return true; + } + } + catch { - Text = text; - Binary = TranslatorHelper.TextToBinary(text); - Hexadecimal = TranslatorHelper.TextToHexadecimal(text); - ASCII = TranslatorHelper.TextToASCII(text); - Base64 = TranslatorHelper.TextToBase64(text); - MD5 = TranslatorHelper.TextToHash(text, HashType.MD5, true); - SHA1 = TranslatorHelper.TextToHash(text, HashType.SHA1, true); - SHA256 = TranslatorHelper.TextToHash(text, HashType.SHA256, true); - SHA384 = TranslatorHelper.TextToHash(text, HashType.SHA384, true); - SHA512 = TranslatorHelper.TextToHash(text, HashType.SHA512, true); - RIPEMD160 = TranslatorHelper.TextToHash(text, HashType.RIPEMD160, true); - return true; } return false; @@ -105,26 +115,58 @@ public bool EncodeText(string text) public bool DecodeBinary(string binary) { - string result = TranslatorHelper.BinaryToText(binary); - return EncodeText(result); + try + { + string result = TranslatorHelper.BinaryToText(binary); + return EncodeText(result); + } + catch + { + } + + return false; } public bool DecodeHex(string hex) { - string result = TranslatorHelper.HexadecimalToText(hex); - return EncodeText(result); + try + { + string result = TranslatorHelper.HexadecimalToText(hex); + return EncodeText(result); + } + catch + { + } + + return false; } public bool DecodeASCII(string ascii) { - string result = TranslatorHelper.ASCIIToText(ascii); - return EncodeText(result); + try + { + string result = TranslatorHelper.ASCIIToText(ascii); + return EncodeText(result); + } + catch + { + } + + return false; } public bool DecodeBase64(string base64) { - string result = TranslatorHelper.Base64ToText(base64); - return EncodeText(result); + try + { + string result = TranslatorHelper.Base64ToText(base64); + return EncodeText(result); + } + catch + { + } + + return false; } public static bool Test() @@ -147,34 +189,24 @@ public static bool Test() public string HashToString() { StringBuilder sb = new StringBuilder(); - sb.Append("MD5: "); - sb.AppendLine(MD5); - sb.Append("SHA1: "); - sb.AppendLine(SHA1); - sb.Append("SHA256: "); - sb.AppendLine(SHA256); - sb.Append("SHA384: "); - sb.AppendLine(SHA384); - sb.Append("SHA512: "); - sb.AppendLine(SHA512); - sb.Append("RIPEMD160: "); - sb.Append(RIPEMD160); + sb.AppendLine($"CRC-32: {CRC32}"); + sb.AppendLine($"MD5: {MD5}"); + sb.AppendLine($"SHA-1: {SHA1}"); + sb.AppendLine($"SHA-256: {SHA256}"); + sb.AppendLine($"SHA-384: {SHA384}"); + sb.AppendLine($"SHA-512: {SHA512}"); + sb.Append($"RIPEMD-160: {RIPEMD160}"); return sb.ToString(); } public override string ToString() { StringBuilder sb = new StringBuilder(); - sb.Append("Text: "); - sb.AppendLine(Text); - sb.Append("Binary: "); - sb.AppendLine(BinaryText); - sb.Append("Hex: "); - sb.AppendLine(HexadecimalText); - sb.Append("ASCII: "); - sb.AppendLine(ASCIIText); - sb.Append("Base64: "); - sb.AppendLine(Base64); + sb.AppendLine($"Text: {Text}"); + sb.AppendLine($"Binary: {BinaryText}"); + sb.AppendLine($"Hex: {HexadecimalText}"); + sb.AppendLine($"ASCII: {ASCIIText}"); + sb.AppendLine($"Base64: {Base64}"); sb.Append(HashToString()); return sb.ToString(); } diff --git a/ShareX.HelpersLib/Forms/HashCheckForm.Designer.cs b/ShareX.HelpersLib/Forms/HashCheckForm.Designer.cs index 9f5d847f5..f7e302f7f 100644 --- a/ShareX.HelpersLib/Forms/HashCheckForm.Designer.cs +++ b/ShareX.HelpersLib/Forms/HashCheckForm.Designer.cs @@ -32,7 +32,6 @@ private void InitializeComponent() this.txtFilePath = new System.Windows.Forms.TextBox(); this.btnFilePathBrowse = new System.Windows.Forms.Button(); this.lblHashType = new System.Windows.Forms.Label(); - this.lblProgress = new System.Windows.Forms.Label(); this.lblResult = new System.Windows.Forms.Label(); this.lblTarget = new System.Windows.Forms.Label(); this.lblProgressPercentage = new System.Windows.Forms.Label(); @@ -42,6 +41,30 @@ private void InitializeComponent() this.txtResult = new System.Windows.Forms.TextBox(); this.txtTarget = new System.Windows.Forms.TextBox(); this.lblFile = new System.Windows.Forms.Label(); + this.tcMain = new System.Windows.Forms.TabControl(); + this.tpFileHashCheck = new System.Windows.Forms.TabPage(); + this.tpTextConversions = new System.Windows.Forms.TabPage(); + this.btnHashCheckCopyAll = new System.Windows.Forms.Button(); + this.txtHashCheckHash = new System.Windows.Forms.TextBox(); + this.lblHashCheckHash = new System.Windows.Forms.Label(); + this.btnHashCheckDecodeBase64 = new System.Windows.Forms.Button(); + this.txtHashCheckBase64 = new System.Windows.Forms.TextBox(); + this.lblHashCheckBase64 = new System.Windows.Forms.Label(); + this.btnHashCheckDecodeASCII = new System.Windows.Forms.Button(); + this.txtHashCheckASCII = new System.Windows.Forms.TextBox(); + this.lblHashCheckASCII = new System.Windows.Forms.Label(); + this.btnHashCheckDecodeHex = new System.Windows.Forms.Button(); + this.txtHashCheckHex = new System.Windows.Forms.TextBox(); + this.lblHashCheckHex = new System.Windows.Forms.Label(); + this.btnHashCheckDecodeBinary = new System.Windows.Forms.Button(); + this.txtHashCheckBinary = new System.Windows.Forms.TextBox(); + this.lblHashCheckBinary = new System.Windows.Forms.Label(); + this.btnHashCheckEncodeText = new System.Windows.Forms.Button(); + this.txtHashCheckText = new System.Windows.Forms.TextBox(); + this.lblHashCheckText = new System.Windows.Forms.Label(); + this.tcMain.SuspendLayout(); + this.tpFileHashCheck.SuspendLayout(); + this.tpTextConversions.SuspendLayout(); this.SuspendLayout(); // // txtFilePath @@ -65,11 +88,6 @@ private void InitializeComponent() resources.ApplyResources(this.lblHashType, "lblHashType"); this.lblHashType.Name = "lblHashType"; // - // lblProgress - // - resources.ApplyResources(this.lblProgress, "lblProgress"); - this.lblProgress.Name = "lblProgress"; - // // lblResult // resources.ApplyResources(this.lblResult, "lblResult"); @@ -124,29 +142,173 @@ private void InitializeComponent() resources.ApplyResources(this.lblFile, "lblFile"); this.lblFile.Name = "lblFile"; // + // tcMain + // + this.tcMain.Controls.Add(this.tpFileHashCheck); + this.tcMain.Controls.Add(this.tpTextConversions); + resources.ApplyResources(this.tcMain, "tcMain"); + this.tcMain.Name = "tcMain"; + this.tcMain.SelectedIndex = 0; + // + // tpFileHashCheck + // + this.tpFileHashCheck.Controls.Add(this.lblFile); + this.tpFileHashCheck.Controls.Add(this.txtFilePath); + this.tpFileHashCheck.Controls.Add(this.txtTarget); + this.tpFileHashCheck.Controls.Add(this.btnFilePathBrowse); + this.tpFileHashCheck.Controls.Add(this.txtResult); + this.tpFileHashCheck.Controls.Add(this.lblHashType); + this.tpFileHashCheck.Controls.Add(this.pbProgress); + this.tpFileHashCheck.Controls.Add(this.cbHashType); + this.tpFileHashCheck.Controls.Add(this.lblResult); + this.tpFileHashCheck.Controls.Add(this.btnStartHashCheck); + this.tpFileHashCheck.Controls.Add(this.lblTarget); + this.tpFileHashCheck.Controls.Add(this.lblProgressPercentage); + resources.ApplyResources(this.tpFileHashCheck, "tpFileHashCheck"); + this.tpFileHashCheck.Name = "tpFileHashCheck"; + this.tpFileHashCheck.UseVisualStyleBackColor = true; + // + // tpTextConversions + // + this.tpTextConversions.Controls.Add(this.btnHashCheckCopyAll); + this.tpTextConversions.Controls.Add(this.txtHashCheckHash); + this.tpTextConversions.Controls.Add(this.lblHashCheckHash); + this.tpTextConversions.Controls.Add(this.btnHashCheckDecodeBase64); + this.tpTextConversions.Controls.Add(this.txtHashCheckBase64); + this.tpTextConversions.Controls.Add(this.lblHashCheckBase64); + this.tpTextConversions.Controls.Add(this.btnHashCheckDecodeASCII); + this.tpTextConversions.Controls.Add(this.txtHashCheckASCII); + this.tpTextConversions.Controls.Add(this.lblHashCheckASCII); + this.tpTextConversions.Controls.Add(this.btnHashCheckDecodeHex); + this.tpTextConversions.Controls.Add(this.txtHashCheckHex); + this.tpTextConversions.Controls.Add(this.lblHashCheckHex); + this.tpTextConversions.Controls.Add(this.btnHashCheckDecodeBinary); + this.tpTextConversions.Controls.Add(this.txtHashCheckBinary); + this.tpTextConversions.Controls.Add(this.lblHashCheckBinary); + this.tpTextConversions.Controls.Add(this.btnHashCheckEncodeText); + this.tpTextConversions.Controls.Add(this.txtHashCheckText); + this.tpTextConversions.Controls.Add(this.lblHashCheckText); + resources.ApplyResources(this.tpTextConversions, "tpTextConversions"); + this.tpTextConversions.Name = "tpTextConversions"; + this.tpTextConversions.UseVisualStyleBackColor = true; + // + // btnHashCheckCopyAll + // + resources.ApplyResources(this.btnHashCheckCopyAll, "btnHashCheckCopyAll"); + this.btnHashCheckCopyAll.Name = "btnHashCheckCopyAll"; + this.btnHashCheckCopyAll.UseVisualStyleBackColor = true; + this.btnHashCheckCopyAll.Click += new System.EventHandler(this.btnHashCheckCopyAll_Click); + // + // txtHashCheckHash + // + resources.ApplyResources(this.txtHashCheckHash, "txtHashCheckHash"); + this.txtHashCheckHash.Name = "txtHashCheckHash"; + // + // lblHashCheckHash + // + resources.ApplyResources(this.lblHashCheckHash, "lblHashCheckHash"); + this.lblHashCheckHash.Name = "lblHashCheckHash"; + // + // btnHashCheckDecodeBase64 + // + resources.ApplyResources(this.btnHashCheckDecodeBase64, "btnHashCheckDecodeBase64"); + this.btnHashCheckDecodeBase64.Name = "btnHashCheckDecodeBase64"; + this.btnHashCheckDecodeBase64.UseVisualStyleBackColor = true; + this.btnHashCheckDecodeBase64.Click += new System.EventHandler(this.btnHashCheckDecodeBase64_Click); + // + // txtHashCheckBase64 + // + resources.ApplyResources(this.txtHashCheckBase64, "txtHashCheckBase64"); + this.txtHashCheckBase64.Name = "txtHashCheckBase64"; + // + // lblHashCheckBase64 + // + resources.ApplyResources(this.lblHashCheckBase64, "lblHashCheckBase64"); + this.lblHashCheckBase64.Name = "lblHashCheckBase64"; + // + // btnHashCheckDecodeASCII + // + resources.ApplyResources(this.btnHashCheckDecodeASCII, "btnHashCheckDecodeASCII"); + this.btnHashCheckDecodeASCII.Name = "btnHashCheckDecodeASCII"; + this.btnHashCheckDecodeASCII.UseVisualStyleBackColor = true; + this.btnHashCheckDecodeASCII.Click += new System.EventHandler(this.btnHashCheckDecodeASCII_Click); + // + // txtHashCheckASCII + // + resources.ApplyResources(this.txtHashCheckASCII, "txtHashCheckASCII"); + this.txtHashCheckASCII.Name = "txtHashCheckASCII"; + // + // lblHashCheckASCII + // + resources.ApplyResources(this.lblHashCheckASCII, "lblHashCheckASCII"); + this.lblHashCheckASCII.Name = "lblHashCheckASCII"; + // + // btnHashCheckDecodeHex + // + resources.ApplyResources(this.btnHashCheckDecodeHex, "btnHashCheckDecodeHex"); + this.btnHashCheckDecodeHex.Name = "btnHashCheckDecodeHex"; + this.btnHashCheckDecodeHex.UseVisualStyleBackColor = true; + this.btnHashCheckDecodeHex.Click += new System.EventHandler(this.btnHashCheckDecodeHex_Click); + // + // txtHashCheckHex + // + resources.ApplyResources(this.txtHashCheckHex, "txtHashCheckHex"); + this.txtHashCheckHex.Name = "txtHashCheckHex"; + // + // lblHashCheckHex + // + resources.ApplyResources(this.lblHashCheckHex, "lblHashCheckHex"); + this.lblHashCheckHex.Name = "lblHashCheckHex"; + // + // btnHashCheckDecodeBinary + // + resources.ApplyResources(this.btnHashCheckDecodeBinary, "btnHashCheckDecodeBinary"); + this.btnHashCheckDecodeBinary.Name = "btnHashCheckDecodeBinary"; + this.btnHashCheckDecodeBinary.UseVisualStyleBackColor = true; + this.btnHashCheckDecodeBinary.Click += new System.EventHandler(this.btnHashCheckDecodeBinary_Click); + // + // txtHashCheckBinary + // + resources.ApplyResources(this.txtHashCheckBinary, "txtHashCheckBinary"); + this.txtHashCheckBinary.Name = "txtHashCheckBinary"; + // + // lblHashCheckBinary + // + resources.ApplyResources(this.lblHashCheckBinary, "lblHashCheckBinary"); + this.lblHashCheckBinary.Name = "lblHashCheckBinary"; + // + // btnHashCheckEncodeText + // + resources.ApplyResources(this.btnHashCheckEncodeText, "btnHashCheckEncodeText"); + this.btnHashCheckEncodeText.Name = "btnHashCheckEncodeText"; + this.btnHashCheckEncodeText.UseVisualStyleBackColor = true; + this.btnHashCheckEncodeText.Click += new System.EventHandler(this.btnHashCheckEncodeText_Click); + // + // txtHashCheckText + // + resources.ApplyResources(this.txtHashCheckText, "txtHashCheckText"); + this.txtHashCheckText.Name = "txtHashCheckText"; + // + // lblHashCheckText + // + resources.ApplyResources(this.lblHashCheckText, "lblHashCheckText"); + this.lblHashCheckText.Name = "lblHashCheckText"; + // // HashCheckForm // resources.ApplyResources(this, "$this"); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.Color.WhiteSmoke; - this.Controls.Add(this.lblFile); - this.Controls.Add(this.txtTarget); - this.Controls.Add(this.txtResult); - this.Controls.Add(this.pbProgress); - this.Controls.Add(this.cbHashType); - this.Controls.Add(this.btnStartHashCheck); - this.Controls.Add(this.lblProgressPercentage); - this.Controls.Add(this.lblTarget); - this.Controls.Add(this.lblResult); - this.Controls.Add(this.lblProgress); - this.Controls.Add(this.lblHashType); - this.Controls.Add(this.btnFilePathBrowse); - this.Controls.Add(this.txtFilePath); + this.Controls.Add(this.tcMain); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.Name = "HashCheckForm"; + this.tcMain.ResumeLayout(false); + this.tpFileHashCheck.ResumeLayout(false); + this.tpFileHashCheck.PerformLayout(); + this.tpTextConversions.ResumeLayout(false); + this.tpTextConversions.PerformLayout(); this.ResumeLayout(false); - this.PerformLayout(); } @@ -155,7 +317,6 @@ private void InitializeComponent() private System.Windows.Forms.TextBox txtFilePath; private System.Windows.Forms.Button btnFilePathBrowse; private System.Windows.Forms.Label lblHashType; - private System.Windows.Forms.Label lblProgress; private System.Windows.Forms.Label lblResult; private System.Windows.Forms.Label lblTarget; private System.Windows.Forms.Label lblProgressPercentage; @@ -165,5 +326,26 @@ private void InitializeComponent() private System.Windows.Forms.TextBox txtResult; private System.Windows.Forms.TextBox txtTarget; private System.Windows.Forms.Label lblFile; + private System.Windows.Forms.TabControl tcMain; + private System.Windows.Forms.TabPage tpFileHashCheck; + private System.Windows.Forms.TabPage tpTextConversions; + private System.Windows.Forms.Button btnHashCheckEncodeText; + private System.Windows.Forms.TextBox txtHashCheckText; + private System.Windows.Forms.Label lblHashCheckText; + private System.Windows.Forms.Button btnHashCheckCopyAll; + private System.Windows.Forms.TextBox txtHashCheckHash; + private System.Windows.Forms.Label lblHashCheckHash; + private System.Windows.Forms.Button btnHashCheckDecodeBase64; + private System.Windows.Forms.TextBox txtHashCheckBase64; + private System.Windows.Forms.Label lblHashCheckBase64; + private System.Windows.Forms.Button btnHashCheckDecodeASCII; + private System.Windows.Forms.TextBox txtHashCheckASCII; + private System.Windows.Forms.Label lblHashCheckASCII; + private System.Windows.Forms.Button btnHashCheckDecodeHex; + private System.Windows.Forms.TextBox txtHashCheckHex; + private System.Windows.Forms.Label lblHashCheckHex; + private System.Windows.Forms.Button btnHashCheckDecodeBinary; + private System.Windows.Forms.TextBox txtHashCheckBinary; + private System.Windows.Forms.Label lblHashCheckBinary; } } \ No newline at end of file diff --git a/ShareX.HelpersLib/Forms/HashCheckForm.cs b/ShareX.HelpersLib/Forms/HashCheckForm.cs index 10ececb23..ddc706cc0 100644 --- a/ShareX.HelpersLib/Forms/HashCheckForm.cs +++ b/ShareX.HelpersLib/Forms/HashCheckForm.cs @@ -33,6 +33,7 @@ namespace ShareX.HelpersLib public partial class HashCheckForm : BaseForm { private HashCheck hashCheck; + private Translator translator; public HashCheckForm() { @@ -44,8 +45,16 @@ public HashCheckForm() hashCheck = new HashCheck(); hashCheck.FileCheckProgressChanged += fileCheck_FileCheckProgressChanged; 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 + private void btnFilePathBrowse_Click(object sender, EventArgs e) { Helpers.BrowseFile("ShareX - " + Resources.HashCheckForm_btnFilePathBrowse_Click_Choose_file_path, txtFilePath); @@ -72,7 +81,7 @@ private void btnStartHashCheck_Click(object sender, EventArgs e) private void fileCheck_FileCheckProgressChanged(float progress) { pbProgress.Value = (int)progress; - lblProgressPercentage.Text = progress.ToString("0.##") + "%"; + lblProgressPercentage.Text = (int)progress + "%"; } private void fileCheck_FileCheckCompleted(string result, bool cancelled) @@ -128,5 +137,77 @@ private void txtFilePath_DragDrop(object sender, DragEventArgs e) } } } + + #endregion File hash check + + #region Text conversions + + private void FillConversionInfo() + { + 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(); + } + } + + private void btnHashCheckCopyAll_Click(object sender, EventArgs e) + { + if (translator != null && !string.IsNullOrEmpty(translator.Text)) + { + string text = translator.ToString(); + + if (!string.IsNullOrEmpty(text)) + { + ClipboardHelpers.CopyText(text); + } + } + } + + private void btnHashCheckEncodeText_Click(object sender, EventArgs e) + { + if (translator.EncodeText(txtHashCheckText.Text)) + { + FillConversionInfo(); + } + } + + private void btnHashCheckDecodeBinary_Click(object sender, EventArgs e) + { + if (translator.DecodeBinary(txtHashCheckBinary.Text)) + { + FillConversionInfo(); + } + } + + private void btnHashCheckDecodeHex_Click(object sender, EventArgs e) + { + if (translator.DecodeHex(txtHashCheckHex.Text)) + { + FillConversionInfo(); + } + } + + private void btnHashCheckDecodeASCII_Click(object sender, EventArgs e) + { + if (translator.DecodeASCII(txtHashCheckASCII.Text)) + { + FillConversionInfo(); + } + } + + private void btnHashCheckDecodeBase64_Click(object sender, EventArgs e) + { + if (translator.DecodeBase64(txtHashCheckBase64.Text)) + { + FillConversionInfo(); + } + } + + #endregion Text conversions } } \ No newline at end of file diff --git a/ShareX.HelpersLib/Forms/HashCheckForm.resx b/ShareX.HelpersLib/Forms/HashCheckForm.resx index d48cb6aba..a9dbaa78d 100644 --- a/ShareX.HelpersLib/Forms/HashCheckForm.resx +++ b/ShareX.HelpersLib/Forms/HashCheckForm.resx @@ -1,3 +1,4 @@ +