Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. package com.uber2d.sql;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.UnsupportedEncodingException;
  6. import java.security.MessageDigest;
  7. import java.security.NoSuchAlgorithmException;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.Statement;
  12. import java.util.logging.Logger;
  13.  
  14. public class Integration implements Runnable {
  15.  
  16. private static final Integration INSTANCE = new Integration();
  17. private static Logger LOGGER = Logger.getLogger(Integration.class.getName());
  18. public String SQL_HOST, SQL_DATABASE, SQL_USER, SQL_PASSWORD;
  19. private Connection connection = null;
  20. private Statement statement = null;
  21. private static Thread thread = null;
  22.  
  23. @SuppressWarnings("static-access")
  24. public void run() {
  25. boolean running = true;
  26. while(running) {
  27. try {
  28. if(connection == null) {
  29. connect();
  30. } else {
  31. ping();
  32. }
  33. thread.sleep(10000);
  34. } catch(Exception e){
  35. }
  36. }
  37.  
  38. }
  39.  
  40. public void ioConfig() {
  41. BufferedReader reader;
  42. try {
  43. reader = new BufferedReader(new FileReader("./data/sql.conf"));
  44. SQL_HOST = reader.readLine();
  45. SQL_DATABASE = reader.readLine();
  46. SQL_USER = reader.readLine();
  47. SQL_PASSWORD = reader.readLine();
  48. /*
  49. System.out.println(
  50. "host="+SQL_HOST+
  51. "\ndatabase="+SQL_DATABASE+
  52. "\nuser="+SQL_USER+
  53. "\npassword="+SQL_PASSWORD
  54. );
  55. */
  56. } catch (Exception e) {
  57. LOGGER.severe("Corrupt file 'sql.conf'");
  58. Runtime.getRuntime().exit(0);
  59. return;
  60. }
  61. }
  62.  
  63. public void connect(){
  64. try {
  65. Class.forName("com.mysql.jdbc.Driver");
  66. } catch(Exception e){
  67. LOGGER.info("Unable to find MySQL driver!");
  68. return;
  69. }
  70. this.ioConfig();
  71. LOGGER.info("Connecting to database..");
  72. try {
  73. connection = DriverManager.getConnection(
  74. "jdbc:mysql://"+SQL_HOST+":3306/"+SQL_DATABASE, SQL_USER, SQL_PASSWORD);
  75. statement = connection.createStatement();
  76. LOGGER.info("Connected to database!");
  77. } catch(Exception e){
  78. connection = null;
  79. LOGGER.severe("Database connection failure!");
  80. //e.printStackTrace();
  81. }
  82. }
  83.  
  84. private void ping(){
  85. try{
  86. @SuppressWarnings("unused")
  87. ResultSet results = null;
  88. String query = "SELECT * FROM user WHERE password LIKE 'null312'";
  89. results = statement.executeQuery(query);
  90. } catch(Exception e){
  91. connection = null;
  92. connect();
  93. e.printStackTrace();
  94. }
  95. }
  96.  
  97. public int[] validateLogin(String name, String password) {
  98. int[] returns = { 0, 0 };
  99. try {
  100. ResultSet results = null;
  101. String query = "SELECT * FROM name WHERE username LIKE '"+name+"'";
  102. try {
  103. if(statement == null)
  104. statement = connection.createStatement();
  105. } catch(Exception e){
  106. statement = null;
  107. connection = null;
  108. connect();
  109. statement = connection.createStatement();
  110. }
  111. results = statement.executeQuery(query);
  112. if(results.next()){
  113. String salt = results.getString("salt");
  114. String pass = results.getString("password");
  115. int group = results.getInt("usergroupid");
  116. returns[1] = group;
  117. String hash = null;
  118. hash = MD5(password);
  119. hash = MD5(hash+salt);
  120. if(pass.equals(hash)){
  121. returns[0] = 0; // okay
  122. return returns;
  123. } else {
  124. returns[0] = 1; // invalid password entered
  125. return returns;
  126. }
  127. } else {
  128. returns[0] = 4; // username does not exist
  129. return returns;
  130. }
  131. } catch(Exception e){
  132. statement = null;
  133. returns[0] = 6; // error loading profile
  134. return returns;
  135. }
  136. }
  137.  
  138. private static String convertToHex(byte[] data) {
  139. StringBuffer buf = new StringBuffer();
  140. for (int i = 0; i < data.length; i++) {
  141. int halfbyte = (data[i] >>> 4) & 0x0F;
  142. int two_halfs = 0;
  143. do {
  144. if ((0 <= halfbyte) && (halfbyte <= 9))
  145. buf.append((char) ('0' + halfbyte));
  146. else
  147. buf.append((char) ('a' + (halfbyte - 10)));
  148. halfbyte = data[i] & 0x0F;
  149. } while(two_halfs++ < 1);
  150. }
  151. return buf.toString();
  152. }
  153.  
  154. public String MD5(String text)
  155. throws NoSuchAlgorithmException, UnsupportedEncodingException {
  156. MessageDigest md;
  157. md = MessageDigest.getInstance("MD5");
  158. byte[] md5hash = new byte[32];
  159. md.update(text.getBytes("iso-8859-1"), 0, text.length());
  160. md5hash = md.digest();
  161. return convertToHex(md5hash);
  162. }
  163.  
  164. public static Integration getInstance() {
  165. return INSTANCE;
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement