Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.Security.Cryptography;
- using System.ServiceModel;
- using System.Text;
- namespace PubSubDup
- {
- [ServiceContract]
- public interface IPub
- {
- [OperationContract]
- void PubInit(string keyPath);
- [OperationContract]
- void Write(string message, byte[] signature, List<int> subscriberIds);
- }
- public interface ISubCallback
- {
- [OperationContract(IsOneWay = true)]
- void WriteToConsole(string message);
- }
- [ServiceContract(CallbackContract = typeof(ISubCallback))]
- public interface ISub
- {
- [OperationContract]
- void SubInit(int id);
- }
- public class PubSubService : IPub, ISub
- {
- delegate void SubDelegate(string message, int subId);
- static event SubDelegate onMessageReceived = null;
- static Dictionary<int, ISubCallback> proxies = new Dictionary<int, ISubCallback>();
- static CspParameters csp = new CspParameters();
- static RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp);
- static string publicKey = null;
- public void PubInit(string keyPath)
- {
- if (publicKey == null)
- {
- publicKey = keyPath;
- onMessageReceived += (string message, int id) => proxies[id].WriteToConsole(message);
- }
- }
- public void SubInit(int id)
- {
- var proxy = OperationContext.Current.GetCallbackChannel
- <ISubCallback>();
- if(proxies.ContainsKey(id))
- {
- proxy.WriteToConsole("Vec postoji unet ID.");
- }
- else
- {
- proxies.Add(id, proxy);
- }
- }
- public void Write(string message, byte[] signature, List<int> subscriberIds)
- {
- byte[] hash = null;
- using (SHA256 sha = SHA256Managed.Create())
- {
- hash = sha.ComputeHash(Encoding.UTF8.GetBytes(message));
- }
- if (publicKey != null)
- {
- using (StreamReader sr = new StreamReader(publicKey))
- {
- rsa.FromXmlString(sr.ReadToEnd());
- }
- }
- else
- {
- return;
- }
- RSAPKCS1SignatureDeformatter defformater = new RSAPKCS1SignatureDeformatter(rsa);
- defformater.SetHashAlgorithm("SHA256");
- if(defformater.VerifySignature(hash, signature))
- {
- if (onMessageReceived != null)
- {
- foreach (int id in subscriberIds)
- {
- if(proxies.ContainsKey(id))
- {
- onMessageReceived(message, id);
- }
- }
- }
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.ServiceModel;
- namespace Subscriber
- {
- public class SubscriberCallback : ServiceReference1.ISubCallback
- {
- public void WriteToConsole(string message)
- {
- Console.WriteLine(message);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- InstanceContext ic = new InstanceContext(new SubscriberCallback());
- ServiceReference1.SubClient proxy = new
- ServiceReference1.SubClient(ic);
- Console.WriteLine("Unesite ID subscribera: ");
- int id = 0;
- id = Convert.ToInt32(Console.ReadLine());
- proxy.SubInit(id);
- Console.ReadKey();
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace Publisher
- {
- class Program
- {
- static ServiceReference1.PubClient pubClient = new ServiceReference1.PubClient();
- static CspParameters csp = new CspParameters();
- static RSACryptoServiceProvider rsa = null;
- const string KEY_PATH = @"C:\temp\publicKey.txt";
- static void CreateAsmKeys()
- {
- rsa = new RSACryptoServiceProvider(csp);
- }
- static void ExportPublicKey()
- {
- using (StreamWriter sw = new StreamWriter(KEY_PATH))
- {
- sw.Write(rsa.ToXmlString(false));
- }
- }
- static byte[] SignMessage(string message)
- {
- byte[] hash = null;
- using (SHA256 sha = SHA256Managed.Create())
- {
- hash = sha.ComputeHash(Encoding.UTF8.GetBytes(message));
- }
- var formatter = new RSAPKCS1SignatureFormatter(rsa);
- formatter.SetHashAlgorithm("SHA256");
- return formatter.CreateSignature(hash);
- }
- static void Main(string[] args)
- {
- CreateAsmKeys();
- ExportPublicKey();
- pubClient.PubInit(KEY_PATH);
- Random rnd = new Random();
- string message = "";
- while(true)
- {
- List<int> subscribers = new List<int>() { -1, -1, -1};
- for(int i = 0; i < subscribers.Count; i++)
- subscribers[i] = rnd.Next(0, 3);
- if(message.Length > 10)
- {
- message = "a";
- }
- else
- {
- message += "a";
- }
- byte[] signature = SignMessage(message);
- pubClient.Write(message, signature, subscribers.ToArray());
- Console.Write($"Poruka {message} je poslata na subscriber-e:");
- foreach (int i in subscribers)
- Console.Write($"{i}, ");
- Console.WriteLine();
- Thread.Sleep(5000);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment