Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.uber2d.sql;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.UnsupportedEncodingException;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.logging.Logger;
- public class Integration implements Runnable {
- private static final Integration INSTANCE = new Integration();
- private static Logger LOGGER = Logger.getLogger(Integration.class.getName());
- public String SQL_HOST, SQL_DATABASE, SQL_USER, SQL_PASSWORD;
- private Connection connection = null;
- private Statement statement = null;
- private static Thread thread = null;
- @SuppressWarnings("static-access")
- public void run() {
- boolean running = true;
- while(running) {
- try {
- if(connection == null) {
- connect();
- } else {
- ping();
- }
- thread.sleep(10000);
- } catch(Exception e){
- }
- }
- }
- public void ioConfig() {
- BufferedReader reader;
- try {
- reader = new BufferedReader(new FileReader("./data/sql.conf"));
- SQL_HOST = reader.readLine();
- SQL_DATABASE = reader.readLine();
- SQL_USER = reader.readLine();
- SQL_PASSWORD = reader.readLine();
- /*
- System.out.println(
- "host="+SQL_HOST+
- "\ndatabase="+SQL_DATABASE+
- "\nuser="+SQL_USER+
- "\npassword="+SQL_PASSWORD
- );
- */
- } catch (Exception e) {
- LOGGER.severe("Corrupt file 'sql.conf'");
- Runtime.getRuntime().exit(0);
- return;
- }
- }
- public void connect(){
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch(Exception e){
- LOGGER.info("Unable to find MySQL driver!");
- return;
- }
- this.ioConfig();
- LOGGER.info("Connecting to database..");
- try {
- connection = DriverManager.getConnection(
- "jdbc:mysql://"+SQL_HOST+":3306/"+SQL_DATABASE, SQL_USER, SQL_PASSWORD);
- statement = connection.createStatement();
- LOGGER.info("Connected to database!");
- } catch(Exception e){
- connection = null;
- LOGGER.severe("Database connection failure!");
- //e.printStackTrace();
- }
- }
- private void ping(){
- try{
- @SuppressWarnings("unused")
- ResultSet results = null;
- String query = "SELECT * FROM user WHERE password LIKE 'null312'";
- results = statement.executeQuery(query);
- } catch(Exception e){
- connection = null;
- connect();
- e.printStackTrace();
- }
- }
- public int[] validateLogin(String name, String password) {
- int[] returns = { 0, 0 };
- try {
- ResultSet results = null;
- String query = "SELECT * FROM name WHERE username LIKE '"+name+"'";
- try {
- if(statement == null)
- statement = connection.createStatement();
- } catch(Exception e){
- statement = null;
- connection = null;
- connect();
- statement = connection.createStatement();
- }
- results = statement.executeQuery(query);
- if(results.next()){
- String salt = results.getString("salt");
- String pass = results.getString("password");
- int group = results.getInt("usergroupid");
- returns[1] = group;
- String hash = null;
- hash = MD5(password);
- hash = MD5(hash+salt);
- if(pass.equals(hash)){
- returns[0] = 0; // okay
- return returns;
- } else {
- returns[0] = 1; // invalid password entered
- return returns;
- }
- } else {
- returns[0] = 4; // username does not exist
- return returns;
- }
- } catch(Exception e){
- statement = null;
- returns[0] = 6; // error loading profile
- return returns;
- }
- }
- private static String convertToHex(byte[] data) {
- StringBuffer buf = new StringBuffer();
- for (int i = 0; i < data.length; i++) {
- int halfbyte = (data[i] >>> 4) & 0x0F;
- int two_halfs = 0;
- do {
- if ((0 <= halfbyte) && (halfbyte <= 9))
- buf.append((char) ('0' + halfbyte));
- else
- buf.append((char) ('a' + (halfbyte - 10)));
- halfbyte = data[i] & 0x0F;
- } while(two_halfs++ < 1);
- }
- return buf.toString();
- }
- public String MD5(String text)
- throws NoSuchAlgorithmException, UnsupportedEncodingException {
- MessageDigest md;
- md = MessageDigest.getInstance("MD5");
- byte[] md5hash = new byte[32];
- md.update(text.getBytes("iso-8859-1"), 0, text.length());
- md5hash = md.digest();
- return convertToHex(md5hash);
- }
- public static Integration getInstance() {
- return INSTANCE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement