MainForm.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. using System.CodeDom.Compiler;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Windows.Forms;
  9. using Microsoft.AspNet.Identity;
  10. namespace MiscToolSet
  11. {
  12. public partial class MainForm : Form
  13. {
  14. private readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create();
  15. private readonly PasswordHasher _passwordHasher = new PasswordHasher();
  16. private readonly MD5 _md5 = new MD5Cng();
  17. private readonly Encoding _asciiEncoding = Encoding.ASCII;
  18. [STAThread]
  19. private static void Main()
  20. {
  21. Application.EnableVisualStyles();
  22. Application.SetCompatibleTextRenderingDefault(false);
  23. Application.Run(new MainForm());
  24. }
  25. public MainForm()
  26. {
  27. InitializeComponent();
  28. }
  29. private void Md5HasherUpperCaseCheckBox_CheckedChanged(object sender, EventArgs e)
  30. {
  31. MD5HasherInputTextBox_TextChanged(null, null);
  32. }
  33. private void MD5HasherInputTextBox_TextChanged(object sender, EventArgs e)
  34. {
  35. var fmt = Md5HasherUpperCaseCheckBox.Checked ? "X2" : "x2";
  36. Md5HasherOutputTextBox.Text = string.Join("", _md5.ComputeHash(Encoding.UTF8.GetBytes(MD5HasherInputTextBox.Text)).Select(p => p.ToString(fmt)));
  37. }
  38. private void txtToPasswordHash_TextChanged(object sender, System.EventArgs e)
  39. {
  40. PasswordHasherOutputTextBox.Text = _passwordHasher.HashPassword(PasswordHasherInputTextBox.Text);
  41. }
  42. private void txtResultOfPasswordHash_MouseDoubleClick(object sender, MouseEventArgs e)
  43. {
  44. PasswordHasherOutputTextBox.SelectAll();
  45. }
  46. private void btnNewGuid_Click(object sender, EventArgs e)
  47. {
  48. var g = Guid.NewGuid();
  49. GuidGeneratorResultDTextBox.Text = g.ToString("D").ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper());
  50. GuidGeneratorResultNTextBox.Text = g.ToString("N").ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper());
  51. GuidGeneratorResultAttributeTextBox.Text = $"[Guid(\"{g.ToString("D").ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper()) }\")]";
  52. GuidGeneratorResultNewTextBox.Text = $"new Guid(\"{g.ToString("D").ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper())}\")";
  53. }
  54. private void btnCpN_Click(object sender, EventArgs e) => Clipboard.SetText(GuidGeneratorResultNTextBox.Text);
  55. private void btnCpD_Click(object sender, EventArgs e) => Clipboard.SetText(GuidGeneratorResultDTextBox.Text);
  56. private void btnCpA_Click(object sender, EventArgs e) => Clipboard.SetText(GuidGeneratorResultAttributeTextBox.Text);
  57. private void GuidGeneratorCopyNewButton_Click(object sender, EventArgs e) => Clipboard.SetText(GuidGeneratorResultNewTextBox.Text);
  58. private void NewMachineKeyButton_Click(object sender, EventArgs e)
  59. {
  60. var rng = new RNGCryptoServiceProvider();
  61. string CreateKey(int numBytes)
  62. {
  63. var buff = new byte[numBytes];
  64. rng.GetBytes(buff);
  65. return string.Join("", buff.Select(p => $"{p:X2}"));
  66. }
  67. MachineKeyResult.Text =
  68. $"<machineKey" +
  69. $" validationKey=\"{CreateKey(64)}\"" +
  70. $" decryptionKey=\"{CreateKey(24)}\"" +
  71. $" decryption=\"3DES\"" +
  72. $" validation=\"3DES\" />";
  73. }
  74. private void CopyMachineKeyButton_Click(object sender, EventArgs e)
  75. {
  76. Clipboard.SetText(MachineKeyResult.Text);
  77. }
  78. private void PasswordGenerateButton_Click(object sender, EventArgs e)
  79. {
  80. int GetIntRandomly()
  81. {
  82. var buf = new byte[4];
  83. _rng.GetBytes(buf);
  84. return BitConverter.ToInt32(buf, 0);
  85. }
  86. var stringBuilder = new StringBuilder();
  87. if (PasswordGenerateIncludeUpperCaseCheckBox.Checked) stringBuilder.Append(Enumerable.Range('A', 'Z' - 'A' + 1).Select(p => (char)p).ToArray());
  88. if (PasswordGenerateIncludeLowerCaseCheckBox.Checked) stringBuilder.Append(Enumerable.Range('a', 'z' - 'a' + 1).Select(p => (char)p).ToArray());
  89. if (PasswordGenerateIncludeNumberCheckBox.Checked) stringBuilder.Append(Enumerable.Range('0', '9' - '0' + 1).Select(p => (char)p).ToArray());
  90. if (PasswordGenerateIncludeSymbolCheckBox.Checked) stringBuilder.Append("!@#$%&*");
  91. var charPool = stringBuilder.ToString();
  92. string GetRandomString()
  93. {
  94. return new string(
  95. Enumerable.Range(0, (int)PasswordGenerateLengthUpDownBox.Value)
  96. .Select(p => charPool[Math.Abs(GetIntRandomly() % charPool.Length)])
  97. .ToArray()
  98. );
  99. }
  100. string HashString(string str)
  101. {
  102. if (PasswordGenerateUseMd5RadioButton.Checked)
  103. {
  104. var fmt = PasswordGeneratorHashUseUpperCaseCheckBox.Checked ? "X2" : "x2";
  105. return string.Join("", _md5.ComputeHash(_asciiEncoding.GetBytes(str)).Select(p => p.ToString(fmt)));
  106. }
  107. if (PasswordGenerateUsePasswordHasherRadioButton.Checked)
  108. {
  109. return _passwordHasher.HashPassword(str);
  110. }
  111. throw new NotImplementedException();
  112. }
  113. PasswordGenerateResultRichTextBox.Clear();
  114. for (var i = 0; i < PasswordGenerateNumberUpDownBox.Value; i++)
  115. {
  116. var str = GetRandomString();
  117. PasswordGenerateResultRichTextBox.AppendText($"{str} {HashString(str)}{Environment.NewLine}");
  118. }
  119. }
  120. private void DropToBase64Label_DragEnter(object sender, DragEventArgs e)
  121. {
  122. e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop)
  123. ? DragDropEffects.Copy
  124. : DragDropEffects.None;
  125. }
  126. private void DropToBase64Label_DragDrop(object sender, DragEventArgs e)
  127. {
  128. var files = (string[])e.Data.GetData(DataFormats.FileDrop);
  129. DropToBase64FileNameLabel.Text = Path.GetFileName(files[0]);
  130. var bytes = File.ReadAllBytes(files[0]);
  131. var base64String = Convert.ToBase64String(bytes, Base64FormattingOptions.None);
  132. DropToBase64TextBox.Text = DataUrlCheckBox.Checked
  133. ? $"data:{DataUrlMimeTypeTextBox.Text};base64,{base64String}"
  134. : base64String;
  135. }
  136. private void CopyBase64Button_Click(object sender, EventArgs e)
  137. {
  138. Clipboard.SetText(DropToBase64TextBox.Text);
  139. }
  140. private void BulkNewGuidButton_Click(object sender, EventArgs e)
  141. {
  142. var length = ((int)BulkNewGuidUpDown.Value);
  143. for (var i = 0; i < length; i++)
  144. {
  145. var guid = Guid.NewGuid();
  146. if (NormalBulkNewGuidRadioButton.Checked) BulkNewGuidTextBox.AppendText($"{guid:D}{Environment.NewLine}".ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper()));
  147. if (CtorBulkNewGuidRadioButton.Checked) BulkNewGuidTextBox.AppendText($"new Guid(\"{$"{guid:D}".ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper())}\");{Environment.NewLine}");
  148. if (AttrBulkNewGuidRadioButton.Checked) BulkNewGuidTextBox.AppendText($"[Guid(\"{$"{guid:D}".ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper())}\"]{Environment.NewLine}");
  149. if (NumberBulkNewGuidRadioButton.Checked) BulkNewGuidTextBox.AppendText($"{guid:N}{Environment.NewLine}".ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper()));
  150. }
  151. }
  152. private void MagicHuntingButton_Click(object sender, EventArgs e)
  153. {
  154. var magicPattern = new Regex(MagicPatternText.Text, RegexOptions.Compiled);
  155. var dicCollect = new System.Collections.Generic.Dictionary<string, string>();
  156. CodeConvertTextBox.Text = magicPattern.Replace(CodeInputTextBox.Text, match =>
  157. {
  158. var identity = match.Groups[1].Value;
  159. if (false == IdentifierChecker.Valid(identity)) return match.Value;
  160. if (false == dicCollect.ContainsKey(identity)) dicCollect[identity] = match.Result(CollectPatternTextBox.Text);
  161. return match.Result(ConvertPatternTextBox.Text);
  162. });
  163. CodeCollectTextBox.Text =
  164. "private static class Identities {"
  165. + Environment.NewLine + "// ReSharper disable InconsistentNaming" + Environment.NewLine;
  166. CodeCollectTextBox.Text += string.Join(Environment.NewLine, dicCollect.Values.OrderBy(p => p));
  167. CodeCollectTextBox.Text +=
  168. Environment.NewLine
  169. + "// ReSharper restore InconsistentNaming" + Environment.NewLine
  170. + "}";
  171. }
  172. private void RsaKeyGenerateButton_Click(object sender, EventArgs e)
  173. {
  174. // thanks to dotblogs.com.tw/supershowwei/2015/12/23/160510
  175. // docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsacryptoserviceprovider.keysize#remarks
  176. var rsa = new RSACryptoServiceProvider((int)RsaKeySizeUpDown.Value);
  177. RsaPrivateKeyTextBox.Text = rsa.ToXmlString(true);
  178. RsaPublicKeyTextBox.Text = rsa.ToXmlString(false);
  179. }
  180. }
  181. internal static class CommonExtensionMetond
  182. {
  183. public static T ProcessWhen<T>(this T me, bool when, Func<T, T> process)
  184. {
  185. return when ? process(me) : me;
  186. }
  187. }
  188. internal static class IdentifierChecker
  189. {
  190. private static readonly CodeDomProvider Provider = CodeDomProvider.CreateProvider("C#");
  191. public static bool Valid(string word) => Provider.IsValidIdentifier(word);
  192. };
  193. }