public static byte[] SerializeToBytes(RouteMessage instance)
 {
     using (MemoryStream ms = new MemoryStream()) {
         Serialize(ms, instance);
         return(ms.ToArray());
     }
 }
        public static RouteMessage Deserialize(Stream stream)
        {
            RouteMessage instance = new RouteMessage();

            Deserialize(stream, instance);
            return(instance);
        }
Exemple #3
0
        public static Message FromChunk(Chunk chunk, KeyStorage keyStorage)
        {
            if (chunk == null)
            {
                return(null);
            }

            using (MemoryStream ms = new MemoryStream(chunk.Data)) {
                byte[] whisper = new byte[7];
                if (ms.Read(whisper, 0, whisper.Length) != whisper.Length)
                {
                    throw new InvalidDataException("Header not right length");
                }
                if (Encoding.ASCII.GetString(whisper) != "Whisper")
                {
                    throw new InvalidDataException("Missing header");
                }

                MessageHeader header = MessageHeader.Deserialize(ProtocolParser.ReadBytes(ms));

                byte[]  messageBytes = ProtocolParser.ReadBytes(ms);
                Message message;
                switch (header.MessageId)
                {
                case 1:
                    message = TreeMessage.Deserialize(messageBytes);
                    break;

                case 2:
                    message = RouteMessage.Deserialize(messageBytes);
                    break;

                case 3:
                    message = ListMessage.Deserialize(messageBytes);
                    break;

                default:
                    throw new NotImplementedException();
                }

                //Verify signature
                if (header.Signature != null)
                {
                    foreach (PublicKey key in keyStorage.PublicKeys)
                    {
                        if (key.Verify(messageBytes, header.Signature))
                        {
                            message.Signature = key;
                            break;
                        }
                    }
                }

                return(message);
            }
        }
Exemple #4
0
 public static byte[] SerializeMessage(Message m)
 {
     if (m is TreeMessage)
     {
         return(TreeMessage.SerializeToBytes((TreeMessage)m));
     }
     if (m is RouteMessage)
     {
         return(RouteMessage.SerializeToBytes((RouteMessage)m));
     }
     if (m is ListMessage)
     {
         return(ListMessage.SerializeToBytes((ListMessage)m));
     }
     throw new NotImplementedException();
 }
 public static void Serialize(Stream stream, RouteMessage instance)
 {
     if (instance.MessageChunkHash == null)
     {
         throw new ArgumentNullException("MessageChunkHash", "Required by proto specification.");
     }
     ProtocolParser.WriteKey(stream, new ProtocolBuffers.Key(1, Wire.LengthDelimited));
     ProtocolParser.WriteBytes(stream, instance.MessageChunkHash);
     if (instance.To == null)
     {
         throw new ArgumentNullException("To", "Required by proto specification.");
     }
     ProtocolParser.WriteKey(stream, new ProtocolBuffers.Key(2, Wire.LengthDelimited));
     ProtocolParser.WriteString(stream, instance.To);
     if (instance.Chunks != null)
     {
         foreach (byte[] i3 in instance.Chunks)
         {
             ProtocolParser.WriteKey(stream, new ProtocolBuffers.Key(3, Wire.LengthDelimited));
             ProtocolParser.WriteBytes(stream, i3);
         }
     }
 }