MTSC  1.0.5
Build TCP servers out of modules with handlers for communication, exception and logging.
HelperFunctions.cs
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Security.Cryptography;
5 using System.Text;
6 using System.Xml;
7 
8 namespace MTSC
9 {
10  static class HelperFunctions
11  {
12  #region XML
13 
14  public static void FromXmlString(this RSA rsa, string xmlString)
15  {
16  RSAParameters parameters = new RSAParameters();
17 
18  XmlDocument xmlDoc = new XmlDocument();
19  xmlDoc.LoadXml(xmlString);
20 
21  if (xmlDoc.DocumentElement.Name.Equals("RSAKeyValue"))
22  {
23  foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
24  {
25  switch (node.Name)
26  {
27  case "Modulus": parameters.Modulus = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
28  case "Exponent": parameters.Exponent = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
29  case "P": parameters.P = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
30  case "Q": parameters.Q = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
31  case "DP": parameters.DP = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
32  case "DQ": parameters.DQ = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
33  case "InverseQ": parameters.InverseQ = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
34  case "D": parameters.D = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
35  }
36  }
37  }
38  else
39  {
40  throw new Exception("Invalid XML RSA key.");
41  }
42 
43  rsa.ImportParameters(parameters);
44  }
45 
46  public static string ToXmlString(this RSA rsa, bool includePrivateParameters)
47  {
48  RSAParameters parameters = rsa.ExportParameters(includePrivateParameters);
49 
50  return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>",
51  parameters.Modulus != null ? Convert.ToBase64String(parameters.Modulus) : null,
52  parameters.Exponent != null ? Convert.ToBase64String(parameters.Exponent) : null,
53  parameters.P != null ? Convert.ToBase64String(parameters.P) : null,
54  parameters.Q != null ? Convert.ToBase64String(parameters.Q) : null,
55  parameters.DP != null ? Convert.ToBase64String(parameters.DP) : null,
56  parameters.DQ != null ? Convert.ToBase64String(parameters.DQ) : null,
57  parameters.InverseQ != null ? Convert.ToBase64String(parameters.InverseQ) : null,
58  parameters.D != null ? Convert.ToBase64String(parameters.D) : null);
59  }
65  public static bool IsSubPathOf(this string path, string baseDirPath)
66  {
67  string normalizedPath = Path.GetFullPath(path.Replace('/', '\\')
68  .WithEnding("\\"));
69 
70  string normalizedBaseDirPath = Path.GetFullPath(baseDirPath.Replace('/', '\\')
71  .WithEnding("\\"));
72 
73  return normalizedPath.StartsWith(normalizedBaseDirPath, StringComparison.OrdinalIgnoreCase);
74  }
80  public static string WithEnding(this string str, string ending)
81  {
82  if (str == null)
83  return ending;
84 
85  string result = str;
86 
87  // Right() is 1-indexed, so include these cases
88  // * Append no characters
89  // * Append up to N characters, where N is ending length
90  for (int i = 0; i <= ending.Length; i++)
91  {
92  string tmp = result + ending.Right(i);
93  if (tmp.EndsWith(ending))
94  return tmp;
95  }
96 
97  return result;
98  }
103  public static string Right(this string value, int length)
104  {
105  if (value == null)
106  {
107  throw new ArgumentNullException("value");
108  }
109  if (length < 0)
110  {
111  throw new ArgumentOutOfRangeException("length", length, "Length is less than zero");
112  }
113 
114  return (length < value.Length) ? value.Substring(value.Length - length) : value;
115  }
116  #endregion
117  }
118 }
Definition: Client.cs:14