Advertisement
Guest User

Untitled

a guest
Jul 8th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutputStream;
  6. import java.io.Serializable;
  7. public class User implements Serializable{
  8.     private static final long serialVersionUID = 1337L;
  9.     private static final String FS = File.separator;
  10.     private final String name;
  11.     private final String password;
  12.     private File directory;
  13.     private File file;
  14.     public User(String name,String password){
  15.         this.name = name;
  16.         this.directory = new File("Users" + FS + this.name);
  17.         this.file = new File(this.directory + FS + this.name + ".ussr");
  18.         this.password = hash(password);
  19.     }
  20.     public static String getFS(){
  21.         return FS;
  22.     }
  23.     public String getName(){
  24.         return this.name;
  25.     }
  26.     public String getPassword(){
  27.         return this.password;
  28.     }
  29.     public File getDirectory(){
  30.         System.out.println(directory);
  31.         return this.directory;
  32.     }
  33.     protected final void write(){
  34.         try{
  35.             if(!this.file.exists()){
  36.                 this.file.getParentFile().mkdirs();
  37.                 this.file.createNewFile();
  38.             }
  39.             FileOutputStream fos = new FileOutputStream(this.file);
  40.             ObjectOutputStream oos = new ObjectOutputStream(fos);
  41.             oos.writeObject(this);
  42.             fos.close();
  43.             oos.close();
  44.         }
  45.         catch(Exception e){
  46.             e.printStackTrace();
  47.         }
  48.     }
  49.  
  50.     protected final User read(){
  51.         if(!this.file.exists()){
  52.             this.write();
  53.         }
  54.         try{
  55.             FileInputStream fis = new FileInputStream(this.file);
  56.             ObjectInputStream ois = new ObjectInputStream(fis);
  57.             User newUser = (User) ois.readObject();
  58.             fis.close();
  59.             ois.close();
  60.             return newUser;
  61.         }
  62.         catch(Exception e){
  63.             e.printStackTrace();
  64.         }
  65.         return null;
  66.     }
  67.  
  68.     protected final boolean checkPassword(){
  69.         User chck = this.read();
  70.         return (new EncryptPassword(getDirectory()).decrypt(this.getPassword()).equals(new EncryptPassword(getDirectory()).decrypt(chck.getPassword()))) ? true : false;
  71.        
  72.         //new system condensed
  73. /*      EncryptPassword EP = new EncryptPassword(getDirectory());
  74.         return (EP.decrypt(this.getPassword()).equals(EP.decrypt(chck.getPassword()))) ? true : false;*/
  75.        
  76.        
  77.         //new system expanded
  78. /*      if(EP.decrypt(this.getPassword()).equals(EP.decrypt(chck.getPassword()))){
  79.             return true;
  80.         }
  81.         else{
  82.             return false;
  83.         }*/
  84.        
  85.     //old system   
  86. /*      if(this.getPassword().equals(chck.getPassword())){
  87.             return true;
  88.         }
  89.         else{
  90.             return false;
  91.         }*/
  92.     }
  93.  
  94.     private final String hash(String pass) {
  95.         EncryptPassword EP = new EncryptPassword(new File(getDirectory() + FS + getName()));
  96.         return EP.encrypt(pass);
  97.        
  98.        
  99.         //old system
  100.         /*char[] charPass = new char[pass.length()];
  101.         int[] intPass = new int[pass.length()];
  102.         for(int i = 0; i < pass.length(); i++){
  103.             charPass[i] = pass.charAt(i);
  104.         }
  105.         for(int j = 0; j < pass.length(); j++){
  106.             intPass[j] = charPass[j];
  107.         }
  108.         pass = "";
  109.         for(int k: intPass){
  110.             pass += k;
  111.         }
  112.         return pass;*/
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement