Web API for the bulk printing desktop application.

Utils.cs 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using MAX.Models;
  2. using Microsoft.Extensions.Logging;
  3. using System;
  4. using System.IO;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace MAX
  9. {
  10. public static class Utils
  11. {
  12. public static byte[] Transform(ICryptoTransform transform, byte[] input)
  13. {
  14. using (var memoryStream = new MemoryStream())
  15. using (var cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write))
  16. {
  17. cryptoStream.Write(input, 0, input.Length);
  18. cryptoStream.FlushFinalBlock();
  19. return memoryStream.ToArray();
  20. }
  21. }
  22. public static string TripleDESDecrypt(string cipherText, TripleDES des)
  23. {
  24. using (var decryptor = des.CreateDecryptor(des.Key, des.IV))
  25. {
  26. return Encoding.UTF8.GetString(Transform(decryptor, Convert.FromBase64String(cipherText)));
  27. }
  28. }
  29. public static string TripleDESDecrypt(string cipherText, byte[] key)
  30. {
  31. using (var des = TripleDES.Create())
  32. {
  33. des.Key = key;
  34. des.IV = new byte[8];
  35. return TripleDESDecrypt(cipherText, des);
  36. }
  37. }
  38. public static string TripleDESEncrypt(string plainText, TripleDES des)
  39. {
  40. using (var encryptor = des.CreateEncryptor(des.Key, des.IV))
  41. {
  42. return Convert.ToBase64String(Transform(encryptor, Encoding.UTF8.GetBytes(plainText)));
  43. }
  44. }
  45. public static string TripleDESEncrypt(string plainText, byte[] key)
  46. {
  47. using (var des = TripleDES.Create())
  48. {
  49. des.Key = key;
  50. des.IV = new byte[8];
  51. return TripleDESEncrypt(plainText, des);
  52. }
  53. }
  54. public static async Task<User> AuthenticateUserAsync(ClientFactory clientFactory,
  55. ILogger logger, int userId, string username, int vendorId, string serialNumber,
  56. string password)
  57. {
  58. using (var client = clientFactory.GetClient(logger, vendorId, serialNumber,
  59. userId, username, password))
  60. {
  61. var user = await client.ConnectAsync().ConfigureAwait(false);
  62. if (user != null)
  63. {
  64. user.Account = await client.GetAccountAsync().ConfigureAwait(false);
  65. }
  66. return user;
  67. }
  68. }
  69. public static async Task<ProductCatalogue> GetProductCatalogueAsync(
  70. ClientFactory clientFactory, ILogger logger, LoginCredentials credentials)
  71. {
  72. using (var client = clientFactory.GetClient(logger, credentials))
  73. {
  74. var user = await client.ConnectAsync().ConfigureAwait(false);
  75. if (user == null)
  76. {
  77. throw new Exception(string.Format("Invalid login credentials for {0}",
  78. credentials.ToString()));
  79. }
  80. return await client.GetProductCatalogueAsync(credentials.User.Account).ConfigureAwait(false);
  81. }
  82. }
  83. public static async Task<OrderResponse> PlaceOrderAsync(
  84. ClientFactory clientFactory, ILogger logger, LoginCredentials credentials,
  85. Product product, int quantity, string customerReference, string internalReference,
  86. Guid? orderGuid, byte[] key)
  87. {
  88. using (var client = clientFactory.GetClient(logger, credentials))
  89. {
  90. var user = await client.ConnectAsync().ConfigureAwait(false);
  91. if (user == null)
  92. {
  93. throw new Exception(string.Format("Invalid login credentials for {0}",
  94. credentials.ToString()));
  95. }
  96. return await client.PlaceOrderAsync(credentials.User.AccountId,
  97. product, quantity, customerReference, internalReference, orderGuid, key).ConfigureAwait(false);
  98. }
  99. }
  100. }
  101. }