Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.Serializable;
- public class User implements Serializable{
- private static final long serialVersionUID = 1337L;
- private static final String FS = File.separator;
- private final String name;
- private final String password;
- private File directory;
- private File file;
- public User(String name,String password){
- this.name = name;
- this.directory = new File("Users" + FS + this.name);
- this.file = new File(this.directory + FS + this.name + ".ussr");
- this.password = hash(password);
- }
- public static String getFS(){
- return FS;
- }
- public String getName(){
- return this.name;
- }
- public String getPassword(){
- return this.password;
- }
- public File getDirectory(){
- System.out.println(directory);
- return this.directory;
- }
- protected final void write(){
- try{
- if(!this.file.exists()){
- this.file.getParentFile().mkdirs();
- this.file.createNewFile();
- }
- FileOutputStream fos = new FileOutputStream(this.file);
- ObjectOutputStream oos = new ObjectOutputStream(fos);
- oos.writeObject(this);
- fos.close();
- oos.close();
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- protected final User read(){
- if(!this.file.exists()){
- this.write();
- }
- try{
- FileInputStream fis = new FileInputStream(this.file);
- ObjectInputStream ois = new ObjectInputStream(fis);
- User newUser = (User) ois.readObject();
- fis.close();
- ois.close();
- return newUser;
- }
- catch(Exception e){
- e.printStackTrace();
- }
- return null;
- }
- protected final boolean checkPassword(){
- User chck = this.read();
- return (new EncryptPassword(getDirectory()).decrypt(this.getPassword()).equals(new EncryptPassword(getDirectory()).decrypt(chck.getPassword()))) ? true : false;
- //new system condensed
- /* EncryptPassword EP = new EncryptPassword(getDirectory());
- return (EP.decrypt(this.getPassword()).equals(EP.decrypt(chck.getPassword()))) ? true : false;*/
- //new system expanded
- /* if(EP.decrypt(this.getPassword()).equals(EP.decrypt(chck.getPassword()))){
- return true;
- }
- else{
- return false;
- }*/
- //old system
- /* if(this.getPassword().equals(chck.getPassword())){
- return true;
- }
- else{
- return false;
- }*/
- }
- private final String hash(String pass) {
- EncryptPassword EP = new EncryptPassword(new File(getDirectory() + FS + getName()));
- return EP.encrypt(pass);
- //old system
- /*char[] charPass = new char[pass.length()];
- int[] intPass = new int[pass.length()];
- for(int i = 0; i < pass.length(); i++){
- charPass[i] = pass.charAt(i);
- }
- for(int j = 0; j < pass.length(); j++){
- intPass[j] = charPass[j];
- }
- pass = "";
- for(int k: intPass){
- pass += k;
- }
- return pass;*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement