Esempio n. 1
0
        public void Decrypt(IUser user)
        {
            if (!IsEncrypt)
            {
                return;
            }

            if (!IsDownloaded)
            {
                throw new ArgumentException("An undownloaded message cannot decrypt");
            }

            if (!user.IsAuthorized)
            {
                throw new ArgumentException("User is unauthorized, message cannot decrypt");
            }

            ISecureManager secureManager = ModulesManager.GetSecureManager();

            string password = secureManager.DecryptWithAes(user.EncryptedPassword);

            if (type != StashMessageType.Empty)
            {
                content = secureManager.DecryptWithAesHmac(content, password);
            }

            IsEncrypt = false;
        }
Esempio n. 2
0
        public void CreateUser(long chatId, string password)
        {
            ISecureManager secureManager = ModulesManager.GetSecureManager();

            if (IsUserExist(chatId))
            {
                LogoutUser(chatId);

                using (UsersContext db = new UsersContext())
                {
                    UserModel userModel = db.Users
                                          .Where(user => user.ChatId == chatId)
                                          .First();

                    userModel.HashPassword = secureManager.CalculateHash(password);
                    db.SaveChanges();
                }
            }
            else
            {
                using (UsersContext db = new UsersContext())
                {
                    UserModel userModel = new UserModel
                    {
                        ChatId       = chatId,
                        HashPassword = secureManager.CalculateHash(password)
                    };

                    db.Users.Add(userModel);
                    db.SaveChanges();
                }
            }
        }
Esempio n. 3
0
        public void Login(string password)
        {
            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentException("Password cannot be null");
            }

            ISecureManager secureManager = ModulesManager.GetSecureManager();

            EncryptedPassword = secureManager.EncryptWithAes(password);
            IsAuthorized      = true;
        }
Esempio n. 4
0
        internal UserLocal(long chatId, string password)
        {
            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentException("Password cannot be null");
            }

            ISecureManager secureManager = ModulesManager.GetSecureManager();

            ChatId            = chatId;
            IsAuthorized      = false;
            hashPassword      = secureManager.CalculateHash(password);
            EncryptedPassword = null;
        }
Esempio n. 5
0
        public bool ValidatePassword(string password)
        {
            ISecureManager secureManager = ModulesManager.GetSecureManager();

            return(secureManager.CompareWithHash(password, hashPassword));
        }