Advertisement
Guest User

Untitled

a guest
May 6th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.60 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.nio.charset.StandardCharsets;
  9. import java.security.NoSuchAlgorithmException;
  10. import java.util.Scanner;
  11. import java.security.SecureRandom;
  12. import java.text.DateFormat;
  13. import java.text.SimpleDateFormat;
  14. import java.time.LocalDateTime;
  15. import java.time.format.DateTimeFormatter;
  16. import java.util.Calendar;
  17. import java.util.Date;
  18. import java.nio.charset.StandardCharsets;
  19. import java.io.FileNotFoundException;
  20. import java.security.SecureRandom;
  21.  
  22. public class Main extends JFrame
  23. {
  24. int i = 0;
  25. boolean logged_in = false;
  26.  
  27. public static void main(String[] args)
  28. {
  29. new Main().setVisible(true);
  30. }
  31.  
  32. public class User
  33. {
  34. String salt;
  35. String username;
  36. String password;
  37. }
  38.  
  39. public Main()
  40. {
  41. super("Login");
  42.  
  43. setLayout(new GridBagLayout());
  44.  
  45. GridBagConstraints gbc = new GridBagConstraints();
  46.  
  47. //username label/field
  48. JLabel userlabel = new JLabel("Enter Username:");
  49. gbc.gridx = 0;
  50. gbc.gridy = 1;
  51. add(userlabel,gbc);
  52.  
  53. final JTextField userfield = new JTextField(20);
  54. gbc.gridx = 1;
  55. gbc.gridy = 1;
  56. add(userfield,gbc);
  57. //end username label/field
  58.  
  59. //password label/field
  60. JLabel passlabel = new JLabel("Enter Password:");
  61. gbc.gridx = 0;
  62. gbc.gridy = 2;
  63. add(passlabel,gbc);
  64.  
  65. final JPasswordField passfield = new JPasswordField(20);
  66. passfield.setEchoChar('*');
  67. gbc.gridx = 1;
  68. gbc.gridy = 2;
  69. add(passfield,gbc);
  70. //end password label/field
  71.  
  72. //user array
  73. User[] userArray = new User[100];
  74. System.out.print(i);
  75. //end user array
  76.  
  77. //login button
  78. JButton login = new JButton("Login");
  79. gbc.gridx = 1;
  80. gbc.gridy = 3;
  81. add(login,gbc);
  82.  
  83. //register button
  84. JButton register = new JButton("Register");
  85. gbc.gridx = 2;
  86. gbc.gridy = 3;
  87. add(register,gbc);
  88.  
  89. //logout button
  90. JButton logout = new JButton("Logout");
  91.  
  92. //end buttons
  93.  
  94. logout.addActionListener(new ActionListener() {
  95. public void actionPerformed(ActionEvent e3) {
  96. setVisible(true);
  97. }
  98. });
  99.  
  100. login.addActionListener(new ActionListener() {
  101. public void actionPerformed(ActionEvent e1)
  102. {
  103.  
  104. int lineNum = 0;
  105. if(lineNum <100){
  106. System.out.print("lineNum =" + lineNum+ "\n");
  107. while((((userArray[lineNum]).username).equals(userfield.getText())))
  108. { System.out.print("lineNum = " + lineNum+ "\n");
  109. System.out.print("\nfound username");
  110.  
  111. System.out.print("\nuser salt is not null");
  112. System.out.print("\nuser salt = " + userArray[lineNum].salt);
  113. String hex2 = Hash.mainHash(String.valueOf(passfield), userArray[lineNum].salt);
  114. System.out.print("\nproposed hash = " + hex2);
  115. if(userArray[lineNum].password.equals(hex2))
  116. {
  117. logged_in = true;
  118.  
  119. JFrame frame2 = new JFrame("Members");
  120. frame2.setVisible(true);
  121. frame2.setSize(500, 500);
  122.  
  123. JLabel welcome = new JLabel("Welcome " + userfield.getText() + "!");
  124.  
  125. JLabel newline = new JLabel("<html> <br/> </html>");
  126.  
  127. DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
  128. Date date = new Date();
  129. JLabel datelabel = new JLabel("Your local time is: \t" + (dateFormat.format(date)));
  130.  
  131. setVisible(false);
  132.  
  133. JPanel panel = new JPanel();
  134. frame2.add(panel);
  135. panel.add(welcome);
  136. panel.add(datelabel);
  137. panel.add(logout);
  138. lineNum = 0;
  139. }else {lineNum++;}
  140. }
  141.  
  142. }
  143. }
  144. });
  145.  
  146. register.addActionListener(new ActionListener() {
  147. public void actionPerformed(ActionEvent e2)
  148. {
  149. SecureRandom secureRNG = new SecureRandom();
  150. int secureSalt = secureRNG.nextInt(1000);
  151.  
  152. String salt = Integer.toString(secureSalt);
  153.  
  154. String hex = Hash.mainHash(String.valueOf(passfield), salt);
  155. userArray[i] = new User();
  156.  
  157. userArray[i].username = userfield.getText();
  158. userArray[i].password = hex;
  159. userArray[i].salt = salt;
  160.  
  161. try {
  162. //String saltedpass = hex;
  163. String pass = String.valueOf(passfield.getPassword());
  164.  
  165. if(pass.length() >= 5)
  166. {
  167. BufferedWriter writer = new BufferedWriter(new FileWriter("C:/Users/cm6140/Desktop/test.txt", true));
  168.  
  169. writer.newLine();
  170. writer.write(userArray[i].username);
  171. writer.write(" ");
  172. writer.write(userArray[i].password);
  173. writer.write(" ");
  174. //writer.write(pass);
  175. writer.write(" ");
  176. writer.write(userArray[i].salt);
  177. writer.close();
  178.  
  179. JFrame frame2 = new JFrame("Members");
  180. frame2.setVisible(true);
  181. frame2.setSize(500, 500);
  182. JLabel welcome = new JLabel("Welcome " + userfield.getText() + "!");
  183.  
  184. JLabel newline = new JLabel("<html> <br/> </html>");
  185.  
  186. DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
  187. Date date = new Date();
  188. JLabel datelabel = new JLabel("Your local time is: \t" + (dateFormat.format(date)));
  189.  
  190. setVisible(false);
  191.  
  192. JPanel panel = new JPanel();
  193. frame2.add(panel);
  194. panel.add(welcome);
  195. panel.add(datelabel);
  196. panel.add(logout);
  197. }
  198.  
  199. } catch (IOException e) {
  200. e.printStackTrace();
  201. }
  202. i++;
  203. }
  204. });
  205.  
  206. setSize(500,500);
  207. setResizable(false);
  208. setDefaultCloseOperation(EXIT_ON_CLOSE);
  209. setVisible(true);
  210. }
  211.  
  212. public static class Hash
  213. {
  214. private static final long MASK_16_BITS = 0xFFFFL;
  215. private static final int MASK_BIT_1 = 0x1;
  216.  
  217. /*
  218. * Paul Hsieh's Hash Function.
  219. *
  220. * @param data data to hash
  221. * @param dataLength length of the data, in bytes
  222. * @param hashedValue previous value of the hash. If it is the start of the
  223. * method, used the length of the data (ex.: 8 bytes).
  224. * @return
  225. */
  226.  
  227. public static int superFastHash(long data, int hash)
  228. {
  229. int tmp;
  230. //int rem;
  231.  
  232. //if (len <= 0) {
  233. // return 0;
  234. //}
  235.  
  236. //rem = len & 3;
  237. //len >>= 2;
  238.  
  239. //Main Loop
  240. for (int i = 0; i < 4; i += 2) {
  241. // Get lower 16 bits
  242. hash += get16BitsAligned(data, i);
  243. // Calculate some random value with second-lower 16 bits
  244. tmp = (get16BitsAligned(data, i + 1) << 11) ^ hash;
  245. hash = (hash << 16) ^ tmp;
  246. // At this point, it would advance the data, but since it is restricted
  247. // to longs (64-bit values), it is unnecessary).
  248. hash += hash >> 11;
  249. }
  250.  
  251. // Handle end cases //
  252. // There are no end cases, main loop is done in chuncks of 32 bits.
  253.  
  254. // Force "avalanching" of final 127 bits //
  255. hash ^= hash << 3;
  256. hash += hash >> 5;
  257. hash ^= hash << 4;
  258. hash += hash >> 17;
  259. hash ^= hash << 25;
  260. hash += hash >> 6;
  261. //System.out.print(hash);
  262. return hash;
  263. }
  264.  
  265. /*
  266. * Returns 16 bits from the long number.
  267. *
  268. * @param data
  269. * @param offset one of 0 to 3
  270. * @return
  271. */
  272.  
  273. public static int get16BitsAligned(long data, int offset)
  274. {
  275. // Normalize offset
  276. offset = offset%4;
  277. //System.out.println("offset:"+offset);
  278. // Align the mask
  279. long mask = MASK_16_BITS << 16*offset;
  280. //System.out.println("Mask:"+Long.toHexString(mask));
  281. //System.out.println("Data:"+Long.toHexString(data));
  282.  
  283. // Get the bits
  284. long result = data & mask;
  285.  
  286. // Put bits in position
  287. return (int) (result >>> (16*offset));
  288. }
  289.  
  290. public static String mainHash(String password, String salt)
  291. {
  292. String password2 = password + salt;
  293.  
  294. int dechash;
  295. byte[] bytes = password2.getBytes(StandardCharsets.US_ASCII);
  296. long result = 0;
  297. for (int i = 0; i < bytes.length; i++) {
  298. result <<= 8;
  299. result |= (bytes[i] & 0xFF);
  300. }
  301. dechash = superFastHash(result, password2.length());
  302. StringBuilder sb = new StringBuilder();
  303. sb.append(Integer.toHexString(dechash));
  304. if (sb.length() % 2 > 0) {
  305. sb.insert(0, '0'); // pad with leading zero if needed
  306. }
  307. String hex = sb.toString();
  308. return hex;
  309. }
  310. }
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement