Web API for the bulk printing desktop application.

Utils.cs 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. user.Account = await client.GetAccountAsync().ConfigureAwait(false);
  63. return user;
  64. }
  65. }
  66. public static async Task<ProductCatalogue> GetProductCatalogueAsync(
  67. ClientFactory clientFactory, ILogger logger, LoginCredentials credentials)
  68. {
  69. using (var client = clientFactory.GetClient(logger, credentials))
  70. {
  71. await client.ConnectAsync().ConfigureAwait(false);
  72. return await client.GetProductCatalogueAsync(credentials.User.Account).ConfigureAwait(false);
  73. }
  74. }
  75. public static async Task<OrderResponse> PlaceOrderAsync(
  76. ClientFactory clientFactory, ILogger logger, LoginCredentials credentials,
  77. Product product, int quantity, string customerReference, string internalReference,
  78. Guid? orderGuid, byte[] key)
  79. {
  80. using (var client = clientFactory.GetClient(logger, credentials))
  81. {
  82. await client.ConnectAsync().ConfigureAwait(false);
  83. return await client.PlaceOrderAsync(credentials.User.AccountId,
  84. product, quantity, customerReference, internalReference, orderGuid, key).ConfigureAwait(false);
  85. }
  86. }
  87. public static async Task ReExportBatchAsync(
  88. ClientFactory clientFactory, ILogger logger, LoginCredentials credentials,
  89. int batchId, byte[] key)
  90. {
  91. using (var client = clientFactory.GetClient(logger, credentials))
  92. {
  93. await client.ConnectAsync().ConfigureAwait(false);
  94. await client.ReExportBatchAsync(batchId, key).ConfigureAwait(false);
  95. }
  96. }
  97. }
  98. }