Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fileserver.client;
- import fileserver.util.Misc;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.security.NoSuchAlgorithmException;
- import java.util.List;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class Account {
- public String username;
- public String password;
- public List<String> fileNames;
- //Credentials are assumed to be correct at this stage, NO DOUBLE CHECKING
- public Account(String username, String password) {
- this.username = username;
- this.password = password;
- load(username, password);
- }
- private void load(String username, String password) {
- File file = new File("./" + username + "/files/");
- File[] files = file.listFiles();
- for (File f : files) {
- if (file.isFile()) {
- fileNames.add(f.getName());
- }
- }
- }
- public static void create(String username, String password) {
- File file = new File("./" + username + ".txt");
- File fileTwo = new File("./" + username + "/files/");
- try {
- file.createNewFile();
- fileTwo.createNewFile();
- FileOutputStream fos = new FileOutputStream(file);
- DataOutputStream dos = new DataOutputStream(fos);
- try {
- dos.writeUTF(Misc.hash(password));
- } catch (NoSuchAlgorithmException ex) {
- Logger.getLogger(Account.class.getName()).log(Level.SEVERE, null, ex);
- }
- dos.flush();
- dos.close();
- fos.close();
- } catch (IOException ex) {
- Logger.getLogger(Account.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static boolean canCreate(String username) {
- if (!new File("./" + username + ".txt").exists()) {
- return true;
- }
- return false;
- }
- public static boolean credentialsCheck(String username, String password) throws NoSuchAlgorithmException {
- File fi = new File("./" + username + ".txt");
- if (fi.exists()) {
- try {
- DataInputStream dis = new DataInputStream(new FileInputStream(fi));
- String passwordHash = Misc.hash(password);
- String passHash = dis.readUTF();
- if (passwordHash.equals(passHash)) {
- return true;
- }
- } catch (FileNotFoundException ex) {
- Logger.getLogger(Account.class.getName()).log(Level.SEVERE, null, ex);
- return true;
- } catch (IOException ex) {
- Logger.getLogger(Account.class.getName()).log(Level.SEVERE, null, ex);
- return false;
- }
- }
- return false;
- }
- /*
- * User txt files will just have password.
- * Files will be stored at "./username/files/"
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement