Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package prometheus.server;
- /*
- * Author: Sajid Anwar
- * Program: Prometheus
- * Date: 3/28/2012
- * School: Ironwood High School
- */
- import java.io.*;
- import prometheus.util.Md5Util;
- public class AuthenticationDatabase {
- private static RandomAccessFile _database;
- private static String FILE_PATH;
- private static final int SIZE_USERNAME = 36;
- private static final int SIZE_PASSWORDHASH = 16;
- private static final int SIZE_RECORD = SIZE_USERNAME + SIZE_PASSWORDHASH;
- /*
- * username = {36 bytes}
- * password = {16 bytes}
- * record = [username][password]
- *
- * The file is basically a list of [record]s. So, 36 bytes and 16 bytes for the
- * the first user's username and password, and then another 36+16 bytes for the
- * next user, and so on.
- */
- public static void initialize() throws IOException {
- FILE_PATH = new File(System.getProperty("user.home"), "prometheus-auth.db").getPath();
- if (!new File(FILE_PATH).exists())
- {
- _database = new RandomAccessFile(FILE_PATH, "rw");
- _database.writeInt(0);
- _database.seek(0);
- }
- else
- {
- _database = new RandomAccessFile(FILE_PATH, "rw");
- }
- }
- public static boolean checkUser(String username, String password) throws Exception
- {
- // Seek to the beginning of the file.
- _database.seek(0);
- // Check if we have any users.
- int users = _database.readInt();
- if (users < 1)
- return false;
- for (int i = 0; i < users; i++)
- {
- // Read SIZE_USERNAME bytes (username) for this user.
- byte[] unBuffer = new byte[SIZE_USERNAME];
- _database.read(unBuffer);
- String un = new String(unBuffer).replace("\0", "");;
- // If the names do not match, skip this user.
- if (!un.equals(username))
- {
- _database.skipBytes(SIZE_PASSWORDHASH);
- continue;
- }
- // Read SIZE_PASSWORDHASH byte (password hash) for this user.
- byte[] pwBuffer = new byte[SIZE_PASSWORDHASH];
- _database.read(pwBuffer);
- // Return whether the password hashes match.
- return AuthenticationDatabase.bytesEqual(pwBuffer, Md5Util.computeHash(password));
- }
- // Couldn't find the user.
- return false;
- }
- public static boolean addUser(String username, String password) throws Exception {
- // Seek to the beginning of the file.
- _database.seek(0);
- // Check if we have any users.
- int users = _database.readInt();
- for (int i = 0; i < users; i++)
- {
- // Read SIZE_USERNAME bytes (username) for this user.
- byte[] unBuffer = new byte[SIZE_USERNAME];
- _database.read(unBuffer);
- String un = new String(unBuffer).replace("\0", "");
- // If the names do match, abort the operation.
- if (un.equals(username))
- {
- return false;
- }
- // Skip the password hash.
- _database.skipBytes(SIZE_PASSWORDHASH);
- }
- // Write the user to the end of our files.
- if (username.length() > SIZE_USERNAME)
- _database.writeBytes(username.substring(0, SIZE_USERNAME));
- else
- {
- _database.writeBytes(username);
- for (int i = 0; i < SIZE_USERNAME - username.length(); i++)
- _database.writeByte(0);
- }
- // Write the password to file.
- _database.write(Md5Util.computeHash(password));
- // Increase the number of users.
- _database.seek(0);
- _database.writeInt(users + 1);
- return true;
- }
- public static boolean deleteUser(String username) throws Exception {
- // Seek to the beginning of the file.
- _database.seek(0);
- // Check if we have any users.
- int users = _database.readInt();
- if (users < 1)
- return false;
- for (int i = 0; i < users; i++)
- {
- // Read SIZE_USERNAME bytes (username) for this user.
- byte[] unBuffer = new byte[SIZE_USERNAME];
- _database.read(unBuffer);
- String un = new String(unBuffer).replace("\0", "");
- // If the names do not match, skip this user.
- if (!un.equals(username))
- {
- _database.skipBytes(SIZE_PASSWORDHASH);
- continue;
- }
- // Skip back SIZE_USERNAME bytes to the beginning of the entry, and clear it.
- _database.seek(_database.getFilePointer() - SIZE_USERNAME);
- for (int j = 0; j < SIZE_RECORD; j++)
- _database.writeByte(0);
- _database.seek(_database.getFilePointer() - SIZE_RECORD);
- // Save the current position, and then read the last entry of the file.
- long insertIndex = _database.getFilePointer();
- byte[] lastEntry = new byte[SIZE_RECORD];
- _database.seek(_database.length() - SIZE_RECORD);
- _database.read(lastEntry);
- // Rewrite the last entry into the space of the deleted entry.
- _database.seek(insertIndex);
- _database.write(lastEntry);
- _database.setLength(_database.length() - SIZE_RECORD);
- // Decrease the number of users.
- _database.seek(0);
- _database.writeInt(users - 1);
- return true;
- }
- // Couldn't find the user.
- return false;
- }
- public static boolean editUser(String username, String newPassword) throws Exception {
- // Seek to the beginning of the file.
- _database.seek(0);
- // Check if we have any users.
- int users = _database.readInt();
- if (users < 1)
- return false;
- for (int i = 0; i < users; i++)
- {
- // Read SIZE_USERNAME bytes (username) for this user.
- byte[] unBuffer = new byte[SIZE_USERNAME];
- _database.read(unBuffer);
- String un = new String(unBuffer).replace("\0", "");;
- // If the names do not match, skip this user.
- if (!un.equals(username))
- {
- _database.skipBytes(SIZE_PASSWORDHASH);
- continue;
- }
- // Write the SIZE_PASSWORDHASH byte (password hash) for this user.
- _database.write(Md5Util.computeHash(newPassword));
- return true;
- }
- // Couldn't find the user.
- return false;
- }
- public static String[] getUsers() throws Exception {
- // Seek to the beginning of the file.
- _database.seek(0);
- // Check if we have any users.
- int users = _database.readInt();
- if (users < 1)
- return new String[0];
- String[] usersArr = new String[users];
- for (int i = 0; i < users; i++)
- {
- // Read SIZE_USERNAME bytes (username) for this user.
- byte[] unBuffer = new byte[SIZE_USERNAME];
- _database.read(unBuffer);
- String un = new String(unBuffer).replace("\0", "");;
- usersArr[i] = un;
- _database.skipBytes(SIZE_PASSWORDHASH);
- }
- // Couldn't find the user.
- return usersArr;
- }
- private static boolean bytesEqual(byte[] b1, byte[] b2) {
- if (b1.length != b2.length)
- return false;
- for (int i = 0; i < b1.length; i++)
- if (b1[i] != b2[i])
- return false;
- return true;
- }
- }
Add Comment
Please, Sign In to add comment