Пример #1
0
        // Creates mainUser and goes to convo screen
        void signInToServerResponse(object sender, EventArgs e)
        {
            int messageIndex = serverConnection.unreadMessages.Count - 1;

            if (messageIndex < 0)
            {
                return;
            }
            JObject message = JObject.Parse(serverConnection.unreadMessages[messageIndex]);
            string  type    = serverConnection.interpretMessageType(message);

            if (type == "signedIn")
            {
                User newUser = new User();
                newUser.username = message[type]["username"].ToString();
                newUser.email    = message[type]["email"].ToString();
                newUser.user_id  = message[type]["id"].ToString();
                newUser.pubKey   = message[type]["pubKey"].ToString();

                // Load storage. If not same user, reset
                loadChatsAndUsersFromStorage();
                if (mainUser == null || newUser.user_id != mainUser.user_id)
                {
                    // Deletes all storage
                    SecureStorageHelper ssHelper = new SecureStorageHelper();
                    ssHelper.RemoveAllItems();

                    // Loads new users and resets memory of users&chats
                    mainUser          = newUser;
                    otherUsers        = new Dictionary <string, User> {
                    };                                             // username:user
                    usernameIdMatches = new Dictionary <string, string> {
                    };                                             //user_id:username
                    myChats           = new Dictionary <string, Chat> {
                    };                                             // chatname:chat
                    chatNameMatches   = new Dictionary <string, string> {
                    };                                             // chat_id:chatname

                    // Creates a new public key and updates server
                    AsymmetricKeyHelper akh = new AsymmetricKeyHelper(myAsymKeyPairAlias + mainUser.username);
                    akh.CreateKey();
                    string pubKey = akh.GetSharablePublicKey();
                    updatePubKey(pubKey);
                }


                // Post sign-in activities
                getAllUsers();
                getMyChats();
            }

            if (type == "noAccount")
            {
                RunOnUiThread(() =>
                {
                    createAccountScreen();
                });
            }
        }
Пример #2
0
        void createUser(string username)
        {
            AsymmetricKeyHelper akh = new AsymmetricKeyHelper(myAsymKeyPairAlias + username);

            akh.CreateKey();
            string pubKey = akh.GetSharablePublicKey();

            string message = "{\"access_id\": \"" + this.access_id + "\", \"username\": \"" + username + "\", \"pubKey\": \"" + pubKey + "\"}";

            serverConnection.WriteMessage("createUser", message);
        }
Пример #3
0
        void inviteUsersToChat(string chatName, string username)
        {
            string chatId          = myChats[chatName].chatId;
            string joinerId        = otherUsers[username].user_id;
            string joinerPubKey    = otherUsers[username].pubKey;
            string symKeyEncrypted = myChats[chatName].getSharableKey();

            // User's pub key is used to encrypt symmetric key
            AsymmetricKeyHelper asymKeyHelper = new AsymmetricKeyHelper("otherUserKey");

            symKeyEncrypted = asymKeyHelper.EncryptWithAnotherPublicKey(symKeyEncrypted, joinerPubKey);

            string message = "{\"access_id\": \"" + this.access_id + "\", \"chatId\": \"" + chatId + "\", \"symKey\": \"" + symKeyEncrypted + "\", \"joinerId\": \"" + joinerId + "\"}";

            serverConnection.WriteMessage("allowUserToJoinChat", message);
        }
Пример #4
0
        // Adds newly created chat to our list
        void proccessNewChat(object sender, EventArgs e)
        {
            int messageIndex = serverConnection.unreadMessages.Count - 1;

            if (messageIndex < 0)
            {
                return;
            }
            JObject message = JObject.Parse(serverConnection.unreadMessages[messageIndex]);
            string  type    = serverConnection.interpretMessageType(message);

            if (type == "chatCreated")
            {
                // Add new chat to our list
                Chat aChat = new Chat(message[type].ToString());

                // Creates sym key
                aChat.createSymKey();

                myChats[aChat.chatName]       = aChat;
                chatNameMatches[aChat.chatId] = aChat.chatName;

                // Invite Users
                string[] usersToInvite = FindViewById <EditText>(Resource.Id.chatInvites).Text.Split(',');
                for (int i = 0; i < usersToInvite.Length; i++)
                {
                    if (usersToInvite[i] == "")
                    {
                        continue;
                    }
                    inviteUsersToChat(aChat.chatName, usersToInvite[i]);
                }


                RunOnUiThread(() =>
                {
                    convoScreen();
                });
            }

            if (type == "acceptedToChat")
            {
                Chat   aChat       = new Chat(message[type].ToString());
                string givenSymKey = message[type]["symKey"].ToString();

                // Decrypts encrypted symkey
                AsymmetricKeyHelper asymKeyHelper = new AsymmetricKeyHelper(myAsymKeyPairAlias + mainUser.username);
                var symKey = asymKeyHelper.DecryptDataFromString(givenSymKey);

                aChat.loadChatKey(symKey);
                myChats[aChat.chatName]       = aChat;
                chatNameMatches[aChat.chatId] = aChat.chatName;
                getNewMessages(aChat);

                if (currentView == "convoScreen")
                {
                    RunOnUiThread(() =>
                    {
                        convoScreen();
                    });
                }
            }
        }