ShareX/ShareX.HelpersLib/Forms/HashCheckForm.cs

290 lines
9 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2021-07-29 15:22:51 +12:00
Copyright (c) 2007-2021 ShareX Team
2013-11-03 23:53:49 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
2014-12-11 09:25:20 +13:00
using ShareX.HelpersLib.Properties;
2013-11-03 23:53:49 +13:00
using System;
using System.Drawing;
using System.Windows.Forms;
2014-12-11 09:25:20 +13:00
namespace ShareX.HelpersLib
2013-11-03 23:53:49 +13:00
{
public partial class HashCheckForm : Form
2013-11-03 23:53:49 +13:00
{
2020-09-13 12:45:40 +12:00
public bool CompareTwoFiles { get; private set; }
2013-11-03 23:53:49 +13:00
private HashCheck hashCheck;
private Translator translator;
2013-11-03 23:53:49 +13:00
public HashCheckForm()
{
InitializeComponent();
2019-06-25 06:36:16 +12:00
ShareXResources.ApplyTheme(this);
2013-11-03 23:53:49 +13:00
2020-09-13 12:45:40 +12:00
UpdateCompareControls();
2013-11-03 23:53:49 +13:00
cbHashType.Items.AddRange(Helpers.GetEnumDescriptions<HashType>());
cbHashType.SelectedIndex = (int)HashType.SHA256;
2013-11-03 23:53:49 +13:00
hashCheck = new HashCheck();
hashCheck.FileCheckProgressChanged += fileCheck_FileCheckProgressChanged;
translator = new Translator();
2013-11-03 23:53:49 +13:00
}
#region File hash check
private void UpdateResult()
{
if (!string.IsNullOrEmpty(txtResult.Text) && !string.IsNullOrEmpty(txtTarget.Text))
{
if (txtResult.Text.Equals(txtTarget.Text, StringComparison.InvariantCultureIgnoreCase))
{
txtTarget.BackColor = Color.FromArgb(200, 255, 200);
}
else
{
txtTarget.BackColor = Color.FromArgb(255, 200, 200);
}
2019-06-24 01:59:48 +12:00
txtTarget.ForeColor = SystemColors.ControlText;
}
else
{
2019-06-24 01:59:48 +12:00
txtTarget.BackColor = txtResult.BackColor;
txtTarget.ForeColor = txtResult.ForeColor;
}
}
2020-09-13 12:45:40 +12:00
private void UpdateCompareControls()
{
lblFilePath2.Enabled = txtFilePath2.Enabled = btnFilePathBrowse2.Enabled = CompareTwoFiles;
if (CompareTwoFiles)
{
lblResult.Text = Resources.ResultOfFirstFile;
lblTarget.Text = Resources.ResultOfSecondFile;
2020-09-13 12:45:40 +12:00
}
else
{
lblResult.Text = Resources.Result;
lblTarget.Text = Resources.Target;
2020-09-13 12:45:40 +12:00
}
txtTarget.ReadOnly = CompareTwoFiles;
}
2013-11-03 23:53:49 +13:00
private void btnFilePathBrowse_Click(object sender, EventArgs e)
{
Helpers.BrowseFile(txtFilePath);
2013-11-03 23:53:49 +13:00
}
2020-09-13 12:45:40 +12:00
private void btnFilePathBrowse2_Click(object sender, EventArgs e)
{
Helpers.BrowseFile(txtFilePath2);
}
private void cbCompareTwoFiles_CheckedChanged(object sender, EventArgs e)
{
CompareTwoFiles = cbCompareTwoFiles.Checked;
UpdateCompareControls();
}
2020-09-13 15:04:03 +12:00
private async void btnStartHashCheck_Click(object sender, EventArgs e)
2013-11-03 23:53:49 +13:00
{
if (hashCheck.IsWorking)
{
hashCheck.Stop();
}
else
{
2020-09-13 15:04:03 +12:00
btnStartHashCheck.Text = Resources.Stop;
pbProgress.Value = 0;
txtResult.Text = "";
2020-09-13 15:17:14 +12:00
if (CompareTwoFiles)
{
txtTarget.Text = "";
}
2013-11-03 23:53:49 +13:00
HashType hashType = (HashType)cbHashType.SelectedIndex;
2020-09-13 15:17:14 +12:00
string filePath = txtFilePath.Text;
2020-09-13 15:04:03 +12:00
string result = await hashCheck.Start(filePath, hashType);
2013-11-03 23:53:49 +13:00
2020-09-13 15:17:14 +12:00
if (!string.IsNullOrEmpty(result))
{
txtResult.Text = result.ToUpperInvariant();
if (CompareTwoFiles)
{
string filePath2 = txtFilePath2.Text;
string result2 = await hashCheck.Start(filePath2, hashType);
if (!string.IsNullOrEmpty(result2))
{
txtTarget.Text = result2.ToUpperInvariant();
}
}
}
2020-09-13 15:04:03 +12:00
btnStartHashCheck.Text = Resources.Start;
2013-11-03 23:53:49 +13:00
}
}
private void fileCheck_FileCheckProgressChanged(float progress)
{
pbProgress.Value = (int)progress;
lblProgressPercentage.Text = (int)progress + "%";
2013-11-03 23:53:49 +13:00
}
private void txtResult_TextChanged(object sender, EventArgs e)
{
UpdateResult();
}
private void txtTarget_TextChanged(object sender, EventArgs e)
{
UpdateResult();
2013-11-03 23:53:49 +13:00
}
private void txtResult_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
{
txtResult.SelectAll();
}
}
private void txtTarget_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
{
txtTarget.SelectAll();
}
}
private void tpFileHashCheck_DragEnter(object sender, DragEventArgs e)
2013-11-03 23:53:49 +13:00
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void tpFileHashCheck_DragDrop(object sender, DragEventArgs e)
2013-11-03 23:53:49 +13:00
{
2021-06-10 10:14:01 +12:00
if (e.Data.GetDataPresent(DataFormats.FileDrop, false) && e.Data.GetData(DataFormats.FileDrop, false) is string[] files && files.Length > 0)
2013-11-03 23:53:49 +13:00
{
2021-06-10 10:14:01 +12:00
txtFilePath.Text = files[0];
2013-11-03 23:53:49 +13:00
}
}
#endregion File hash check
#region Text conversions
private void FillConversionInfo()
2016-01-03 02:24:14 +13:00
{
FillConversionInfo(translator.Text);
}
private void FillConversionInfo(string text)
{
if (translator != null)
{
2016-01-03 02:24:14 +13:00
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)
{
2016-01-03 02:24:14 +13:00
if (translator != null)
{
string text = translator.ToString();
if (!string.IsNullOrEmpty(text))
{
ClipboardHelpers.CopyText(text);
}
}
}
private void btnHashCheckEncodeText_Click(object sender, EventArgs e)
{
2016-01-03 02:24:14 +13:00
FillConversionInfo(txtHashCheckText.Text);
}
private void btnHashCheckDecodeBinary_Click(object sender, EventArgs e)
{
2016-01-03 02:24:14 +13:00
string binary = txtHashCheckBinary.Text;
translator.DecodeBinary(binary);
FillConversionInfo();
txtHashCheckBinary.Text = binary;
}
private void btnHashCheckDecodeHex_Click(object sender, EventArgs e)
{
2016-01-03 02:24:14 +13:00
string hex = txtHashCheckHex.Text;
translator.DecodeHex(hex);
FillConversionInfo();
txtHashCheckHex.Text = hex;
}
private void btnHashCheckDecodeASCII_Click(object sender, EventArgs e)
{
2016-01-03 02:24:14 +13:00
string ascii = txtHashCheckASCII.Text;
translator.DecodeASCII(ascii);
FillConversionInfo();
txtHashCheckASCII.Text = ascii;
}
private void btnHashCheckDecodeBase64_Click(object sender, EventArgs e)
{
2016-01-03 02:24:14 +13:00
string base64 = txtHashCheckBase64.Text;
translator.DecodeBase64(base64);
FillConversionInfo();
txtHashCheckBase64.Text = base64;
}
#endregion Text conversions
2013-11-03 23:53:49 +13:00
}
}