public static void saveHashes(String path, HashMap<String, String> hashes) { if (path.equalsIgnoreCase("")) { MLog.e("Can't save Hashes to not given file."); return; } File iFile = new File(path); FileOutputStream fos = null; ObjectOutputStream oos = null; if (!iFile.exists()) { MLog.e("Can't save Hashes to not existing file: " + path); return; } if (hashes == null) MLog.w("Writing 'null' to hash database means clearing."); try { fos = new FileOutputStream(iFile); oos = new ObjectOutputStream(fos); } catch (FileNotFoundException fnfex) { MLog.e("Can't save Hashes to not existing file: " + path); return; } catch (IOException ioex) { MLog.e("Can't create ObjectOutputStream while saving Hashes from " + path); return; } try { oos.writeObject(hashes); } catch (IOException ioex) { MLog.e("Error writing Stream in file: " + path); return; } // catch (ClassNotFoundException cnfex) { MLog.e("Error writing Stream in file // (ClassNotFoundException): " + path); return; } MLog.d("Saved Hashmap successfully"); return; }
/** * Checks if player is registered with the given password * * @param p The Player * @param pass The Password * @return true: Player registered and correct password, false: Anything went wrong, or no access */ public boolean b(Player p, String pass) { if (!loaded) { MLog.e("(MAuth) Can't do check on " + p.getName() + ": HashMaps are not loaded."); return false; } if (!isRegistered(p.getName())) { MLog.e("(MAuth) Can't check player " + p.getName() + ": Never registered."); } return d(p.getName()) .equals( MCrypt.getHash( 1000, pass, "i8765rtghjklo987654redfghjukiloi8u7z654e34r56789ikjhgf87654rfghzjui876tghjkioi8u7z6trer456z7uj")); }
/** * Registers a new player. * * @param p The Player * @param pass The password * @return SUCCESS: All did fine, ERROR: Something went wrong. Check log. */ public MResult register(Player p, String pass) { if (c.contains(p.getName()) && !b(p, pass)) return MResult.RES_NOACCESS; if (a.containsKey(p.getName())) return MResult.RES_ALREADY; if (c(p, pass)) return MResult.RES_SUCCESS; MLog.e("Something went wrong while registering new player :("); return MResult.RES_ERROR; }
private boolean c(Player p, String pass) { if (!loaded) { MLog.e("(MAuth) Can't register player " + p.getName() + ": HashMaps are not initialized."); return false; } if (isRegistered(p.getName())) { MLog.e("(MAuth) Can't register player " + p.getName()); return false; } a.put( p.getName(), MCrypt.getHash( 1000, pass, "i8765rtghjklo987654redfghjukiloi8u7z654e34r56789ikjhgf87654rfghzjui876tghjkioi8u7z6trer456z7uj")); MCrypt.saveHashes(b.getAbsolutePath(), a); return true; }
public static String getHash(int iterationNb, String password, String salt) { try { MessageDigest digest = MessageDigest.getInstance("SHA-1"); digest.reset(); digest.update(salt.getBytes()); byte[] input; try { input = digest.digest(password.getBytes("UTF-8")); for (int i = 0; i < iterationNb; i++) { digest.reset(); input = digest.digest(input); } return new String(input); } catch (UnsupportedEncodingException ex) { MLog.e("Failure while doing crypt algorithm."); return ""; } } catch (NoSuchAlgorithmException ex) { MLog.e("Failure while doing crypt algorithm."); return ""; } }
public MAuthorizer(String a) { try { b = new File(a); this.a = new HashMap<>(); this.c = new ArrayList<>(); if (!b.exists()) { b.mkdirs(); b.createNewFile(); MCrypt.saveHashes(a, this.a); } } catch (IOException ioex) { MLog.e("(MAuth) Authorization file invalid."); } }
public static HashMap<String, String> loadHashes(String path) { if (path.equalsIgnoreCase("")) { MLog.e("Can't load Hases from not given file"); return null; } File iFile = new File(path); FileInputStream fis = null; ObjectInputStream ois = null; if (!iFile.exists()) { MLog.e("Can't load Hashes from not existing file: " + path); return null; } try { fis = new FileInputStream(iFile); ois = new ObjectInputStream(fis); } catch (FileNotFoundException fnfex) { MLog.e("Can't load Hashes from not existing file: " + path); return null; } catch (EOFException eofex) { HashMap<String, String> tres = new HashMap<String, String>(); saveHashes(path, tres); return tres; } catch (IOException ioex) { MLog.e("Can't create ObjectInputStream while loading Hashes from " + path); ioex.printStackTrace(); return null; } Object o = null; try { o = ois.readObject(); } catch (IOException ioex) { MLog.e("Error reading Stream in file: " + path); return null; } catch (ClassNotFoundException cnfex) { MLog.e("Error reading Stream in file (ClassNotFoundException): " + path); return null; } if (!(o instanceof HashMap)) { MLog.e("Invalid hash file at: " + path); return null; } MLog.d("Loaded Hashmap successfully"); return (HashMap<String, String>) o; }