Esempio n. 1
0
 public static String getPasswordFromFile() {
   // String hashPassSaved = null;
   ObjectInputStream ois; // = null;
   try {
     ois = new ObjectInputStream(new FileInputStream(new File(ApplicationData.passwordFile)));
     hashPassSaved = (String) (ois.readObject());
     ois.close();
   } catch (IOException ex) {
     ApplicationData.exitWithError(ApplicationData.ERROR_26);
   } catch (ClassNotFoundException ex) {
     ApplicationData.exitWithError(ApplicationData.ERROR_15);
     // Logger.getLogger(AccessPassword.class.getName()).log(Level.SEVERE, null, ex);
   }
   return hashPassSaved;
 }
Esempio n. 2
0
  /*
     private static void lockPort(int p){
         ServerSocket s;
  // static initializer
         try {
             s = new ServerSocket(p, 10, InetAddress.getLocalHost());
             D.deb("Porta bloqueada com sucesso: "+p);
         } catch (UnknownHostException e) {
                 // shouldn't happen for localhost
             D.deb("Porta não bloqueada - Unknown!");
         } catch (IOException e) {
             D.deb("Porta não bloqueada - IO error");
                 // port taken, so app is already running
         }
     }
     */
  public static void changePassword() {
    String userConfPass; // = null;
    // String hashUserConfPass = null;
    boolean passVal = false;

    while (!passVal) {
      try {
        userPass = askUserPass("Introduza Nova Password");
        userConfPass = askUserPass("Confirme a password");
        hashUserConfPass = createHash(userConfPass);
        passVal = validatePassword(userPass, hashUserConfPass);
      } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) {
        ApplicationData.exitWithError(ApplicationData.ERROR_13);
        // Logger.getLogger(AccessPassword.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    /*
    if (fileExists(ApplicationData.passwordFile)){
        ApplicationData.OLD_KEY = AccessPassword.getPasswordFromFile();
    }
    */
    setPasswordToFile();
    ApplicationData.KEY = userPass;
    ApplicationData.HASHED_SAVED_PASSWORD = hashUserConfPass;
    D.deb("valor de userPass: "******"valor de hashUserConfPass: "******"valor de userPass: "******"valor de hashUserConfPass: " + hashUserConfPass);
  }
Esempio n. 3
0
 public static void setPasswordToFile() {
   ObjectOutputStream oos; // = null;
   try {
     oos = new ObjectOutputStream(new FileOutputStream(new File(ApplicationData.passwordFile)));
     oos.writeObject(hashUserConfPass);
     oos.close();
   } catch (IOException ex) {
     D.deb("Erro 14: Path do ficheiro: " + ApplicationData.passwordFile);
     ApplicationData.exitWithError(ApplicationData.ERROR_14);
     // Logger.getLogger(AccessPassword.class.getName()).log(Level.SEVERE, null, ex);
   }
 }
Esempio n. 4
0
 private int getPortFromFileRunText() {
   int p = 65110; // never used!
   try {
     FileReader fr = new FileReader(ApplicationData.fileRun);
     BufferedReader br = new BufferedReader(fr);
     p = Integer.parseInt(br.readLine());
     br.close();
   } catch (IOException ex) {
     ApplicationData.exitWithError(ApplicationData.ERROR_30);
     // Logger.getLogger(ApplicationData.class.getName()).log(Level.SEVERE, null, ex);
   }
   D.deb("Porta lida de fileRun: " + p);
   return p;
 }
Esempio n. 5
0
 private int getPortFromFileRun() {
   int p = 65110; // never used!
   try {
     FileInputStream fis = new FileInputStream(ApplicationData.fileRun);
     DataInputStream dis = new DataInputStream(fis);
     p = dis.readInt();
     dis.close();
   } catch (IOException ex) {
     ApplicationData.exitWithError(ApplicationData.ERROR_30);
     // Logger.getLogger(ApplicationData.class.getName()).log(Level.SEVERE, null, ex);
   }
   D.deb("Porta lida de fileRun: " + p);
   return p;
 }
Esempio n. 6
0
  public AccessPassword() {
    // String userPass= askUserPass(false);// true if want to confirm password
    if (fileExists(ApplicationData.fileRun)) {
      // tests for file1.run that marks app is running
      // file exists, EXIT, and warns user tha is allready running
      ApplicationData.port = getPortFromFileRunText();

      if (isRunning(ApplicationData.port)) {
        JOptionPane.showMessageDialog(
            null,
            "Aviso: A aplicação já se encontra em execução!",
            "ATENÇÃO!",
            JOptionPane.WARNING_MESSAGE);
        System.exit(0);
        // exitWithError(ApplicationData.ERROR_27);

      } else {
        JOptionPane.showMessageDialog(
            null,
            "Aviso: A aplicação recuperou de falha grave! Tente novamente!",
            "ATENÇÃO!",
            JOptionPane.WARNING_MESSAGE);
        ApplicationData.deleteFileRun();
        System.exit(0);
        // ApplicationData.makeFileRun(ApplicationData.defaultPort);
      }

    } else {

      if (isRunning(ApplicationData.defaultPort)) {
        JOptionPane.showMessageDialog(
            null,
            "Aviso: A aplicação já se encontra em execução!",
            "ATENÇÃO!",
            JOptionPane.WARNING_MESSAGE);
        System.exit(0);
      }
      // lockPort(defaultPort);
      // creates file1.run
      ApplicationData.makeFileRunText(ApplicationData.defaultPort);
      if (fileExists(ApplicationData.dataFileEncrypted)) {
        if (fileExists(ApplicationData.passwordFile)) {
          hashPassSaved = getPasswordFromFile();
          userPass = askUserPass("Password");
          D.deb("Password from file: \"" + hashPassSaved + "\"");
          try {
            // compara a password com a hash carregada
            if (!validatePassword(userPass, hashPassSaved)) {
              // ApplicationData.deleteFileRun();
              JOptionPane.showMessageDialog(
                  null, "Password incorrecta!", "ATENÇÃO!", JOptionPane.WARNING_MESSAGE);
              ApplicationData.deleteFileRun();
              System.exit(0);
              // ApplicationData.exitWithError(20);
              // System.exit(0);
            }
          } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) {
            ApplicationData.exitWithError(ApplicationData.ERROR_16);
            Logger.getLogger(AccessPassword.class.getName()).log(Level.SEVERE, null, ex);
          }
          ApplicationData.HASHED_SAVED_PASSWORD = hashPassSaved;
          D.deb("Arranque normal: hash from file: \"" + ApplicationData.HASHED_SAVED_PASSWORD);
        } else {
          ApplicationData.exitWithError(ApplicationData.ERROR_11);
        }

      } else {
        // se ficheiro de dados (encryp) não esxiste, então:
        // pede nova password (muda password) e grava
        changePassword();
        JOptionPane.showMessageDialog(null, "Nova password definida!");
      }
      ApplicationData.KEY = userPass;
    }
  }