private void createNewPwdFile(EncryptedPassword passwordForEncoding, File pwdFile)
      throws Exception {
    if (pwdFile.exists()) {
      pwdFile.delete();
    }

    byte[] encr = encodeService.encrypt(passwordForEncoding, passwordForEncoding.getPassword());
    FileUtils.writeByteArrayToFile(pwdFile, encr);
  }
  public boolean Login(PlainTextPassword pp, String userName) {
    EncryptedPassword pwd1 = null;

    PieUser user = userService.getUser();

    if (user == null || user.getPieShareConfiguration() == null) {
      return false;
    }

    File pwdFile = user.getPieShareConfiguration().getPwdFile();

    if (pp != null) {
      pwd1 = passwordEncryptionService.encryptPassword(pp);
      pp = null;
    } else {
      try {
        File unencryptedFile =
            new File(
                String.format(
                    unsafeFile, user.getPieShareConfiguration().getPwdFile().getCanonicalPath()));
        if (!unencryptedFile.exists()) {
          return false;
        }
        pwd1 =
            passwordEncryptionService.getEncryptedPasswordFromExistingSecretKey(
                FileUtils.readFileToByteArray(unencryptedFile));

        File ivF = new File(user.getPieShareConfiguration().getPwdFile().getParent(), ivFile);
        if (ivF.exists()) {
          pwd1.setIv(FileUtils.readFileToByteArray(ivF));
        }
      } catch (IOException e) {
        PieLogger.error(
            this.getClass(), String.format("error during auto login! Message: %s", e.getMessage()));
        return false;
      }
    }
    pwd1.setUseIv(useIv);

    if (pwdFile.exists()) {
      try {
        if (!Arrays.equals(
            encodeService.decrypt(pwd1, FileUtils.readFileToByteArray(pwdFile)),
            pwd1.getPassword())) {
          return false; // throw new WrongPasswordException("The given password was wrong.");
        }

        File unencryptedFile =
            new File(
                String.format(
                    unsafeFile, user.getPieShareConfiguration().getPwdFile().getCanonicalPath()));
        FileUtils.writeByteArrayToFile(unencryptedFile, pwd1.getPassword());

        if (pwd1.getIv() != null) {
          FileUtils.writeByteArrayToFile(
              new File(
                  userService.getUser().getPieShareConfiguration().getPwdFile().getParent(),
                  ivFile),
              pwd1.getIv());
        }

      } catch (Exception ex) {
        PieLogger.info(
            this.getClass(),
            String.format(
                "Wrong password, not possible to encrypt file! Messgae %s", ex.getMessage()));
        return false;
      }
    } else {
      PieLogger.info(this.getClass(), "Tried to login but no passwordFile was avaliable!");
      return false;
    }

    if (userService.getUser().getUserName() == null && userName != null) {
      userService.getUser().setUserName(userName);
    }

    databaseService.mergePieUser(userService.getUser());

    user.setPassword(pwd1);
    user.setHasPasswordFile(true);
    user.setIsLoggedIn(true);

    SymmetricEncryptedChannel channel = this.symmetricEncryptedChannelProvider.get();
    channel.setChannelId(user.getUserName());
    channel.setEncPwd(user.getPassword());

    try {
      this.clusterManagementService.registerChannel(user.getCloudName(), channel);

    } catch (ClusterManagmentServiceException e) {
      PieLogger.error(
          this.getClass(),
          String.format("Error in Register Channel. Message:  %s", e.getMessage()));
    }

    PieLogger.info(this.getClass(), "Login Successful");

    this.historyService.syncLocalFilders();
    // todo-mr3: now we have to send a list with everything

    // todo-after-ase: ultimatively instead of making an initial sync we have to perform a list
    // request when ever
    // the cluster state changes
    try {
      // todo: change this maybe in future to different aproach
      // should probably be coupled with network chang detection!
      // this is needed to recognize local changes on this node
      IFileListMessage fileList = this.messageFactoryService.getFileListMessage();
      fileList.getAddress().setClusterName(userService.getUser().getCloudName());
      fileList.getAddress().setChannelId(userService.getUser().getUserName());
      fileList.setFileList(this.historyService.getPieFiles());
      fileList.setFolderList(this.historyService.getPieFolders());
      this.clusterManagementService.sendMessage(fileList);

      // send file list request message to cluster
      FileListRequestMessage msg = this.messageFactoryService.getFileListRequestMessage();
      msg.getAddress().setClusterName(userService.getUser().getCloudName());
      msg.getAddress().setChannelId(userService.getUser().getUserName());
      this.clusterManagementService.sendMessage(msg);
    } catch (ClusterManagmentServiceException ex) {
      PieLogger.error(this.getClass(), "Connect failed!", ex);
    }

    return true;
  }