public static void StorePasswordInPendingTable(PasswordData passwordData, int destinationUserId)
        {
            var aesLogic = new AesLogic();

            var destinationUser = Context.Users.First(x => x.Id == destinationUserId);
            var sourceUser      = Context.Users.First(x => x.Login == UserName);
            var password        = Context.Passwords.First(x => x.Id == passwordData.Id);

            var encryptPassword = aesLogic.EncryptPassword(passwordData.Password, RandomData); //Get encrypted password

            var passwordSharesDb = new PendingPasswordSharesDb
            {
                PasswordHash      = encryptPassword,
                PasswordId        = passwordData.Id,
                Password          = password,
                SourceUserId      = sourceUser.Id,
                SourceUser        = sourceUser,
                DestinationUserId = destinationUserId,
                DestinationUser   = destinationUser,
                IsStale           = false,
                SharedPasswordId  = null
            };

            Context.PendingPasswordShares.Add(passwordSharesDb);

            Context.SaveChanges(); //save
        }
        public static string ReleasePassword(byte[] passwordHash, string keyValue)
        {
            var aesLogic = new AesLogic();

            var encryptPassword = aesLogic.DecryptPassword(passwordHash, keyValue);

            return(encryptPassword);
        }
        public static void EditPassword(EditPasswordData editPasswordData)
        {
            var aesLogic = new AesLogic();

            //update na password
            var password = Context.Passwords.FirstOrDefault(x => x.Id == editPasswordData.PasswordId);

            if (password == null)
            {
                return;
            }

            if (!string.IsNullOrEmpty(editPasswordData.NewLogin))
            {
                password.Login = editPasswordData.NewLogin;
            }

            if (!string.IsNullOrEmpty(editPasswordData.NewWebAddress))
            {
                password.WebAddress = editPasswordData.NewWebAddress;
            }

            if (!string.IsNullOrEmpty(editPasswordData.NewDescription))
            {
                password.Description = editPasswordData.NewDescription;
            }

            if (!string.IsNullOrEmpty(editPasswordData.NewPassword))
            {
                password.PasswordHash = aesLogic.EncryptPassword(editPasswordData.NewPassword, Password);
            }

            var passwordsIdsFromSharedPasswords = Context.PendingPasswordShares.Where(x => x.PasswordId == editPasswordData.PasswordId)
                                                  .Select(y => y.SharedPasswordId).ToList(); //From Passwords by received from SharedPasswordIds

            foreach (var passwordId in passwordsIdsFromSharedPasswords)
            {
                var passwordDb = Context.Passwords.FirstOrDefault(x => x.Id == passwordId);
                if (passwordDb != null)
                {
                    Context.Passwords.Remove(passwordDb);
                }

                var sharedPasswordDb =
                    Context.PendingPasswordShares.FirstOrDefault(x => x.SharedPasswordId == passwordId);
                if (sharedPasswordDb != null)
                {
                    Context.PendingPasswordShares.Remove(sharedPasswordDb);
                }
            }

            Context.SaveChanges();
        }
        public static void RecryptPasswordForUser(SecureString newUserPassword)
        {
            var aesLogic = new AesLogic();

            var user = Context.Users.First(x => x.Login == UserName);
            var userPasswordsData = Context.Passwords.Where(x => x.IdUser == user.Id).ToList();

            foreach (var userPasswordData in userPasswordsData)
            {
                var websitePassword = ReleasePassword(userPasswordData.PasswordHash);                       //decrypt website password

                userPasswordData.PasswordHash = aesLogic.EncryptPassword(websitePassword, newUserPassword); //update old password hash new one
            }

            Context.SaveChanges();
        }
        public static int StorePassword(PasswordData passwordData)
        {
            var aesLogic = new AesLogic();

            var user            = Context.Users.First(x => x.Login == UserName);
            var encryptPassword = aesLogic.EncryptPassword(passwordData.Password, Password); //Get encrypted password

            var passwordDb = new PasswordDb
            {
                Login        = passwordData.Login,
                PasswordHash = encryptPassword,
                WebAddress   = passwordData.WebAddress,
                Description  = passwordData.Description,
                User         = user,
                IdUser       = user.Id
            };

            Context.Passwords.Add(passwordDb);
            Context.SaveChanges();

            return(passwordDb.Id);
        }