MainForm.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 Md5HasherUpperCaseCheckBox_CheckedChanged(object sender, EventArgs e)
  31. {
  32. MD5HasherInputTextBox_TextChanged(null, null);
  33. }
  34. private void MD5HasherInputTextBox_TextChanged(object sender, EventArgs e)
  35. {
  36. var fmt = Md5HasherUpperCaseCheckBox.Checked ? "X2" : "x2";
  37. Md5HasherOutputTextBox.Text = string.Join("", _md5.ComputeHash(Encoding.UTF8.GetBytes(MD5HasherInputTextBox.Text)).Select(p => p.ToString(fmt)));
  38. }
  39. private void txtToPasswordHash_TextChanged(object sender, System.EventArgs e)
  40. {
  41. PasswordHasherOutputTextBox.Text = _passwordHasher.HashPassword(PasswordHasherInputTextBox.Text);
  42. }
  43. private void txtResultOfPasswordHash_MouseDoubleClick(object sender, MouseEventArgs e)
  44. {
  45. PasswordHasherOutputTextBox.SelectAll();
  46. }
  47. private void btnNewGuid_Click(object sender, EventArgs e)
  48. {
  49. var g = Guid.NewGuid();
  50. GuidGeneratorResultDTextBox.Text = g.ToString("D").ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper());
  51. GuidGeneratorResultNTextBox.Text = g.ToString("N").ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper());
  52. GuidGeneratorResultAttributeTextBox.Text = $"[Guid(\"{g.ToString("D").ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper()) }\")]";
  53. GuidGeneratorResultNewTextBox.Text = $"new Guid(\"{g.ToString("D").ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper())}\")";
  54. }
  55. private void btnCpN_Click(object sender, EventArgs e) => Clipboard.SetText(GuidGeneratorResultNTextBox.Text);
  56. private void btnCpD_Click(object sender, EventArgs e) => Clipboard.SetText(GuidGeneratorResultDTextBox.Text);
  57. private void btnCpA_Click(object sender, EventArgs e) => Clipboard.SetText(GuidGeneratorResultAttributeTextBox.Text);
  58. private void GuidGeneratorCopyNewButton_Click(object sender, EventArgs e) => Clipboard.SetText(GuidGeneratorResultNewTextBox.Text);
  59. private void NewMachineKeyButton_Click(object sender, EventArgs e)
  60. {
  61. var rng = new RNGCryptoServiceProvider();
  62. string CreateKey(int numBytes)
  63. {
  64. var buff = new byte[numBytes];
  65. rng.GetBytes(buff);
  66. return string.Join("", buff.Select(p => $"{p:X2}"));
  67. }
  68. MachineKeyResult.Text =
  69. $"<machineKey" +
  70. $" validationKey=\"{CreateKey(64)}\"" +
  71. $" decryptionKey=\"{CreateKey(24)}\"" +
  72. $" decryption=\"3DES\"" +
  73. $" validation=\"3DES\" />";
  74. }
  75. private void CopyMachineKeyButton_Click(object sender, EventArgs e)
  76. {
  77. Clipboard.SetText(MachineKeyResult.Text);
  78. }
  79. private void PasswordGenerateButton_Click(object sender, EventArgs e)
  80. {
  81. int GetIntRandomly()
  82. {
  83. var buf = new byte[4];
  84. _rng.GetBytes(buf);
  85. return BitConverter.ToInt32(buf, 0);
  86. }
  87. var stringBuilder = new StringBuilder();
  88. if (PasswordGenerateIncludeUpperCaseCheckBox.Checked) stringBuilder.Append(Enumerable.Range('A', 'Z' - 'A' + 1).Select(p => (char)p).ToArray());
  89. if (PasswordGenerateIncludeLowerCaseCheckBox.Checked) stringBuilder.Append(Enumerable.Range('a', 'z' - 'a' + 1).Select(p => (char)p).ToArray());
  90. if (PasswordGenerateIncludeNumberCheckBox.Checked) stringBuilder.Append(Enumerable.Range('0', '9' - '0' + 1).Select(p => (char)p).ToArray());
  91. if (PasswordGenerateIncludeSymbolCheckBox.Checked) stringBuilder.Append("!@#$%&*");
  92. var charPool = stringBuilder.ToString();
  93. string GetRandomString()
  94. {
  95. return new string(
  96. Enumerable.Range(0, (int)PasswordGenerateLengthUpDownBox.Value)
  97. .Select(p => charPool[Math.Abs(GetIntRandomly() % charPool.Length)])
  98. .ToArray()
  99. );
  100. }
  101. string HashString(string str)
  102. {
  103. if (PasswordGenerateUseMd5RadioButton.Checked)
  104. {
  105. var fmt = PasswordGeneratorHashUseUpperCaseCheckBox.Checked ? "X2" : "x2";
  106. return string.Join("", _md5.ComputeHash(_asciiEncoding.GetBytes(str)).Select(p => p.ToString(fmt)));
  107. }
  108. if (PasswordGenerateUsePasswordHasherRadioButton.Checked)
  109. {
  110. return _passwordHasher.HashPassword(str);
  111. }
  112. throw new NotImplementedException();
  113. }
  114. PasswordGenerateResultRichTextBox.Clear();
  115. for (var i = 0; i < PasswordGenerateNumberUpDownBox.Value; i++)
  116. {
  117. var str = GetRandomString();
  118. PasswordGenerateResultRichTextBox.AppendText($"{str} {HashString(str)}{Environment.NewLine}");
  119. }
  120. }
  121. private void DropToBase64Label_DragEnter(object sender, DragEventArgs e)
  122. {
  123. e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop)
  124. ? DragDropEffects.Copy
  125. : DragDropEffects.None;
  126. }
  127. private void DropToBase64Label_DragDrop(object sender, DragEventArgs e)
  128. {
  129. var files = (string[])e.Data.GetData(DataFormats.FileDrop);
  130. DropToBase64FileNameLabel.Text = Path.GetFileName(files[0]);
  131. var bytes = File.ReadAllBytes(files[0]);
  132. var base64String = Convert.ToBase64String(bytes, Base64FormattingOptions.None);
  133. DropToBase64TextBox.Text = DataUrlCheckBox.Checked
  134. ? $"data:{DataUrlMimeTypeTextBox.Text};base64,{base64String}"
  135. : base64String;
  136. }
  137. private void CopyBase64Button_Click(object sender, EventArgs e)
  138. {
  139. Clipboard.SetText(DropToBase64TextBox.Text);
  140. }
  141. private void BulkNewGuidButton_Click(object sender, EventArgs e)
  142. {
  143. var length = ((int)BulkNewGuidUpDown.Value);
  144. for (var i = 0; i < length; i++)
  145. {
  146. var guid = Guid.NewGuid();
  147. if (NormalBulkNewGuidRadioButton.Checked) BulkNewGuidTextBox.AppendText($"{guid:D}{Environment.NewLine}".ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper()));
  148. if (CtorBulkNewGuidRadioButton.Checked) BulkNewGuidTextBox.AppendText($"new Guid(\"{$"{guid:D}".ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper())}\");{Environment.NewLine}");
  149. if (AttrBulkNewGuidRadioButton.Checked) BulkNewGuidTextBox.AppendText($"[Guid(\"{$"{guid:D}".ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper())}\"]{Environment.NewLine}");
  150. if (NumberBulkNewGuidRadioButton.Checked) BulkNewGuidTextBox.AppendText($"{guid:N}{Environment.NewLine}".ProcessWhen(UpperCaseCheckBox.Checked, p => p.ToUpper()));
  151. }
  152. }
  153. private void MagicHuntingButton_Click(object sender, EventArgs e)
  154. {
  155. var magicPattern = new Regex(MagicPatternText.Text, RegexOptions.Compiled);
  156. var dicCollect = new System.Collections.Generic.Dictionary<string, string>();
  157. CodeConvertTextBox.Text = magicPattern.Replace(CodeInputTextBox.Text, match =>
  158. {
  159. var identity = match.Groups[1].Value;
  160. if (false == IdentifierChecker.Valid(identity)) return match.Value;
  161. if (false == dicCollect.ContainsKey(identity)) dicCollect[identity] = match.Result(CollectPatternTextBox.Text);
  162. return match.Result(ConvertPatternTextBox.Text);
  163. });
  164. CodeCollectTextBox.Text =
  165. "private static class Identities {"
  166. + Environment.NewLine + "// ReSharper disable InconsistentNaming" + Environment.NewLine;
  167. CodeCollectTextBox.Text += string.Join(Environment.NewLine, dicCollect.Values.OrderBy(p => p));
  168. CodeCollectTextBox.Text +=
  169. Environment.NewLine
  170. + "// ReSharper restore InconsistentNaming" + Environment.NewLine
  171. + "}";
  172. }
  173. private void RsaKeyGenerateButton_Click(object sender, EventArgs e)
  174. {
  175. // thanks to dotblogs.com.tw/supershowwei/2015/12/23/160510
  176. // docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsacryptoserviceprovider.keysize#remarks
  177. var rsa = new RSACryptoServiceProvider((int)RsaKeySizeUpDown.Value);
  178. RsaPrivateKeyTextBox.Text = rsa.ToXmlString(true);
  179. RsaPublicKeyTextBox.Text = rsa.ToXmlString(false);
  180. }
  181. private void ConvertCode_Click(object sender, EventArgs e)
  182. {
  183. //html encoding
  184. // <br /> append to end of line
  185. // spaces indent
  186. ConvertedCodeTextBox.Clear();
  187. var lines = InputCodeTextBox.Lines;
  188. for (var index = 0; index < lines.Length; index++)
  189. {
  190. var line = lines[index];
  191. var htmlEncode = HttpUtility.HtmlEncode(line);
  192. var spaces = line.TakeWhile(p => p == ' ').Count();
  193. ConvertedCodeTextBox.AppendText($"<span style=\"padding-left: {spaces / 2.0:00.0}em;\">{htmlEncode}</span>");
  194. if (index < lines.Length - 1)
  195. {
  196. ConvertedCodeTextBox.AppendText($"<br />{Environment.NewLine}");
  197. }
  198. }
  199. }
  200. private void CopyButton_Click(object sender, EventArgs e)
  201. {
  202. Clipboard.SetText(ConvertedCodeTextBox.Text);
  203. }
  204. }
  205. internal static class CommonExtensionMetond
  206. {
  207. public static T ProcessWhen<T>(this T me, bool when, Func<T, T> process)
  208. {
  209. return when ? process(me) : me;
  210. }
  211. }
  212. internal static class IdentifierChecker
  213. {
  214. private static readonly CodeDomProvider Provider = CodeDomProvider.CreateProvider("C#");
  215. public static bool Valid(string word) => Provider.IsValidIdentifier(word);
  216. };
  217. }