Advertisement
Guest User

Untitled

a guest
May 6th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.34 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 = 1;
  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 = 1;
  105. if(lineNum <100){
  106.  
  107. while((userfield.getText().equals(userArray[lineNum])))
  108. {
  109. if(userArray[lineNum].salt != null){
  110. String hex2 = Hash.mainHash(String.valueOf(passfield), userArray[lineNum].salt);
  111.  
  112. if(userArray[lineNum].password.equals(hex2))
  113. {
  114. logged_in = true;
  115.  
  116. JFrame frame2 = new JFrame("Members");
  117. frame2.setVisible(true);
  118. frame2.setSize(500, 500);
  119.  
  120. JLabel welcome = new JLabel("Welcome " + userfield.getText() + "!");
  121.  
  122. JLabel newline = new JLabel("<html> <br/> </html>");
  123.  
  124. DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
  125. Date date = new Date();
  126. JLabel datelabel = new JLabel("Your local time is: \t" + (dateFormat.format(date)));
  127.  
  128. setVisible(false);
  129.  
  130. JPanel panel = new JPanel();
  131. frame2.add(panel);
  132. panel.add(welcome);
  133. panel.add(datelabel);
  134. panel.add(logout);
  135. lineNum = 1;
  136. }else {lineNum++;}
  137. }
  138.  
  139. }
  140. }
  141.  
  142. }
  143. });
  144.  
  145. register.addActionListener(new ActionListener() {
  146. public void actionPerformed(ActionEvent e2)
  147. {
  148. SecureRandom secureRNG = new SecureRandom();
  149. int secureSalt = secureRNG.nextInt(1000);
  150.  
  151. String salt = Integer.toString(secureSalt);
  152.  
  153. String hex = Hash.mainHash(String.valueOf(passfield), salt);
  154. userArray[i] = new User();
  155.  
  156. userArray[i].username = userfield.getText();
  157. userArray[i].password = hex;
  158. userArray[i].salt = salt;
  159.  
  160. try {
  161. //String saltedpass = hex;
  162. String pass = String.valueOf(passfield.getPassword());
  163.  
  164. if(pass.length() >= 5)
  165. {
  166. BufferedWriter writer = new BufferedWriter(new FileWriter("C:/Users/cm6140/Desktop/test.txt", true));
  167.  
  168. writer.newLine();
  169. writer.write(userArray[i].username);
  170. writer.write(" ");
  171. writer.write(userArray[i].password);
  172. writer.write(" ");
  173. //writer.write(pass);
  174. writer.write(" ");
  175. writer.write(userArray[i].salt);
  176. writer.close();
  177.  
  178. JFrame frame2 = new JFrame("Members");
  179. frame2.setVisible(true);
  180. frame2.setSize(500, 500);
  181. JLabel welcome = new JLabel("Welcome " + userfield.getText() + "!");
  182.  
  183. JLabel newline = new JLabel("<html> <br/> </html>");
  184.  
  185. DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
  186. Date date = new Date();
  187. JLabel datelabel = new JLabel("Your local time is: \t" + (dateFormat.format(date)));
  188.  
  189. setVisible(false);
  190.  
  191. JPanel panel = new JPanel();
  192. frame2.add(panel);
  193. panel.add(welcome);
  194. panel.add(datelabel);
  195. panel.add(logout);
  196. }
  197.  
  198. } catch (IOException e) {
  199. e.printStackTrace();
  200. }
  201. i++;
  202. }
  203. });
  204.  
  205. setSize(500,500);
  206. setResizable(false);
  207. setDefaultCloseOperation(EXIT_ON_CLOSE);
  208. setVisible(true);
  209. }
  210.  
  211. public static class Hash
  212. {
  213. private static final long MASK_16_BITS = 0xFFFFL;
  214. private static final int MASK_BIT_1 = 0x1;
  215.  
  216. /*
  217. * Paul Hsieh's Hash Function.
  218. *
  219. * @param data data to hash
  220. * @param dataLength length of the data, in bytes
  221. * @param hashedValue previous value of the hash. If it is the start of the
  222. * method, used the length of the data (ex.: 8 bytes).
  223. * @return
  224. */
  225.  
  226. public static int superFastHash(long data, int hash)
  227. {
  228. int tmp;
  229. //int rem;
  230.  
  231. //if (len <= 0) {
  232. // return 0;
  233. //}
  234.  
  235. //rem = len & 3;
  236. //len >>= 2;
  237.  
  238. //Main Loop
  239. for (int i = 0; i < 4; i += 2) {
  240. // Get lower 16 bits
  241. hash += get16BitsAligned(data, i);
  242. // Calculate some random value with second-lower 16 bits
  243. tmp = (get16BitsAligned(data, i + 1) << 11) ^ hash;
  244. hash = (hash << 16) ^ tmp;
  245. // At this point, it would advance the data, but since it is restricted
  246. // to longs (64-bit values), it is unnecessary).
  247. hash += hash >> 11;
  248. }
  249.  
  250. // Handle end cases //
  251. // There are no end cases, main loop is done in chuncks of 32 bits.
  252.  
  253. // Force "avalanching" of final 127 bits //
  254. hash ^= hash << 3;
  255. hash += hash >> 5;
  256. hash ^= hash << 4;
  257. hash += hash >> 17;
  258. hash ^= hash << 25;
  259. hash += hash >> 6;
  260. //System.out.print(hash);
  261. return hash;
  262. }
  263.  
  264. /*
  265. * Returns 16 bits from the long number.
  266. *
  267. * @param data
  268. * @param offset one of 0 to 3
  269. * @return
  270. */
  271.  
  272. public static int get16BitsAligned(long data, int offset)
  273. {
  274. // Normalize offset
  275. offset = offset%4;
  276. //System.out.println("offset:"+offset);
  277. // Align the mask
  278. long mask = MASK_16_BITS << 16*offset;
  279. //System.out.println("Mask:"+Long.toHexString(mask));
  280. //System.out.println("Data:"+Long.toHexString(data));
  281.  
  282. // Get the bits
  283. long result = data & mask;
  284.  
  285. // Put bits in position
  286. return (int) (result >>> (16*offset));
  287. }
  288.  
  289. public static String mainHash(String password, String salt)
  290. {
  291. String password2 = password + salt;
  292.  
  293. int dechash;
  294. byte[] bytes = password2.getBytes(StandardCharsets.US_ASCII);
  295. long result = 0;
  296. for (int i = 0; i < bytes.length; i++) {
  297. result <<= 8;
  298. result |= (bytes[i] & 0xFF);
  299. }
  300. dechash = superFastHash(result, password2.length());
  301. StringBuilder sb = new StringBuilder();
  302. sb.append(Integer.toHexString(dechash));
  303. if (sb.length() % 2 > 0) {
  304. sb.insert(0, '0'); // pad with leading zero if needed
  305. }
  306. String hex = sb.toString();
  307. return hex;
  308. }
  309. }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement