PageRenderTime 71ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/Mimoza/Utils/CompressMACs/Program.cs

#
C# | 378 lines | 293 code | 53 blank | 32 comment | 34 complexity | ce2b40d68e599c6c0471cf951193fe8c MD5 | raw file
Possible License(s): GPL-2.0
  1. using System;
  2. using System.IO;
  3. //using System.Text;
  4. using System.Xml;
  5. using System.Xml.Serialization;
  6. using System.Collections;
  7. using System.Resources;
  8. using System.Reflection;
  9. using System.Security.Cryptography;
  10. namespace Mimoza.Utils
  11. {
  12. public class MyComparerClass : IComparer
  13. {
  14. // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
  15. int IComparer.Compare(Object x, Object y)
  16. {
  17. byte[] x1 = (byte[])x;
  18. byte[] y1 = (byte[])y;
  19. for (int i = 0; i < 3; i++)
  20. {
  21. if (x1[i] < y1[i]) return -1;
  22. if (x1[i] > y1[i]) return 1;
  23. }
  24. return 0;
  25. }
  26. }
  27. public class MACList
  28. {
  29. public MACList() { }
  30. public byte[] root = new byte[9];
  31. public ArrayList list = new ArrayList();
  32. }
  33. public class ReadMACs
  34. {
  35. public ArrayList readedMACs = new ArrayList();
  36. public ReadMACs()
  37. { }
  38. public void Do(string fileName)
  39. {
  40. try
  41. {
  42. using (StreamReader sr = new StreamReader(fileName))
  43. {
  44. int i, lineNumber = 0;
  45. byte[] mac;
  46. byte[] root = new byte[12];
  47. byte[] current = new byte[12];
  48. String line, l;
  49. MACList mlist;
  50. // Read and display lines from the file until the end of
  51. // the file is reached.
  52. while ((l = sr.ReadLine()) != null)
  53. {
  54. line = l.Trim();
  55. i = 0;
  56. lineNumber++;
  57. root.Initialize();
  58. foreach (char c in line)
  59. {
  60. root[i] = (byte)Convert.ToInt16(c.ToString(), 16);
  61. i++;
  62. if (i > 12)
  63. {
  64. Console.WriteLine("MAC address '" + line + "' to long: Error in line " + lineNumber.ToString());
  65. break;
  66. }
  67. }
  68. if (i < 12)
  69. {
  70. Console.WriteLine("MAC address '" + line + "' to short: Error in line " + lineNumber.ToString());
  71. continue;
  72. }
  73. if (i > 12)
  74. {
  75. continue;
  76. }
  77. mlist = null;
  78. foreach (MACList m in readedMACs)
  79. {
  80. mlist = m;
  81. for (int k = 0; k < 9; k++)
  82. {
  83. if (mlist.root[k] != root[k])
  84. {
  85. mlist = null;
  86. break;
  87. }
  88. }
  89. if (mlist != null)
  90. {
  91. break;
  92. }
  93. }
  94. if (mlist == null)
  95. {
  96. mlist = new MACList();
  97. Array.Copy(root, 0, mlist.root, 0, 9);
  98. readedMACs.Add(mlist);
  99. }
  100. mac = new byte[3];
  101. Array.Copy(root, 9, mac, 0, 3);
  102. mlist.list.Add(mac);
  103. }
  104. }
  105. }
  106. catch (Exception e)
  107. {
  108. Console.WriteLine("The file could not be read:");
  109. Console.WriteLine(e.Message);
  110. }
  111. }
  112. }
  113. public class WriteMACs
  114. {
  115. private const int MaxNumberInThread = 1000;
  116. private ArrayList readedMACs;
  117. public WriteMACs(ArrayList macs)
  118. {
  119. readedMACs = macs;
  120. }
  121. public void Do(string modelName, string OSName, string version, string fileName)
  122. {
  123. byte[] lastStr;
  124. int writedNumbers;
  125. int writedNumbersInThread;
  126. string datFileName = fileName + ".dat";
  127. Mimoza.Common.LicenceFile licenceFile = new Mimoza.Common.LicenceFile();
  128. licenceFile.ModelName = modelName;
  129. licenceFile.OSName = OSName;
  130. licenceFile.Version = version;
  131. if (modelName == Mimoza.Common.Model.C36SX_DisplayName)
  132. {
  133. licenceFile.Model = Mimoza.Common.ModelEnum.C36SX;
  134. }
  135. else if (modelName == Mimoza.Common.Model.L66_DisplayName)
  136. {
  137. licenceFile.Model = Mimoza.Common.ModelEnum.L66;
  138. }
  139. else if (modelName == Mimoza.Common.Model.LX_DisplayName)
  140. {
  141. licenceFile.Model = Mimoza.Common.ModelEnum.LX;
  142. }
  143. else if (modelName == Mimoza.Common.Model.VPC_DisplayName)
  144. {
  145. licenceFile.Model = Mimoza.Common.ModelEnum.VPC;
  146. }
  147. else
  148. {
  149. throw new Exception("Unknown platform - " + modelName);
  150. }
  151. using (FileStream fs = new FileStream(datFileName, FileMode.Create))
  152. using (BinaryWriter w = new BinaryWriter(fs))
  153. {
  154. licenceFile.WriteToStream(fs);
  155. #region Write MACs foreach (MACList m in readedMACs )
  156. foreach (MACList m in readedMACs)
  157. {
  158. m.list.Sort(new MyComparerClass());
  159. writedNumbers = 0;
  160. writedNumbersInThread = 0;
  161. #region foreach ( byte[] str in m.list )
  162. foreach (byte[] str in m.list)
  163. {
  164. //write header of the thteard
  165. if (writedNumbersInThread == 0 || writedNumbersInThread > MaxNumberInThread - 1)
  166. {
  167. int count;
  168. //Console.WriteLine("Start new thread");
  169. foreach (byte c in m.root)
  170. {
  171. w.Write(c);
  172. }
  173. if (m.list.Count - writedNumbers > MaxNumberInThread)
  174. {
  175. count = MaxNumberInThread;
  176. }
  177. else
  178. {
  179. count = m.list.Count - writedNumbers;
  180. }
  181. //Console.Write(" = " + count.ToString() + " last mac = ");
  182. w.Write((Int16)count);
  183. writedNumbersInThread = 0;
  184. lastStr = (byte[])m.list[writedNumbers + count - 1];
  185. foreach (byte c in lastStr)
  186. {
  187. w.Write(c);
  188. }
  189. }
  190. writedNumbers++;
  191. writedNumbersInThread++;
  192. foreach (byte c in str)
  193. {
  194. w.Write(c);
  195. }
  196. }
  197. #endregion
  198. }
  199. #endregion
  200. #region Arrange size of file up to 1M
  201. int randomBytes = 1048576 - (int)(fs.Length % 1048576);
  202. for (int i = 0; i < randomBytes; i++)
  203. {
  204. char c = (char)(0);
  205. w.Write(c);
  206. }
  207. #endregion
  208. SingMACS(fileName, fs);
  209. bool res = VerifySingMACS(fileName, fs);
  210. Console.WriteLine("Result - " + res.ToString());
  211. }
  212. }
  213. private Stream GetSerealizedKey(string name)
  214. {
  215. string path = Assembly.GetExecutingAssembly().Location;
  216. path = path.Substring(0,path.LastIndexOf('\\')) + @"\";
  217. return new FileStream(path + name, FileMode.Open,FileAccess.Read);
  218. //string name1 = Assembly.GetExecutingAssembly().GetName().CodeBase;
  219. //Assembly asm = Assembly.LoadFrom(name1);
  220. //if (asm != null)
  221. //{
  222. // string[] strArr = asm.GetManifestResourceNames();
  223. // if (strArr != null && strArr.Length > 0)
  224. // {
  225. // foreach (string str in strArr)
  226. // {
  227. // if (str.EndsWith(name))
  228. // {
  229. // return asm.GetManifestResourceStream(str);
  230. // //Stream stream = asm.GetManifestResourceStream(str);
  231. // //return new XmlTextReader(stream);
  232. // }
  233. // }
  234. // }
  235. //}
  236. //return null;
  237. }
  238. private bool VerifySingMACS(string filename, Stream dataStream)
  239. {
  240. dataStream.Position = 0;
  241. byte[] sign;
  242. byte[] data = new byte[dataStream.Length];
  243. dataStream.Read(data, 0, (int)dataStream.Length);
  244. RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();
  245. using (Stream file = GetSerealizedKey("PublicKey.bin"))
  246. {
  247. byte[] blob = new byte[file.Length];
  248. file.Read(blob, 0, (int)file.Length);
  249. RSAalg.ImportCspBlob(blob);
  250. }
  251. using (FileStream file = new FileStream(filename + ".sign", FileMode.Open, FileAccess.Read))
  252. {
  253. sign = new byte[file.Length];
  254. file.Read(sign, 0, (int)file.Length);
  255. Array.Reverse(sign);
  256. }
  257. return RSAalg.VerifyData(data, new SHA1CryptoServiceProvider(), sign);
  258. }
  259. private void SingMACS(string filename, Stream dataStream)
  260. {
  261. dataStream.Position = 0;
  262. RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();
  263. using (Stream file = GetSerealizedKey("PrivateKey.bin"))
  264. {
  265. byte[] blob = new byte[file.Length];
  266. file.Read(blob, 0, (int)file.Length);
  267. RSAalg.ImportCspBlob(blob);
  268. }
  269. #region Export Public Key
  270. using (StreamWriter sw = new StreamWriter("PublicKey.txt"))
  271. {
  272. byte[] pubKey = RSAalg.ExportCspBlob(false);
  273. foreach (byte b in pubKey)
  274. {
  275. sw.Write(b.ToString() + ",");
  276. }
  277. }
  278. #endregion
  279. // Hash and sign the data. Pass a new instance of SHA1CryptoServiceProvider
  280. // to specify the use of SHA1 for hashing.
  281. using (FileStream file = new FileStream(filename + ".sign", FileMode.CreateNew))
  282. {
  283. SHA1Managed hash = new SHA1Managed();
  284. byte[] hashedData;
  285. byte[] data = new byte[dataStream.Length];
  286. dataStream.Read(data, 0, (int)dataStream.Length);
  287. hashedData = hash.ComputeHash(data);
  288. byte[] signedData = RSAalg.SignHash(hashedData, CryptoConfig.MapNameToOID("SHA1"));
  289. //Reverse signature for using in C++
  290. Array.Reverse(signedData);
  291. file.Write(signedData, 0, signedData.Length);
  292. }
  293. }
  294. }
  295. class CompressMACs
  296. {
  297. /// <summary>
  298. /// The main entry point for the application.
  299. /// </summary>
  300. [STAThread]
  301. static void Main(string[] args)
  302. {
  303. try
  304. {
  305. ReadMACs r_macs = new ReadMACs();
  306. WriteMACs w_macs = new WriteMACs(r_macs.readedMACs);
  307. if (args.Length != 5)
  308. {
  309. Console.WriteLine("The syntax of the command is incorrect:" + args.Length.ToString());
  310. Console.WriteLine(" Use the following syntax 'CompressMACs ModelName OSNAme Version sourceFILE destinationFILE'");
  311. }
  312. else
  313. {
  314. r_macs.Do(args[3]);
  315. w_macs.Do(args[0], args[1], args[2], args[4]);
  316. }
  317. }
  318. catch (Exception e)
  319. {
  320. Console.Write("Exception was occuried:" + e.ToString());
  321. Mimoza.Common.Logger.Log.Error("CompressMACs: Progarm.Main error - '" + e.ToString() + "'.");
  322. }
  323. }
  324. }
  325. }