public static byte[] SerializeToBytes(TreeMessage instance) { using (MemoryStream ms = new MemoryStream()) { Serialize(ms, instance); return(ms.ToArray()); } }
public static TreeMessage Deserialize(Stream stream) { TreeMessage instance = new TreeMessage(); Deserialize(stream, instance); return(instance); }
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); } }
public static void Main(string[] args, KeyStorage keyStorage) { //Usage if (args.Length != 4) throw new HelpException ("Missing arguments"); string sourcePath = args [1]; string repoPath = args [2]; string receipientName = args [3]; //Source if (Directory.Exists (sourcePath) == false) throw new HelpException ("Source directory not found: " + sourcePath); //Repo Repo repo = Repo.Create (repoPath); //Sender and Recipient keys PrivateKey senderKey = keyStorage.DefaultKey; PublicKey recipientKey = keyStorage.GetPublic (receipientName); //Prepare Route message recording of ChunkID RouteRepo rr = new RouteRepo (repo); //Prepare Encryption EncryptedRepo er = new EncryptedRepo (rr, null); er.AddKey (recipientKey); Console.Write ("Generating Tree..."); //Send Tree ChunkHash tree = TreeChunk.GenerateChunk (sourcePath, er); //TreeMessage TreeMessage tm = new TreeMessage (tree, Path.GetDirectoryName (sourcePath)); Chunk tmc = Message.ToChunk (tm, senderKey); ChunkHash tmch = er.WriteChunk (tmc); er.StoreMessage ("file", tmch); //RouteMessage RouteMessage rm = rr.RouteMessage; rm.MessageChunkHash = tmch.bytes; rm.To = receipientName; //Store unencrypted RouteMessage Chunk rmChunk = Message.ToChunk (rm); repo.WriteChunk (rmChunk); repo.StoreMessage ("route", rmChunk.ChunkHash); Console.WriteLine ("RouteMessage Stored"); }
public static void Serialize(Stream stream, TreeMessage instance) { if (instance.Name == null) { throw new ArgumentNullException("Name", "Required by proto specification."); } ProtocolParser.WriteKey(stream, new ProtocolBuffers.Key(1, Wire.LengthDelimited)); ProtocolParser.WriteString(stream, instance.Name); if (instance.TreeChunkHash == null) { throw new ArgumentNullException("TreeChunkHash", "Required by proto specification."); } ProtocolParser.WriteKey(stream, new ProtocolBuffers.Key(2, Wire.LengthDelimited)); ProtocolParser.WriteBytes(stream, instance.TreeChunkHash); }
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(); }