Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. package fileserver.client;
  2.  
  3. import fileserver.util.Misc;
  4. import java.io.DataInputStream;
  5. import java.io.DataOutputStream;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.security.NoSuchAlgorithmException;
  12. import java.util.List;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. public class Account {
  17.  
  18. public String username;
  19. public String password;
  20. public List<String> fileNames;
  21.  
  22. //Credentials are assumed to be correct at this stage, NO DOUBLE CHECKING
  23. public Account(String username, String password) {
  24. this.username = username;
  25. this.password = password;
  26. load(username, password);
  27. }
  28.  
  29. private void load(String username, String password) {
  30. File file = new File("./" + username + "/files/");
  31. File[] files = file.listFiles();
  32. for (File f : files) {
  33. if (file.isFile()) {
  34. fileNames.add(f.getName());
  35. }
  36. }
  37. }
  38.  
  39. public static void create(String username, String password) {
  40. File file = new File("./" + username + ".txt");
  41. File fileTwo = new File("./" + username + "/files/");
  42. try {
  43. file.createNewFile();
  44. fileTwo.createNewFile();
  45. FileOutputStream fos = new FileOutputStream(file);
  46. DataOutputStream dos = new DataOutputStream(fos);
  47. try {
  48. dos.writeUTF(Misc.hash(password));
  49. } catch (NoSuchAlgorithmException ex) {
  50. Logger.getLogger(Account.class.getName()).log(Level.SEVERE, null, ex);
  51. }
  52. dos.flush();
  53. dos.close();
  54. fos.close();
  55. } catch (IOException ex) {
  56. Logger.getLogger(Account.class.getName()).log(Level.SEVERE, null, ex);
  57. }
  58. }
  59.  
  60. public static boolean canCreate(String username) {
  61. if (!new File("./" + username + ".txt").exists()) {
  62. return true;
  63. }
  64. return false;
  65. }
  66.  
  67. public static boolean credentialsCheck(String username, String password) throws NoSuchAlgorithmException {
  68. File fi = new File("./" + username + ".txt");
  69. if (fi.exists()) {
  70. try {
  71. DataInputStream dis = new DataInputStream(new FileInputStream(fi));
  72. String passwordHash = Misc.hash(password);
  73. String passHash = dis.readUTF();
  74. if (passwordHash.equals(passHash)) {
  75. return true;
  76. }
  77. } catch (FileNotFoundException ex) {
  78. Logger.getLogger(Account.class.getName()).log(Level.SEVERE, null, ex);
  79. return true;
  80. } catch (IOException ex) {
  81. Logger.getLogger(Account.class.getName()).log(Level.SEVERE, null, ex);
  82. return false;
  83. }
  84. }
  85. return false;
  86. }
  87. /*
  88. * User txt files will just have password.
  89. * Files will be stored at "./username/files/"
  90. */
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement