Advertisement
Guest User

EX16

a guest
Apr 27th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.38 KB | None | 0 0
  1. public class Main {
  2.  
  3. /**
  4. * The input file for plain user data.
  5. */
  6. public static final String INPUT_FILE = "user.properties";
  7. /**
  8. * The output file for hashed user data.
  9. */
  10. public static final String OUTPUT_FILE = "newUser.properties";
  11. /**
  12. * The length of salt key.
  13. */
  14. public static final int LENGTH_OF_SALT = 10;
  15. /**
  16. * The max number of tries for password guessing.
  17. */
  18. public static final int MAX_CHANCES_COUNT = 3;
  19.  
  20.  
  21. public static void main(String[] args) {
  22. try {
  23. // check whether a user with a hashed password already exists
  24. if(new File(OUTPUT_FILE).isFile()) {
  25. HashMap<String, String> data = readDataFromFile(OUTPUT_FILE);
  26. if (data == null) {
  27. System.out.println("Data from " + OUTPUT_FILE + " cannot be read");
  28. System.exit(2);
  29. }
  30. String username = data.get("username");
  31. String password = data.get("password");
  32. String salt = data.get("salt");
  33. authenticate(username, password, salt);
  34. } else {
  35. // "register" new user with salted hash
  36. HashMap<String, String> data = readDataFromFile(INPUT_FILE);
  37. if (data == null) {
  38. System.out.println("data cannot be read");
  39. System.out.println("(file does not exist, is corrupted, "
  40. + "or readDataFromFile not implemented).");
  41. System.out.println("Exiting.");
  42. System.exit(1);
  43. }
  44. String username = data.get("username");
  45. String password = data.get("password");
  46. String salt = md5MyString("saltyRandomString").substring(0, LENGTH_OF_SALT);
  47. password = md5MyString(password + salt);
  48. boolean result = saveNewUserData(username, password, salt);
  49. if (!result) {
  50. System.out.println("Storing hashed data did not work properly.");
  51. System.exit(3);
  52. }
  53. }
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. }
  57. }
  58.  
  59. /**
  60. * Let's the user to try to authenticate with the given username.
  61. * The hashed password along with the salt is used to validate
  62. * the user guess. The user has MAX_CHANGED_COUNT number of tries
  63. * before the "game" ends.
  64. *
  65. * @param username The username to be tested.
  66. * @param password Hashed password.
  67. * @param salt The salt used with password.
  68. * @throws Exception simplifaction
  69. */
  70. public static void authenticate(String username, String password, String salt) throws Exception {
  71. @SuppressWarnings("resource")
  72. Scanner userInput = new Scanner(System.in);
  73. String guess;
  74. System.out.println("Hello, You now have " + MAX_CHANCES_COUNT + " chances to guess " + username + "'s password.");
  75. int chances;
  76. for (chances = MAX_CHANCES_COUNT - 1; chances >= 0; chances--) {
  77. guess = userInput.next();
  78. if (md5MyString(guess + salt).equals(password)) {
  79. System.out.println("That's correct!");
  80. break;
  81. } else if (chances > 0) {
  82. System.out.println("That's invalid! " + chances + " guess" + (chances > 1 ? "es" : "") + " left.");
  83. }
  84. }
  85. if (chances < 0)
  86. System.out.println("Sorry, You didn't guess " + username + "'s password");
  87. }
  88.  
  89. /**
  90. * Reads all the properties from the given input file and returns
  91. * a key-value mapping of the contents in the file.
  92. * In case the file is corrupted or something goes wrong, returns null./wt
  93. * @param filename File to be read.
  94. * @return Hash map of settings in the file, NULL if something goes wrong.
  95. */
  96. public static HashMap<String, String> readDataFromFile(String filename) {
  97.  
  98. Properties prop = new Properties();
  99. InputStream input = null;
  100. HashMap<String, String> Return = new HashMap<String, String>();
  101.  
  102. try {
  103. input = new FileInputStream(filename);
  104. prop.load(input);
  105. System.out.println(prop.getProperty("username"));
  106. Return.put("username", prop.getProperty("username"));
  107.  
  108. System.out.println(prop.getProperty("password"));
  109. Return.put("password", prop.getProperty("password"));
  110. if (prop.getProperty("salt") != null) {
  111. System.out.println(prop.getProperty("salt"));
  112. Return.put("salt", prop.getProperty("salt"));
  113. }
  114. return Return;
  115. } catch (IOException ex) {
  116. ex.printStackTrace();
  117. return null;
  118. } finally {
  119. if (input != null) {
  120. try {
  121. input.close();
  122. return Return;
  123. } catch (IOException e) {
  124. e.printStackTrace();
  125. return null;
  126. }
  127. }
  128. }
  129. }
  130. /**
  131. * Writes username, password and salt values to
  132. * the corresponding keys (username, password, salt).
  133. * in the file OUTPUT_FILE.
  134. * In case the data is successfully stored, returns true.
  135. * If something goes wrong, returns false.
  136. * You have to use saveDataToFile method inside this method.
  137. * You have to modify the call to saveDataToFile in order to
  138. * pass the correct key-value map. The actual writing
  139. * will be done in saveDataToFile method.
  140. * @param username The value of username.
  141. * @param password The value of hashed password.
  142. * @param salt The value of salt.
  143. * @return true if data is stored, false otherwise.
  144. */
  145. public static boolean saveNewUserData(String username, String password, String salt) {
  146. // TODO: write your code here...
  147. // instead of null,
  148. // send user data (username, password, salt) to be saved
  149. HashMap<String, String> data = new HashMap<String, String>();
  150. data.put("username", username);
  151. data.put("password", password);
  152. data.put("salt", salt);
  153. return saveDataToFile(OUTPUT_FILE, data);
  154. }
  155.  
  156. /**
  157. * Writes key-values from data into properties file which is given
  158. * by filename. If the file does not exist, the file is created.
  159. * @param filename The file name to be saved.
  160. * @param data The data to be stored in the file.
  161. * @return true, if the file is successfully stored, false otherwise.
  162. */
  163. public static boolean saveDataToFile(String filename, HashMap<String, String> data) {
  164. if (data == null) return false;
  165. try {
  166. Properties props = new Properties();
  167. props.setProperty("username", data.get("username"));
  168. props.setProperty("password", data.get("password"));
  169. props.setProperty("salt", data.get("salt"));
  170. if(new File(OUTPUT_FILE).isFile()) {
  171. OutputStream out = new FileOutputStream( filename );
  172. props.store(out, "Stored to file");
  173. return true;
  174. } else {
  175. File f = new File(filename);
  176. OutputStream out = new FileOutputStream( f );
  177. props.store(out, "Stored to file");
  178. return true;
  179. }
  180. } catch (Exception e ) {
  181. e.printStackTrace();
  182. return false;
  183. }
  184. }
  185.  
  186. /**
  187. * Gets MD5 hash of the given string.
  188. * @param str The string to be hashed.
  189. * @return Hashed string
  190. * @throws Exception
  191. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement