Guest User

Untitled

a guest
Jul 20th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.54 KB | None | 0 0
  1. package prometheus.server;
  2.  
  3. /*
  4.  * Author: Sajid Anwar
  5.  * Program: Prometheus
  6.  * Date: 3/28/2012
  7.  * School: Ironwood High School
  8.  */
  9.  
  10. import java.io.*;
  11.  
  12. import prometheus.util.Md5Util;
  13.  
  14. public class AuthenticationDatabase {
  15.    
  16.     private static RandomAccessFile _database;
  17.    
  18.     private static String FILE_PATH;
  19.     private static final int SIZE_USERNAME = 36;
  20.     private static final int SIZE_PASSWORDHASH = 16;
  21.     private static final int SIZE_RECORD = SIZE_USERNAME + SIZE_PASSWORDHASH;
  22.  
  23.     /*
  24.      *      username = {36 bytes}
  25.      *      password = {16 bytes}
  26.      *      record = [username][password]
  27.      *
  28.      * The file is basically a list of [record]s. So, 36 bytes and 16 bytes for the
  29.      * the first user's username and password, and then another 36+16 bytes for the
  30.      * next user, and so on.
  31.      */
  32.    
  33.     public static void initialize() throws IOException {
  34.        
  35.         FILE_PATH = new File(System.getProperty("user.home"), "prometheus-auth.db").getPath();
  36.        
  37.         if (!new File(FILE_PATH).exists())
  38.         {
  39.             _database = new RandomAccessFile(FILE_PATH, "rw");
  40.             _database.writeInt(0);
  41.             _database.seek(0);
  42.         }
  43.         else
  44.         {
  45.             _database = new RandomAccessFile(FILE_PATH, "rw");
  46.         }
  47.     }
  48.    
  49.     public static boolean checkUser(String username, String password) throws Exception
  50.     {
  51.         // Seek to the beginning of the file.
  52.         _database.seek(0);
  53.        
  54.         // Check if we have any users.
  55.         int users = _database.readInt();
  56.         if (users < 1)
  57.             return false;
  58.        
  59.         for (int i = 0; i < users; i++)
  60.         {
  61.             // Read SIZE_USERNAME bytes (username) for this user.
  62.             byte[] unBuffer = new byte[SIZE_USERNAME];
  63.             _database.read(unBuffer);
  64.             String un = new String(unBuffer).replace("\0", "");;
  65.            
  66.             // If the names do not match, skip this user.
  67.             if (!un.equals(username))
  68.             {
  69.                 _database.skipBytes(SIZE_PASSWORDHASH);
  70.                 continue;
  71.             }
  72.            
  73.             // Read SIZE_PASSWORDHASH byte (password hash) for this user.
  74.             byte[] pwBuffer = new byte[SIZE_PASSWORDHASH];
  75.             _database.read(pwBuffer);
  76.            
  77.             // Return whether the password hashes match.
  78.             return AuthenticationDatabase.bytesEqual(pwBuffer, Md5Util.computeHash(password));
  79.         }
  80.        
  81.         // Couldn't find the user.
  82.         return false;
  83.     }
  84.  
  85.     public static boolean addUser(String username, String password) throws Exception {
  86.         // Seek to the beginning of the file.
  87.         _database.seek(0);
  88.        
  89.         // Check if we have any users.
  90.         int users = _database.readInt();
  91.                
  92.         for (int i = 0; i < users; i++)
  93.         {
  94.             // Read SIZE_USERNAME bytes (username) for this user.
  95.             byte[] unBuffer = new byte[SIZE_USERNAME];
  96.             _database.read(unBuffer);
  97.             String un = new String(unBuffer).replace("\0", "");
  98.            
  99.             // If the names do match, abort the operation.
  100.             if (un.equals(username))
  101.             {
  102.                 return false;
  103.             }
  104.  
  105.             // Skip the password hash.
  106.             _database.skipBytes(SIZE_PASSWORDHASH);
  107.         }
  108.        
  109.         // Write the user to the end of our files.
  110.         if (username.length() > SIZE_USERNAME)
  111.             _database.writeBytes(username.substring(0, SIZE_USERNAME));
  112.         else
  113.         {
  114.             _database.writeBytes(username);
  115.            
  116.             for (int i = 0; i < SIZE_USERNAME - username.length(); i++)
  117.                 _database.writeByte(0);
  118.         }
  119.        
  120.        
  121.         // Write the password to file.
  122.         _database.write(Md5Util.computeHash(password));
  123.        
  124.         // Increase the number of users.
  125.         _database.seek(0);
  126.         _database.writeInt(users + 1);
  127.        
  128.         return true;
  129.     }
  130.  
  131.     public static boolean deleteUser(String username) throws Exception {
  132.         // Seek to the beginning of the file.
  133.         _database.seek(0);
  134.        
  135.         // Check if we have any users.
  136.         int users = _database.readInt();
  137.         if (users < 1)
  138.             return false;
  139.        
  140.         for (int i = 0; i < users; i++)
  141.         {
  142.             // Read SIZE_USERNAME bytes (username) for this user.
  143.             byte[] unBuffer = new byte[SIZE_USERNAME];
  144.             _database.read(unBuffer);
  145.             String un = new String(unBuffer).replace("\0", "");
  146.            
  147.             // If the names do not match, skip this user.
  148.             if (!un.equals(username))
  149.             {
  150.                 _database.skipBytes(SIZE_PASSWORDHASH);
  151.                 continue;
  152.             }
  153.            
  154.             // Skip back SIZE_USERNAME bytes to the beginning of the entry, and clear it.
  155.             _database.seek(_database.getFilePointer() - SIZE_USERNAME);
  156.             for (int j = 0; j < SIZE_RECORD; j++)
  157.                 _database.writeByte(0);
  158.             _database.seek(_database.getFilePointer() - SIZE_RECORD);
  159.            
  160.             // Save the current position, and then read the last entry of the file.
  161.             long insertIndex = _database.getFilePointer();
  162.             byte[] lastEntry = new byte[SIZE_RECORD];
  163.             _database.seek(_database.length() - SIZE_RECORD);
  164.             _database.read(lastEntry);
  165.            
  166.             // Rewrite the last entry into the space of the deleted entry.
  167.             _database.seek(insertIndex);
  168.             _database.write(lastEntry);
  169.             _database.setLength(_database.length() - SIZE_RECORD);
  170.            
  171.             // Decrease the number of users.
  172.             _database.seek(0);
  173.             _database.writeInt(users - 1);
  174.            
  175.             return true;
  176.         }
  177.        
  178.         // Couldn't find the user.
  179.         return false;
  180.     }
  181.  
  182.     public static boolean editUser(String username, String newPassword) throws Exception {
  183.         // Seek to the beginning of the file.
  184.         _database.seek(0);
  185.        
  186.         // Check if we have any users.
  187.         int users = _database.readInt();
  188.         if (users < 1)
  189.             return false;
  190.        
  191.         for (int i = 0; i < users; i++)
  192.         {
  193.             // Read SIZE_USERNAME bytes (username) for this user.
  194.             byte[] unBuffer = new byte[SIZE_USERNAME];
  195.             _database.read(unBuffer);
  196.             String un = new String(unBuffer).replace("\0", "");;
  197.            
  198.             // If the names do not match, skip this user.
  199.             if (!un.equals(username))
  200.             {
  201.                 _database.skipBytes(SIZE_PASSWORDHASH);
  202.                 continue;
  203.             }
  204.            
  205.             // Write the SIZE_PASSWORDHASH byte (password hash) for this user.
  206.             _database.write(Md5Util.computeHash(newPassword));
  207.            
  208.             return true;
  209.         }
  210.        
  211.         // Couldn't find the user.
  212.         return false;
  213.     }
  214.  
  215.     public static String[] getUsers() throws Exception {
  216.         // Seek to the beginning of the file.
  217.         _database.seek(0);
  218.        
  219.         // Check if we have any users.
  220.         int users = _database.readInt();
  221.         if (users < 1)
  222.             return new String[0];
  223.        
  224.         String[] usersArr = new String[users];
  225.        
  226.         for (int i = 0; i < users; i++)
  227.         {
  228.             // Read SIZE_USERNAME bytes (username) for this user.
  229.             byte[] unBuffer = new byte[SIZE_USERNAME];
  230.             _database.read(unBuffer);
  231.             String un = new String(unBuffer).replace("\0", "");;
  232.             usersArr[i] = un;
  233.            
  234.             _database.skipBytes(SIZE_PASSWORDHASH);
  235.         }
  236.        
  237.         // Couldn't find the user.
  238.         return usersArr;
  239.     }
  240.    
  241.     private static boolean bytesEqual(byte[] b1, byte[] b2) {
  242.         if (b1.length != b2.length)
  243.             return false;
  244.        
  245.         for (int i = 0; i < b1.length; i++)
  246.             if (b1[i] != b2[i])
  247.                 return false;
  248.        
  249.         return true;
  250.     }
  251.    
  252. }
Add Comment
Please, Sign In to add comment