Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- /**
- * The input file for plain user data.
- */
- public static final String INPUT_FILE = "user.properties";
- /**
- * The output file for hashed user data.
- */
- public static final String OUTPUT_FILE = "newUser.properties";
- /**
- * The length of salt key.
- */
- public static final int LENGTH_OF_SALT = 10;
- /**
- * The max number of tries for password guessing.
- */
- public static final int MAX_CHANCES_COUNT = 3;
- public static void main(String[] args) {
- try {
- // check whether a user with a hashed password already exists
- if(new File(OUTPUT_FILE).isFile()) {
- HashMap<String, String> data = readDataFromFile(OUTPUT_FILE);
- if (data == null) {
- System.out.println("Data from " + OUTPUT_FILE + " cannot be read");
- System.exit(2);
- }
- String username = data.get("username");
- String password = data.get("password");
- String salt = data.get("salt");
- authenticate(username, password, salt);
- } else {
- // "register" new user with salted hash
- HashMap<String, String> data = readDataFromFile(INPUT_FILE);
- if (data == null) {
- System.out.println("data cannot be read");
- System.out.println("(file does not exist, is corrupted, "
- + "or readDataFromFile not implemented).");
- System.out.println("Exiting.");
- System.exit(1);
- }
- String username = data.get("username");
- String password = data.get("password");
- String salt = md5MyString("saltyRandomString").substring(0, LENGTH_OF_SALT);
- password = md5MyString(password + salt);
- boolean result = saveNewUserData(username, password, salt);
- if (!result) {
- System.out.println("Storing hashed data did not work properly.");
- System.exit(3);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Let's the user to try to authenticate with the given username.
- * The hashed password along with the salt is used to validate
- * the user guess. The user has MAX_CHANGED_COUNT number of tries
- * before the "game" ends.
- *
- * @param username The username to be tested.
- * @param password Hashed password.
- * @param salt The salt used with password.
- * @throws Exception simplifaction
- */
- public static void authenticate(String username, String password, String salt) throws Exception {
- @SuppressWarnings("resource")
- Scanner userInput = new Scanner(System.in);
- String guess;
- System.out.println("Hello, You now have " + MAX_CHANCES_COUNT + " chances to guess " + username + "'s password.");
- int chances;
- for (chances = MAX_CHANCES_COUNT - 1; chances >= 0; chances--) {
- guess = userInput.next();
- if (md5MyString(guess + salt).equals(password)) {
- System.out.println("That's correct!");
- break;
- } else if (chances > 0) {
- System.out.println("That's invalid! " + chances + " guess" + (chances > 1 ? "es" : "") + " left.");
- }
- }
- if (chances < 0)
- System.out.println("Sorry, You didn't guess " + username + "'s password");
- }
- /**
- * Reads all the properties from the given input file and returns
- * a key-value mapping of the contents in the file.
- * In case the file is corrupted or something goes wrong, returns null./wt
- * @param filename File to be read.
- * @return Hash map of settings in the file, NULL if something goes wrong.
- */
- public static HashMap<String, String> readDataFromFile(String filename) {
- Properties prop = new Properties();
- InputStream input = null;
- HashMap<String, String> Return = new HashMap<String, String>();
- try {
- input = new FileInputStream(filename);
- prop.load(input);
- System.out.println(prop.getProperty("username"));
- Return.put("username", prop.getProperty("username"));
- System.out.println(prop.getProperty("password"));
- Return.put("password", prop.getProperty("password"));
- if (prop.getProperty("salt") != null) {
- System.out.println(prop.getProperty("salt"));
- Return.put("salt", prop.getProperty("salt"));
- }
- return Return;
- } catch (IOException ex) {
- ex.printStackTrace();
- return null;
- } finally {
- if (input != null) {
- try {
- input.close();
- return Return;
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- }
- }
- }
- }
- /**
- * Writes username, password and salt values to
- * the corresponding keys (username, password, salt).
- * in the file OUTPUT_FILE.
- * In case the data is successfully stored, returns true.
- * If something goes wrong, returns false.
- * You have to use saveDataToFile method inside this method.
- * You have to modify the call to saveDataToFile in order to
- * pass the correct key-value map. The actual writing
- * will be done in saveDataToFile method.
- * @param username The value of username.
- * @param password The value of hashed password.
- * @param salt The value of salt.
- * @return true if data is stored, false otherwise.
- */
- public static boolean saveNewUserData(String username, String password, String salt) {
- // TODO: write your code here...
- // instead of null,
- // send user data (username, password, salt) to be saved
- HashMap<String, String> data = new HashMap<String, String>();
- data.put("username", username);
- data.put("password", password);
- data.put("salt", salt);
- return saveDataToFile(OUTPUT_FILE, data);
- }
- /**
- * Writes key-values from data into properties file which is given
- * by filename. If the file does not exist, the file is created.
- * @param filename The file name to be saved.
- * @param data The data to be stored in the file.
- * @return true, if the file is successfully stored, false otherwise.
- */
- public static boolean saveDataToFile(String filename, HashMap<String, String> data) {
- if (data == null) return false;
- try {
- Properties props = new Properties();
- props.setProperty("username", data.get("username"));
- props.setProperty("password", data.get("password"));
- props.setProperty("salt", data.get("salt"));
- if(new File(OUTPUT_FILE).isFile()) {
- OutputStream out = new FileOutputStream( filename );
- props.store(out, "Stored to file");
- return true;
- } else {
- File f = new File(filename);
- OutputStream out = new FileOutputStream( f );
- props.store(out, "Stored to file");
- return true;
- }
- } catch (Exception e ) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * Gets MD5 hash of the given string.
- * @param str The string to be hashed.
- * @return Hashed string
- * @throws Exception
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement