Beispiel #1
0
        /// <summary>
        /// Deserializer
        /// </summary>
        /// <param name="str">The Json serialized message</param>
        /// <returns>The deserialized <see cref="Message"/></returns>
        public static Message FromJson(string str)
        {
            SignedMessage sm = SignedMessage.FromJson(str);

            // if the message is a signed message decode the contained json
            if (sm != null)
            {
                str = sm.JsonMessage;
                Message  deserialized = FromJson(str);
                ChatUser from         = new AddressBook().RetrieveUser(deserialized.Sender);
                deserialized.Verified = (from != null && SignedMessage.Verify(sm, from.PublicKey));
                return(deserialized);
            }

            var res = JsonConvert.DeserializeObject <Message>(str);

            res.Verified = false;
            switch (res.Kind)
            {
            case MessageKindType.STRING:
            case MessageKindType.URL:
            {
                return(res);
            }

            case MessageKindType.FILE:
            {
                var res2 = JsonConvert.DeserializeObject <ChatFile>(str);
                return(res2);
            }

            case MessageKindType.IMAGE:
            {
                var res2 = JsonConvert.DeserializeObject <ChatImage>(str);
                return(res2);
            }

            case MessageKindType.USER:
            {
                var res2 = JsonConvert.DeserializeObject <ChatUser>(str);
                return(res2);
            }

            case MessageKindType.ENCRYPTED:
            {
                byte[] decrypted = new SecureMe().DecryptMessage(res.DataContent);
                if (decrypted == null)
                {
                    return new Message()
                           {
                               Kind = MessageKindType.VOID
                           }
                }
                ;
                var res2 = FromJson(Encoding.ASCII.GetString(new SecureMe().DecryptMessage(res.DataContent)));
                //restore destination from envelop
                res2.Destination = res.Destination;
                //restore verified from envelop
                res2.Verified = res.Verified;
                return(res2);
            }

            default: throw new InvalidOperationException(string.Format("Unrecognized message type {0}", res.Kind));
            }
        }
Beispiel #2
0
 /// <summary>
 /// Verify the signature of a <see cref="SignedMessage"/>
 /// </summary>
 /// <param name="sm">The <see cref="SignedMessage"/> to verify</param>
 /// <param name="publicKey">The public key of the signer</param>
 /// <returns>True if the signature is verified, false elseware</returns>
 public static bool Verify(SignedMessage sm, string publicKey)
 {
     return(new SecureMe().Verify(sm.JsonMessage, sm.Signature, publicKey));
 }