private static async Task GetMessages(GetMessagesOptions opts)
        {
            var currentUser = ContactsExtensions.GetCurrentUser();

            var httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", currentUser.Token);

            var messages = await httpClient.GetFromJsonAsync <List <Message> >($"{Url}/messaging/messages");

            if (messages != null)
            {
                var keyManager = new KeyManager();
                var privateKey = keyManager.ReadPrivateKeyFromFile(Path.Combine(BasePath, $"{currentUser.Username}.data/{currentUser.Username}.private.pem"));

                foreach (var message in messages)
                {
                    var key = keyManager.RsaDecrypt(message.Key, privateKey);
                    var messagePlainText = keyManager.AesDecrypt(message.Content, key);

                    System.Console.WriteLine($"{ContactsExtensions.GetUserNameById(message.SenderId)}: {messagePlainText}");

                    if (opts.Debug)
                    {
                        System.Console.WriteLine("Received:");
                        System.Console.WriteLine(JsonConvert.SerializeObject(messages));
                    }
                }
            }
            else
            {
                throw new Exception("messaging/messages failed");
            }
        }
        private static string GetUserPublicKeyByName(string userName)
        {
            var currentUser = ContactsExtensions.GetCurrentUser();

            var users = JsonConvert.DeserializeObject <List <UserDetail> >(
                File.ReadAllText(Path.Combine(BasePath, $"{currentUser.Username}.data/contacts.data")));

            var user = users.FirstOrDefault(u => u.Username == userName);

            return(user.PublicKey);
        }
        private static async Task LoadContacts()
        {
            var currentUser = ContactsExtensions.GetCurrentUser();

            var path = Path.Combine(BasePath, $"{currentUser.Username}.data/contacts.data");

            var httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", currentUser.Token);

            var users = await httpClient.GetFromJsonAsync <List <UserDetail> >($"{Url}/messaging/list");

            File.WriteAllText(Path.Combine(BasePath, $"{currentUser.Username}.data/contacts.data"), JsonConvert.SerializeObject(users));
        }
        private static async Task DisplayUsers()
        {
            var currentUser = ContactsExtensions.GetCurrentUser();

            await LoadContacts();

            var users = JsonConvert.DeserializeObject <List <UserDetail> >(File.ReadAllText(Path.Combine(BasePath, $"{currentUser.Username}.data/contacts.data")));

            System.Console.WriteLine("Users: ");
            foreach (var user in users)
            {
                System.Console.WriteLine(user.Username);
            }
        }
        private static async Task Send(SendOptions opts)
        {
            var currentUser = ContactsExtensions.GetCurrentUser();

            if (opts.To == currentUser.Username)
            {
                System.Console.WriteLine("Cannot send message to yourself");
                return;
            }

            var keyManager = new KeyManager();

            var recipientPublicKeyString = GetUserPublicKeyByName(opts.To);

            var publicKey = keyManager.LoadPublicKey(recipientPublicKeyString);

            var(encryptedMessage, aesKey) = keyManager.AesEncrypt(opts.Message);

            var encryptedKey = keyManager.RsaEncrypt(aesKey, publicKey);

            var httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", currentUser.Token);

            var message = new Message()
            {
                Key         = encryptedKey,
                Content     = encryptedMessage,
                SenderId    = currentUser.UserId,
                RecipientId = ContactsExtensions.GetUserIdByName(opts.To)
            };

            if (opts.Debug)
            {
                System.Console.WriteLine("Data sent:");
                System.Console.WriteLine(JsonConvert.SerializeObject(message));
            }

            var postRequest = new HttpRequestMessage(HttpMethod.Post, new Uri($"{Url}/messaging/send"))
            {
                Content = JsonContent.Create(message)
            };

            var response = await httpClient.SendAsync(postRequest);

            response.EnsureSuccessStatusCode();
        }