MainForm.cs 13 KB

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