123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- using Microsoft.AspNet.Identity;
- using System;
- using System.CodeDom.Compiler;
- using System.IO;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Web;
- using System.Windows.Forms;
- namespace MiscToolSet
- {
- public partial class MainForm : Form
- {
- private readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create();
- private readonly PasswordHasher _passwordHasher = new PasswordHasher();
- private readonly MD5 _md5 = new MD5Cng();
- private readonly Encoding _asciiEncoding = Encoding.ASCII;
- [STAThread]
- private static void Main()
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new MainForm());
- }
- public MainForm()
- {
- InitializeComponent();
- }
- private void MainForm_Shown(object sender, EventArgs e)
- {
- EncodingComboBox.ValueMember = "CodePage";
- EncodingComboBox.DisplayMember = "DisplayName";
- EncodingComboBox.DataSource = Encoding.GetEncodings();
- EncodingComboBox.ValueMember = "CodePage";
- EncodingComboBox.DisplayMember = "DisplayName";
- EncodingComboBox.SelectedValue = Encoding.Default.CodePage;
- DropToDataUriTextBox.AllowDrop = true;
- DropToDataUriTextBox.DragEnter += DropToDataUriTextBox_DragEnter;
- DropToDataUriTextBox.DragDrop += DropToDataUriTextBox_DragDrop;
- }
- private void Md5HasherUpperCaseCheckBox_CheckedChanged(object sender, EventArgs e)
- {
- MD5HasherInputTextBox_TextChanged(null, null);
- }
- private void MD5HasherInputTextBox_TextChanged(object sender, EventArgs e)
- {
- var fmt = Md5HasherUpperCaseCheckBox.Checked ? "X2" : "x2";
- Md5HasherOutputTextBox.Text = string.Join("", _md5.ComputeHash(Encoding.UTF8.GetBytes(MD5HasherInputTextBox.Text)).Select(p => p.ToString(fmt)));
- }
- private void txtToPasswordHash_TextChanged(object sender, System.EventArgs e)
- {
- PasswordHasherOutputTextBox.Text = _passwordHasher.HashPassword(PasswordHasherInputTextBox.Text);
- }
- private void txtResultOfPasswordHash_MouseDoubleClick(object sender, MouseEventArgs e)
- {
- PasswordHasherOutputTextBox.SelectAll();
- }
- private void btnNewGuid_Click(object sender, EventArgs e)
- {
- var g = Guid.NewGuid();
- GuidGeneratorResultDTextBox.Text = g.ToString("D").ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper());
- GuidGeneratorResultNTextBox.Text = g.ToString("N").ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper());
- GuidGeneratorResultAttributeTextBox.Text = $"[Guid(\"{g.ToString("D").ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper()) }\")]";
- GuidGeneratorResultNewTextBox.Text = $"new Guid(\"{g.ToString("D").ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper())}\")";
- }
- private void btnCpN_Click(object sender, EventArgs e) => Clipboard.SetText(GuidGeneratorResultNTextBox.Text);
- private void btnCpD_Click(object sender, EventArgs e) => Clipboard.SetText(GuidGeneratorResultDTextBox.Text);
- private void btnCpA_Click(object sender, EventArgs e) => Clipboard.SetText(GuidGeneratorResultAttributeTextBox.Text);
- private void GuidGeneratorCopyNewButton_Click(object sender, EventArgs e) => Clipboard.SetText(GuidGeneratorResultNewTextBox.Text);
- private void NewMachineKeyButton_Click(object sender, EventArgs e)
- {
- var rng = new RNGCryptoServiceProvider();
- string CreateKey(int numBytes)
- {
- var buff = new byte[numBytes];
- rng.GetBytes(buff);
- return string.Join("", buff.Select(p => $"{p:X2}"));
- }
- MachineKeyResult.Text =
- $"<machineKey" +
- $" validationKey=\"{CreateKey(64)}\"" +
- $" decryptionKey=\"{CreateKey(24)}\"" +
- $" decryption=\"3DES\"" +
- $" validation=\"3DES\" />";
- }
- private void CopyMachineKeyButton_Click(object sender, EventArgs e)
- {
- Clipboard.SetText(MachineKeyResult.Text);
- }
- private void PasswordGenerateButton_Click(object sender, EventArgs e)
- {
- int GetIntRandomly()
- {
- var buf = new byte[4];
- _rng.GetBytes(buf);
- return BitConverter.ToInt32(buf, 0);
- }
- var stringBuilder = new StringBuilder();
- if (PasswordGenerateIncludeUpperCaseCheckBox.Checked) stringBuilder.Append(Enumerable.Range('A', 'Z' - 'A' + 1).Select(p => (char)p).ToArray());
- if (PasswordGenerateIncludeLowerCaseCheckBox.Checked) stringBuilder.Append(Enumerable.Range('a', 'z' - 'a' + 1).Select(p => (char)p).ToArray());
- if (PasswordGenerateIncludeNumberCheckBox.Checked) stringBuilder.Append(Enumerable.Range('0', '9' - '0' + 1).Select(p => (char)p).ToArray());
- if (PasswordGenerateIncludeSymbolCheckBox.Checked) stringBuilder.Append("!@#$%&*");
- var charPool = stringBuilder.ToString();
- string GetRandomString()
- {
- return new string(
- Enumerable.Range(0, (int)PasswordGenerateLengthUpDownBox.Value)
- .Select(p => charPool[Math.Abs(GetIntRandomly() % charPool.Length)])
- .ToArray()
- );
- }
- string HashString(string str)
- {
- if (PasswordGenerateUseMd5RadioButton.Checked)
- {
- var fmt = PasswordGeneratorHashUseUpperCaseCheckBox.Checked ? "X2" : "x2";
- return string.Join("", _md5.ComputeHash(_asciiEncoding.GetBytes(str)).Select(p => p.ToString(fmt)));
- }
- if (PasswordGenerateUsePasswordHasherRadioButton.Checked)
- {
- return _passwordHasher.HashPassword(str);
- }
- throw new NotImplementedException();
- }
- PasswordGenerateResultRichTextBox.Clear();
- for (var i = 0; i < PasswordGenerateNumberUpDownBox.Value; i++)
- {
- var str = GetRandomString();
- PasswordGenerateResultRichTextBox.AppendText($"{str} {HashString(str)}{Environment.NewLine}");
- }
- }
- private void DropToDataUriTextBox_DragEnter(object sender, DragEventArgs e)
- {
- e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop)
- ? DragDropEffects.Copy
- : DragDropEffects.None;
- }
- private void DropToDataUriTextBox_DragDrop(object sender, DragEventArgs e)
- {
- var files = (string[])e.Data.GetData(DataFormats.FileDrop);
- DropToBase64FileNameLabel.Text = Path.GetFileName(files[0]);
- var bytes = File.ReadAllBytes(files[0]);
- var base64String = Convert.ToBase64String(bytes, Base64FormattingOptions.None);
- DropToDataUriTextBox.Text = DataUrlCheckBox.Checked
- ? $"data:{DataUrlMimeTypeTextBox.Text};base64,{base64String}"
- : base64String;
- }
- private void PasteToDataUri_Click(object sender, EventArgs e)
- {
- var bytes = Encoding.UTF8.GetBytes(Clipboard.GetText());
- var base64String = Convert.ToBase64String(bytes, Base64FormattingOptions.None);
- DropToDataUriTextBox.Text = DataUrlCheckBox.Checked
- ? $"data:{DataUrlMimeTypeTextBox.Text};base64,{base64String}"
- : base64String;
- }
- private void CopyDataUriButton_Click(object sender, EventArgs e)
- {
- Clipboard.SetText(DropToDataUriTextBox.Text);
- }
- private void CopyBase64Button_Click(object sender, EventArgs e)
- {
- Clipboard.SetText(DropToDataUriTextBox.Text);
- }
- private void BulkNewGuidButton_Click(object sender, EventArgs e)
- {
- var length = ((int)BulkNewGuidUpDown.Value);
- for (var i = 0; i < length; i++)
- {
- var guid = Guid.NewGuid();
- if (NormalBulkNewGuidRadioButton.Checked) BulkNewGuidTextBox.AppendText($"{guid:D}{Environment.NewLine}".ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper()));
- if (CtorBulkNewGuidRadioButton.Checked) BulkNewGuidTextBox.AppendText($"new Guid(\"{$"{guid:D}".ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper())}\");{Environment.NewLine}");
- if (AttrBulkNewGuidRadioButton.Checked) BulkNewGuidTextBox.AppendText($"[Guid(\"{$"{guid:D}".ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper())}\"]{Environment.NewLine}");
- if (NumberBulkNewGuidRadioButton.Checked) BulkNewGuidTextBox.AppendText($"{guid:N}{Environment.NewLine}".ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper()));
- }
- }
- private void MagicHuntingButton_Click(object sender, EventArgs e)
- {
- var magicPattern = new Regex(MagicPatternText.Text, RegexOptions.Compiled);
- var dicCollect = new System.Collections.Generic.Dictionary<string, string>();
- CodeConvertTextBox.Text = magicPattern.Replace(CodeInputTextBox.Text, match =>
- {
- var identity = match.Groups[1].Value;
- if (false == IdentifierChecker.Valid(identity)) return match.Value;
- if (false == dicCollect.ContainsKey(identity)) dicCollect[identity] = match.Result(CollectPatternTextBox.Text);
- return match.Result(ConvertPatternTextBox.Text);
- });
- CodeCollectTextBox.Text =
- "private static class Identities {"
- + Environment.NewLine + "// ReSharper disable InconsistentNaming" + Environment.NewLine;
- CodeCollectTextBox.Text += string.Join(Environment.NewLine, dicCollect.Values.OrderBy(p => p));
- CodeCollectTextBox.Text +=
- Environment.NewLine
- + "// ReSharper restore InconsistentNaming" + Environment.NewLine
- + "}";
- }
- private RSACryptoServiceProvider _rsa;
- private void RsaKeyGenerateButton_Click(object sender, EventArgs e)
- {
- // thanks to dotblogs.com.tw/supershowwei/2015/12/23/160510
- // docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsacryptoserviceprovider.keysize#remarks
- _rsa = new RSACryptoServiceProvider((int)RsaKeySizeUpDown.Value);
- RenderRsaKey();
- }
- private void RenderRsaKey()
- {
- if (RsaKeyGenerateXmlRadioButton.Checked)
- {
- RsaPrivateKeyTextBox.Text = _rsa.ToXmlString(true);
- RsaPublicKeyTextBox.Text = _rsa.ToXmlString(false);
- }
- else
- {
- RsaPrivateKeyTextBox.Text = Convert.ToBase64String(_rsa.ExportCspBlob(true));
- RsaPublicKeyTextBox.Text = Convert.ToBase64String(_rsa.ExportCspBlob(false));
- }
- }
- private void RsaKeyGenerateXmlRadioButton_CheckedChanged(object sender, EventArgs e)
- {
- if(_rsa==null) return;
- RenderRsaKey();
- }
- private void ConvertCode_Click(object sender, EventArgs e)
- {
- //html encoding
- // <br /> append to end of line
- // spaces indent
- ConvertedCodeTextBox.Clear();
- var lines = InputCodeTextBox.Lines;
- for (var index = 0; index < lines.Length; index++)
- {
- var line = lines[index];
- var htmlEncode = HttpUtility.HtmlEncode(line);
- ConvertedCodeTextBox.AppendText($"{htmlEncode.Replace(" ", " ")}");
- if (index < lines.Length - 1)
- {
- ConvertedCodeTextBox.AppendText($"<br />{Environment.NewLine}");
- }
- }
- }
- private void CopyButton_Click(object sender, EventArgs e)
- {
- Clipboard.SetText(ConvertedCodeTextBox.Text);
- }
- private void PasteCodeButton_Click(object sender, EventArgs e)
- {
- InputCodeTextBox.Text = Clipboard.GetText();
- }
- private void PasteRawContentButton_Click(object sender, EventArgs e)
- {
- RawContentTextBox.Text = Clipboard.GetText();
- }
- private void PasteBase64Button_Click(object sender, EventArgs e)
- {
- Base64TextBox.Text = Clipboard.GetText();
- }
- private void CopyRawContentButton_Click(object sender, EventArgs e)
- {
- Clipboard.SetText(RawContentTextBox.Text);
- }
- private void CopyBase64_Click(object sender, EventArgs e)
- {
- Clipboard.SetText(Base64TextBox.Text);
- }
- private void EncodeButton_Click(object sender, EventArgs e)
- {
- var enc = Encoding.GetEncoding((int)EncodingComboBox.SelectedValue);
- var buf = enc.GetBytes(RawContentTextBox.Text);
- var b64 = Convert.ToBase64String(buf, Base64FormattingOptions.None);
- Base64TextBox.Text = b64;
- }
- private void DecodeButton_Click(object sender, EventArgs e)
- {
- var enc = Encoding.GetEncoding((int)EncodingComboBox.SelectedValue);
- var b64 = Base64TextBox.Text;
- var buf = Convert.FromBase64String(b64);
- var raw = enc.GetString(buf);
- RawContentTextBox.Text = raw;
- }
- private void SaveBase64ToFileButton_Click(object sender, EventArgs e)
- {
- var dlg = new SaveFileDialog();
- if (dlg.ShowDialog() == DialogResult.OK)
- {
- var data = Convert.FromBase64String(Base64ToFileTextBox.Text);
- File.WriteAllBytes(dlg.FileName, data);
- }
- }
- }
- internal static class CommonExtensionMetond
- {
- public static T ProcessWhen<T>(this T me, bool when, Func<T, T> process)
- {
- return when ? process(me) : me;
- }
- }
- internal static class IdentifierChecker
- {
- private static readonly CodeDomProvider Provider = CodeDomProvider.CreateProvider("C#");
- public static bool Valid(string word) => Provider.IsValidIdentifier(word);
- };
- }
|