Advertisement
Mizzzlo

Untitled

Sep 19th, 2023
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.60 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5.  
  6. class Program
  7. {
  8.     static void Main(string[] args)
  9.     {
  10.         //Выведите платёжные ссылки для трёх разных систем платежа:
  11.         //pay.system1.ru/order?amount=12000RUB&hash={MD5 хеш ID заказа}
  12.         //order.system2.ru/pay?hash={MD5 хеш ID заказа + сумма заказа}
  13.         //system3.com/pay?amount=12000&curency=RUB&hash={SHA-1 хеш сумма заказа + ID заказа + секретный ключ от системы}
  14.  
  15.         PayLinkFinder payLinkFinder1 = new PayLinkFinder(new System1(new MD5HashInt()), new Order(13, 12000));
  16.         PayLinkFinder payLinkFinder2 = new PayLinkFinder(new System2(new MD5HashInt()), new Order(4234, 3000));
  17.         PayLinkFinder payLinkFinder3 = new PayLinkFinder(new System3(new SHA1HashInt()), new Order(5321, 12000));
  18.  
  19.         PayLinkFinder[] payLinkFinders = new PayLinkFinder[] { payLinkFinder1, payLinkFinder2, payLinkFinder3 };
  20.  
  21.         foreach (var payLinkFinder in payLinkFinders)
  22.         {
  23.             payLinkFinder.Find();
  24.         }
  25.     }
  26. }
  27.  
  28. public class PayLinkFinder
  29. {
  30.     private IPaymentSystem _paymentSystem;
  31.     private Order _order;
  32.  
  33.     public PayLinkFinder(IPaymentSystem paymentSystem, Order order)
  34.     {
  35.         _paymentSystem = paymentSystem;
  36.         _order = order;
  37.     }
  38.  
  39.     public void Find()
  40.     {
  41.         Console.WriteLine(_paymentSystem.GetPayingLink(_order));
  42.         Console.WriteLine();
  43.     }
  44. }
  45.  
  46. public class MD5HashInt : IHash
  47. {
  48.     public string GetHash(int inputData)
  49.     {
  50.         MD5 md5 = MD5.Create();
  51.  
  52.         return String.Concat(md5.ComputeHash(BitConverter.GetBytes(inputData)).Select(x => x.ToString("x2")));
  53.     }
  54. }
  55.  
  56. public class SHA1HashInt : IHash
  57. {
  58.     public string GetHash(int inputData)
  59.     {
  60.         var hash = new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(inputData.ToString()));
  61.  
  62.         return $"{String.Concat(hash.Select(b => b.ToString("x2")))}";
  63.     }
  64. }
  65.  
  66. public class Order
  67. {
  68.     public readonly int Id;
  69.     public readonly int Amount;
  70.  
  71.     public Order(int id, int amount)
  72.     {
  73.         Id = id;
  74.         Amount = amount;
  75.     }
  76. }
  77.  
  78. public abstract class PaymentSystem : IPaymentSystem
  79. {
  80.     protected readonly IHash HashInt;
  81.     protected readonly string PaymentSystemLink;
  82.  
  83.     protected PaymentSystem(IHash hashInt)
  84.     {
  85.         HashInt = hashInt;
  86.     }
  87.  
  88.     public abstract string GetPayingLink(Order order);
  89. }
  90.  
  91. public class System1 : PaymentSystem
  92. {
  93.     public System1(IHash hashInt) : base(hashInt)
  94.     {
  95.     }
  96.  
  97.     public override string GetPayingLink(Order order)
  98.     {
  99.         return $"pay.system1.ru/order?amount=12000RUB&hash={HashInt.GetHash(order.Id)}";
  100.     }
  101. }
  102.  
  103. public class System2 : PaymentSystem
  104. {
  105.     public System2(IHash hashInt) : base(hashInt)
  106.     {
  107.     }
  108.  
  109.     public override string GetPayingLink(Order order)
  110.     {
  111.         return $"order.system2.ru/pay?hash ={HashInt.GetHash(order.Id)}+{HashInt.GetHash(order.Amount)}";
  112.     }
  113. }
  114.  
  115. public class System3 : PaymentSystem
  116. {
  117.     private int _secretKey = 125623145;
  118.  
  119.     public System3(IHash hashInt) : base(hashInt)
  120.     {
  121.     }
  122.  
  123.     public override string GetPayingLink(Order order)
  124.     {
  125.         return $"system3.com/pay?amount=12000&curency=RUB&hash={HashInt.GetHash(order.Id)}+{HashInt.GetHash(order.Amount)}+{HashInt.GetHash(_secretKey)}";
  126.     }
  127. }
  128.  
  129. public interface IPaymentSystem
  130. {
  131.     public string GetPayingLink(Order order);
  132. }
  133.  
  134. public interface IHash
  135. {
  136.     public string GetHash(int inputData);
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement