private async void OnSendInitialMessagePressed(object sender, EventArgs e)
        {
            string recipientUuid = RecipientUuid.Text;
            string messageText   = Message.Text;

            ChatPackage chatPackage = await _connection.GetChatPackage(recipientUuid);

            MetaMessage initialMessage = MessageUtils.ComposeInitialMessage(recipientUuid, chatPackage, messageText);

            await _connection.SendMessageAsync(initialMessage);
        }
Esempio n. 2
0
 public static bool NetPackageChat_ProcessPackage_Prefix(NetPackageChat __instance)
 {
     if (PersistentOperations.Net_Package_Detector && __instance.Sender != null)
     {
         if (!ChatPackage.IsValid(__instance))
         {
             return(false);
         }
     }
     return(true);
 }
Esempio n. 3
0
 public async static Task CreateChatConversation(ChatPackage package)
 {
     try
     {
         if (App.HubConn != null && App.HubConn.State == HubConnectionState.Connected)
         {
             await App.HubConn.InvokeAsync("CreateChatConversation", package);
         }
         else
         {
             throw new Exception("Mất kết nói với máy chủ. Hãy đăng nhập lại!");
         }
     }
     catch (Exception ex)
     {
         throw new Exception(ex.Message);
     }
 }
Esempio n. 4
0
        public Task <ChatPackage> GetMessagesTaskStart(bool getGlobal, bool getPrivate, bool getLog)
        {
            var t = new Task <ChatPackage>
                    (
                () =>
            {
                ChatPackage chatPack = new ChatPackage();

                lock (_dataLock)
                {
                    if (getGlobal)
                    {
                        foreach (string content in _globalChatContent)
                        {
                            chatPack.GlobalChatMessageList.Add(content);
                        }
                    }

                    if (getPrivate)
                    {
                        foreach (string content in _privateChatContent)
                        {
                            chatPack.PrivateChatMessageList.Add(content);
                        }
                    }

                    if (getLog)
                    {
                        foreach (string content in _logContent)
                        {
                            chatPack.LogMessageList.Add(content);
                        }
                    }
                }

                return(chatPack);
            }
                    );

            t.Start();
            return(t);
        }
Esempio n. 5
0
    private async void GetNewMessagesAsync()
    {
        ChatPackage msgPack = await _chat.GetMessagesTaskStart
                              (
            (_currentType == MessageType.Global),
            (_currentType == MessageType.Private),
            (_currentType == MessageType.Log)
                              );

        if (msgPack.GlobalChatMessageList.Count > 0)
        {
            UpdateChat(msgPack.GlobalChatMessageList, MessageType.Global);
        }

        if (msgPack.PrivateChatMessageList.Count > 0)
        {
            UpdateChat(msgPack.PrivateChatMessageList, MessageType.Private);
        }

        if (msgPack.LogMessageList.Count > 0)
        {
            UpdateChat(msgPack.LogMessageList, MessageType.Log);
        }
    }
Esempio n. 6
0
        public static MetaMessage ComposeInitialMessage(string recipientUuid, ChatPackage chatPackage, string text)
        {
            Curve25519KeyPair ephemeralKeyPair = new Curve25519KeyPair();

            byte[] secretKey = CryptoUtils.DeriveX3DhSecretSender(GlobalKeyStore.Instance.IdentityKeyPair,
                                                                  ephemeralKeyPair, chatPackage.OtherIdentityKey, chatPackage.OtherSignedPreKey,
                                                                  chatPackage.OtherOneTimePreKey);


            Curve25519KeyPair dhSendingKeyPair = new Curve25519KeyPair();

            (byte[] rootKey, byte[] sendingChainKey) = CryptoUtils.RatchetRootKey(secretKey,
                                                                                  dhSendingKeyPair.CalculateSharedSecret(chatPackage.OtherSignedPreKey));

            ChatState state = new ChatState
            {
                DhSendingKeyPair  = dhSendingKeyPair,
                DhReceivingKey    = chatPackage.OtherSignedPreKey,
                RootKey           = rootKey,
                SendingChainKey   = sendingChainKey,
                ReceivingChainKey = null,
                CountSent         = 0,
                CountReceived     = 0,
                PreviousCount     = 0,
                MissedMessages    = new Dictionary <string, (byte[], byte[])>()
            };

            GlobalKeyStore.Instance.ChatStates[recipientUuid] = state;

            byte[] initialAssociatedData = CryptoUtils.CalculateInitialAssociatedData(
                GlobalKeyStore.Instance.IdentityKeyPair,
                chatPackage.OtherIdentityKey);
            (byte[] encryptedText, MessageHeader header) = RatchetEncrypt(state, Encoding.UTF8.GetBytes(text), initialAssociatedData);

            NormalTextMessage innerPayload = new NormalTextMessage
            {
                AssociatedDataBase64 = Convert.ToBase64String(initialAssociatedData),
                DhKeyBase64          = Convert.ToBase64String(dhSendingKeyPair.XPublicKey),
                EncryptedTextBase64  = Convert.ToBase64String(encryptedText),
                MessageNumber        = header.MessageNumber,
                PreviousCount        = header.PreviousCount
            };

            string innerPayloadJson = JsonConvert.SerializeObject(innerPayload);

            InitialMessage payload = new InitialMessage
            {
                EphemeralKeyBase64           = Convert.ToBase64String(ephemeralKeyPair.XPublicKey),
                InnerMessagePayload          = innerPayloadJson,
                SenderIdentityKeyBase64      = Convert.ToBase64String(GlobalKeyStore.Instance.IdentityKeyPair.EdPublicKey),
                RecipientSignedPreKeyBase64  = Convert.ToBase64String(chatPackage.OtherSignedPreKey.XPublicKey),
                RecipientOneTimePreKeyBase64 = Convert.ToBase64String(chatPackage.OtherOneTimePreKey.XPublicKey)
            };

            string payloadJson = JsonConvert.SerializeObject(payload);

            return(new MetaMessage
            {
                OtherUuid = recipientUuid,
                Type = MessageType.InitialMessage,
                Payload = payloadJson
            });
        }