MainForm.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. DropToDataUriTextBox.AllowDrop = true;
  39. DropToDataUriTextBox.DragEnter += DropToDataUriTextBox_DragEnter;
  40. DropToDataUriTextBox.DragDrop += DropToDataUriTextBox_DragDrop;
  41. }
  42. private void Md5HasherUpperCaseCheckBox_CheckedChanged(object sender, EventArgs e)
  43. {
  44. MD5HasherInputTextBox_TextChanged(null, null);
  45. }
  46. private void MD5HasherInputTextBox_TextChanged(object sender, EventArgs e)
  47. {
  48. var fmt = Md5HasherUpperCaseCheckBox.Checked ? "X2" : "x2";
  49. Md5HasherOutputTextBox.Text = string.Join("", _md5.ComputeHash(Encoding.UTF8.GetBytes(MD5HasherInputTextBox.Text)).Select(p => p.ToString(fmt)));
  50. }
  51. private void txtToPasswordHash_TextChanged(object sender, System.EventArgs e)
  52. {
  53. PasswordHasherOutputTextBox.Text = _passwordHasher.HashPassword(PasswordHasherInputTextBox.Text);
  54. }
  55. private void txtResultOfPasswordHash_MouseDoubleClick(object sender, MouseEventArgs e)
  56. {
  57. PasswordHasherOutputTextBox.SelectAll();
  58. }
  59. private void btnNewGuid_Click(object sender, EventArgs e)
  60. {
  61. var g = Guid.NewGuid();
  62. GuidGeneratorResultDTextBox.Text = g.ToString("D").ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper());
  63. GuidGeneratorResultNTextBox.Text = g.ToString("N").ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper());
  64. GuidGeneratorResultAttributeTextBox.Text = $"[Guid(\"{g.ToString("D").ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper()) }\")]";
  65. GuidGeneratorResultNewTextBox.Text = $"new Guid(\"{g.ToString("D").ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper())}\")";
  66. }
  67. private void btnCpN_Click(object sender, EventArgs e) => Clipboard.SetText(GuidGeneratorResultNTextBox.Text);
  68. private void btnCpD_Click(object sender, EventArgs e) => Clipboard.SetText(GuidGeneratorResultDTextBox.Text);
  69. private void btnCpA_Click(object sender, EventArgs e) => Clipboard.SetText(GuidGeneratorResultAttributeTextBox.Text);
  70. private void GuidGeneratorCopyNewButton_Click(object sender, EventArgs e) => Clipboard.SetText(GuidGeneratorResultNewTextBox.Text);
  71. private void NewMachineKeyButton_Click(object sender, EventArgs e)
  72. {
  73. var rng = new RNGCryptoServiceProvider();
  74. string CreateKey(int numBytes)
  75. {
  76. var buff = new byte[numBytes];
  77. rng.GetBytes(buff);
  78. return string.Join("", buff.Select(p => $"{p:X2}"));
  79. }
  80. MachineKeyResult.Text =
  81. $"<machineKey" +
  82. $" validationKey=\"{CreateKey(64)}\"" +
  83. $" decryptionKey=\"{CreateKey(24)}\"" +
  84. $" decryption=\"3DES\"" +
  85. $" validation=\"3DES\" />";
  86. }
  87. private void CopyMachineKeyButton_Click(object sender, EventArgs e)
  88. {
  89. Clipboard.SetText(MachineKeyResult.Text);
  90. }
  91. private void PasswordGenerateButton_Click(object sender, EventArgs e)
  92. {
  93. int GetIntRandomly()
  94. {
  95. var buf = new byte[4];
  96. _rng.GetBytes(buf);
  97. return BitConverter.ToInt32(buf, 0);
  98. }
  99. var stringBuilder = new StringBuilder();
  100. if (PasswordGenerateIncludeUpperCaseCheckBox.Checked) stringBuilder.Append(Enumerable.Range('A', 'Z' - 'A' + 1).Select(p => (char)p).ToArray());
  101. if (PasswordGenerateIncludeLowerCaseCheckBox.Checked) stringBuilder.Append(Enumerable.Range('a', 'z' - 'a' + 1).Select(p => (char)p).ToArray());
  102. if (PasswordGenerateIncludeNumberCheckBox.Checked) stringBuilder.Append(Enumerable.Range('0', '9' - '0' + 1).Select(p => (char)p).ToArray());
  103. if (PasswordGenerateIncludeSymbolCheckBox.Checked) stringBuilder.Append("!@#$%&*");
  104. var charPool = stringBuilder.ToString();
  105. string GetRandomString()
  106. {
  107. return new string(
  108. Enumerable.Range(0, (int)PasswordGenerateLengthUpDownBox.Value)
  109. .Select(p => charPool[Math.Abs(GetIntRandomly() % charPool.Length)])
  110. .ToArray()
  111. );
  112. }
  113. string HashString(string str)
  114. {
  115. if (PasswordGenerateUseMd5RadioButton.Checked)
  116. {
  117. var fmt = PasswordGeneratorHashUseUpperCaseCheckBox.Checked ? "X2" : "x2";
  118. return string.Join("", _md5.ComputeHash(_asciiEncoding.GetBytes(str)).Select(p => p.ToString(fmt)));
  119. }
  120. if (PasswordGenerateUsePasswordHasherRadioButton.Checked)
  121. {
  122. return _passwordHasher.HashPassword(str);
  123. }
  124. throw new NotImplementedException();
  125. }
  126. PasswordGenerateResultRichTextBox.Clear();
  127. for (var i = 0; i < PasswordGenerateNumberUpDownBox.Value; i++)
  128. {
  129. var str = GetRandomString();
  130. PasswordGenerateResultRichTextBox.AppendText($"{str} {HashString(str)}{Environment.NewLine}");
  131. }
  132. }
  133. private void DropToDataUriTextBox_DragEnter(object sender, DragEventArgs e)
  134. {
  135. e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop)
  136. ? DragDropEffects.Copy
  137. : DragDropEffects.None;
  138. }
  139. private void DropToDataUriTextBox_DragDrop(object sender, DragEventArgs e)
  140. {
  141. var files = (string[])e.Data.GetData(DataFormats.FileDrop);
  142. DropToBase64FileNameLabel.Text = Path.GetFileName(files[0]);
  143. var bytes = File.ReadAllBytes(files[0]);
  144. var base64String = Convert.ToBase64String(bytes, Base64FormattingOptions.None);
  145. DropToDataUriTextBox.Text = DataUrlCheckBox.Checked
  146. ? $"data:{DataUrlMimeTypeTextBox.Text};base64,{base64String}"
  147. : base64String;
  148. }
  149. private void PasteToDataUri_Click(object sender, EventArgs e)
  150. {
  151. var bytes = Encoding.UTF8.GetBytes(Clipboard.GetText());
  152. var base64String = Convert.ToBase64String(bytes, Base64FormattingOptions.None);
  153. DropToDataUriTextBox.Text = DataUrlCheckBox.Checked
  154. ? $"data:{DataUrlMimeTypeTextBox.Text};base64,{base64String}"
  155. : base64String;
  156. }
  157. private void CopyDataUriButton_Click(object sender, EventArgs e)
  158. {
  159. Clipboard.SetText(DropToDataUriTextBox.Text);
  160. }
  161. private void CopyBase64Button_Click(object sender, EventArgs e)
  162. {
  163. Clipboard.SetText(DropToDataUriTextBox.Text);
  164. }
  165. private void BulkNewGuidButton_Click(object sender, EventArgs e)
  166. {
  167. var length = ((int)BulkNewGuidUpDown.Value);
  168. for (var i = 0; i < length; i++)
  169. {
  170. var guid = Guid.NewGuid();
  171. if (NormalBulkNewGuidRadioButton.Checked) BulkNewGuidTextBox.AppendText($"{guid:D}{Environment.NewLine}".ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper()));
  172. if (CtorBulkNewGuidRadioButton.Checked) BulkNewGuidTextBox.AppendText($"new Guid(\"{$"{guid:D}".ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper())}\");{Environment.NewLine}");
  173. if (AttrBulkNewGuidRadioButton.Checked) BulkNewGuidTextBox.AppendText($"[Guid(\"{$"{guid:D}".ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper())}\"]{Environment.NewLine}");
  174. if (NumberBulkNewGuidRadioButton.Checked) BulkNewGuidTextBox.AppendText($"{guid:N}{Environment.NewLine}".ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper()));
  175. }
  176. }
  177. private void MagicHuntingButton_Click(object sender, EventArgs e)
  178. {
  179. var magicPattern = new Regex(MagicPatternText.Text, RegexOptions.Compiled);
  180. var dicCollect = new System.Collections.Generic.Dictionary<string, string>();
  181. CodeConvertTextBox.Text = magicPattern.Replace(CodeInputTextBox.Text, match =>
  182. {
  183. var identity = match.Groups[1].Value;
  184. if (false == IdentifierChecker.Valid(identity)) return match.Value;
  185. if (false == dicCollect.ContainsKey(identity)) dicCollect[identity] = match.Result(CollectPatternTextBox.Text);
  186. return match.Result(ConvertPatternTextBox.Text);
  187. });
  188. CodeCollectTextBox.Text =
  189. "private static class Identities {"
  190. + Environment.NewLine + "// ReSharper disable InconsistentNaming" + Environment.NewLine;
  191. CodeCollectTextBox.Text += string.Join(Environment.NewLine, dicCollect.Values.OrderBy(p => p));
  192. CodeCollectTextBox.Text +=
  193. Environment.NewLine
  194. + "// ReSharper restore InconsistentNaming" + Environment.NewLine
  195. + "}";
  196. }
  197. private RSACryptoServiceProvider _rsa;
  198. private void RsaKeyGenerateButton_Click(object sender, EventArgs e)
  199. {
  200. // thanks to dotblogs.com.tw/supershowwei/2015/12/23/160510
  201. // docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsacryptoserviceprovider.keysize#remarks
  202. _rsa = new RSACryptoServiceProvider((int)RsaKeySizeUpDown.Value);
  203. RenderRsaKey();
  204. }
  205. private void RenderRsaKey()
  206. {
  207. if (RsaKeyGenerateXmlRadioButton.Checked)
  208. {
  209. RsaPrivateKeyTextBox.Text = _rsa.ToXmlString(true);
  210. RsaPublicKeyTextBox.Text = _rsa.ToXmlString(false);
  211. }
  212. else
  213. {
  214. RsaPrivateKeyTextBox.Text = Convert.ToBase64String(_rsa.ExportCspBlob(true));
  215. RsaPublicKeyTextBox.Text = Convert.ToBase64String(_rsa.ExportCspBlob(false));
  216. }
  217. }
  218. private void RsaKeyGenerateXmlRadioButton_CheckedChanged(object sender, EventArgs e)
  219. {
  220. if(_rsa==null) return;
  221. RenderRsaKey();
  222. }
  223. private void ConvertCode_Click(object sender, EventArgs e)
  224. {
  225. //html encoding
  226. // <br /> append to end of line
  227. // spaces indent
  228. ConvertedCodeTextBox.Clear();
  229. var lines = InputCodeTextBox.Lines;
  230. for (var index = 0; index < lines.Length; index++)
  231. {
  232. var line = lines[index];
  233. var htmlEncode = HttpUtility.HtmlEncode(line);
  234. ConvertedCodeTextBox.AppendText($"{htmlEncode.Replace(" ", "&nbsp;")}");
  235. if (index < lines.Length - 1)
  236. {
  237. ConvertedCodeTextBox.AppendText($"<br />{Environment.NewLine}");
  238. }
  239. }
  240. }
  241. private void CopyButton_Click(object sender, EventArgs e)
  242. {
  243. Clipboard.SetText(ConvertedCodeTextBox.Text);
  244. }
  245. private void PasteCodeButton_Click(object sender, EventArgs e)
  246. {
  247. InputCodeTextBox.Text = Clipboard.GetText();
  248. }
  249. private void PasteRawContentButton_Click(object sender, EventArgs e)
  250. {
  251. RawContentTextBox.Text = Clipboard.GetText();
  252. }
  253. private void PasteBase64Button_Click(object sender, EventArgs e)
  254. {
  255. Base64TextBox.Text = Clipboard.GetText();
  256. }
  257. private void CopyRawContentButton_Click(object sender, EventArgs e)
  258. {
  259. Clipboard.SetText(RawContentTextBox.Text);
  260. }
  261. private void CopyBase64_Click(object sender, EventArgs e)
  262. {
  263. Clipboard.SetText(Base64TextBox.Text);
  264. }
  265. private void EncodeButton_Click(object sender, EventArgs e)
  266. {
  267. var enc = Encoding.GetEncoding((int)EncodingComboBox.SelectedValue);
  268. var buf = enc.GetBytes(RawContentTextBox.Text);
  269. var b64 = Convert.ToBase64String(buf, Base64FormattingOptions.None);
  270. Base64TextBox.Text = b64;
  271. }
  272. private void DecodeButton_Click(object sender, EventArgs e)
  273. {
  274. var enc = Encoding.GetEncoding((int)EncodingComboBox.SelectedValue);
  275. var b64 = Base64TextBox.Text;
  276. var buf = Convert.FromBase64String(b64);
  277. var raw = enc.GetString(buf);
  278. RawContentTextBox.Text = raw;
  279. }
  280. private void SaveBase64ToFileButton_Click(object sender, EventArgs e)
  281. {
  282. var dlg = new SaveFileDialog();
  283. if (dlg.ShowDialog() == DialogResult.OK)
  284. {
  285. var data = Convert.FromBase64String(Base64ToFileTextBox.Text);
  286. File.WriteAllBytes(dlg.FileName, data);
  287. }
  288. }
  289. }
  290. internal static class CommonExtensionMetond
  291. {
  292. public static T ProcessWhen<T>(this T me, bool when, Func<T, T> process)
  293. {
  294. return when ? process(me) : me;
  295. }
  296. }
  297. internal static class IdentifierChecker
  298. {
  299. private static readonly CodeDomProvider Provider = CodeDomProvider.CreateProvider("C#");
  300. public static bool Valid(string word) => Provider.IsValidIdentifier(word);
  301. };
  302. }