Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.nio.charset.StandardCharsets;
- import java.security.NoSuchAlgorithmException;
- import java.util.Scanner;
- import java.security.SecureRandom;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- import java.util.Calendar;
- import java.util.Date;
- import java.nio.charset.StandardCharsets;
- import java.io.FileNotFoundException;
- import java.security.SecureRandom;
- public class Main extends JFrame
- {
- int i = 0;
- boolean logged_in = false;
- public static void main(String[] args)
- {
- new Main().setVisible(true);
- }
- public class User
- {
- String salt;
- String username;
- String password;
- }
- public Main()
- {
- super("Login");
- setLayout(new GridBagLayout());
- GridBagConstraints gbc = new GridBagConstraints();
- //username label/field
- JLabel userlabel = new JLabel("Enter Username:");
- gbc.gridx = 0;
- gbc.gridy = 1;
- add(userlabel,gbc);
- final JTextField userfield = new JTextField(20);
- gbc.gridx = 1;
- gbc.gridy = 1;
- add(userfield,gbc);
- //end username label/field
- //password label/field
- JLabel passlabel = new JLabel("Enter Password:");
- gbc.gridx = 0;
- gbc.gridy = 2;
- add(passlabel,gbc);
- final JPasswordField passfield = new JPasswordField(20);
- passfield.setEchoChar('*');
- gbc.gridx = 1;
- gbc.gridy = 2;
- add(passfield,gbc);
- //end password label/field
- //user array
- User[] userArray = new User[100];
- System.out.print(i);
- //end user array
- //login button
- JButton login = new JButton("Login");
- gbc.gridx = 1;
- gbc.gridy = 3;
- add(login,gbc);
- //register button
- JButton register = new JButton("Register");
- gbc.gridx = 2;
- gbc.gridy = 3;
- add(register,gbc);
- //logout button
- JButton logout = new JButton("Logout");
- //end buttons
- logout.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e3) {
- setVisible(true);
- }
- });
- login.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e1)
- {
- int lineNum = 0;
- if(lineNum <100){
- System.out.print("lineNum =" + lineNum+ "\n");
- while((((userArray[lineNum]).username).equals(userfield.getText())))
- { System.out.print("lineNum = " + lineNum+ "\n");
- System.out.print("\nfound username");
- System.out.print("\nuser salt is not null");
- System.out.print("\nuser salt = " + userArray[lineNum].salt);
- String hex2 = Hash.mainHash(String.valueOf(passfield), userArray[lineNum].salt);
- System.out.print("\nproposed hash = " + hex2);
- if(userArray[lineNum].password.equals(hex2))
- {
- logged_in = true;
- JFrame frame2 = new JFrame("Members");
- frame2.setVisible(true);
- frame2.setSize(500, 500);
- JLabel welcome = new JLabel("Welcome " + userfield.getText() + "!");
- JLabel newline = new JLabel("<html> <br/> </html>");
- DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
- Date date = new Date();
- JLabel datelabel = new JLabel("Your local time is: \t" + (dateFormat.format(date)));
- setVisible(false);
- JPanel panel = new JPanel();
- frame2.add(panel);
- panel.add(welcome);
- panel.add(datelabel);
- panel.add(logout);
- lineNum = 0;
- }else {lineNum++;}
- }
- }
- }
- });
- register.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e2)
- {
- SecureRandom secureRNG = new SecureRandom();
- int secureSalt = secureRNG.nextInt(1000);
- String salt = Integer.toString(secureSalt);
- String hex = Hash.mainHash(String.valueOf(passfield), salt);
- userArray[i] = new User();
- userArray[i].username = userfield.getText();
- userArray[i].password = hex;
- userArray[i].salt = salt;
- try {
- //String saltedpass = hex;
- String pass = String.valueOf(passfield.getPassword());
- if(pass.length() >= 5)
- {
- BufferedWriter writer = new BufferedWriter(new FileWriter("C:/Users/cm6140/Desktop/test.txt", true));
- writer.newLine();
- writer.write(userArray[i].username);
- writer.write(" ");
- writer.write(userArray[i].password);
- writer.write(" ");
- //writer.write(pass);
- writer.write(" ");
- writer.write(userArray[i].salt);
- writer.close();
- JFrame frame2 = new JFrame("Members");
- frame2.setVisible(true);
- frame2.setSize(500, 500);
- JLabel welcome = new JLabel("Welcome " + userfield.getText() + "!");
- JLabel newline = new JLabel("<html> <br/> </html>");
- DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
- Date date = new Date();
- JLabel datelabel = new JLabel("Your local time is: \t" + (dateFormat.format(date)));
- setVisible(false);
- JPanel panel = new JPanel();
- frame2.add(panel);
- panel.add(welcome);
- panel.add(datelabel);
- panel.add(logout);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- i++;
- }
- });
- setSize(500,500);
- setResizable(false);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setVisible(true);
- }
- public static class Hash
- {
- private static final long MASK_16_BITS = 0xFFFFL;
- private static final int MASK_BIT_1 = 0x1;
- /*
- * Paul Hsieh's Hash Function.
- *
- * @param data data to hash
- * @param dataLength length of the data, in bytes
- * @param hashedValue previous value of the hash. If it is the start of the
- * method, used the length of the data (ex.: 8 bytes).
- * @return
- */
- public static int superFastHash(long data, int hash)
- {
- int tmp;
- //int rem;
- //if (len <= 0) {
- // return 0;
- //}
- //rem = len & 3;
- //len >>= 2;
- //Main Loop
- for (int i = 0; i < 4; i += 2) {
- // Get lower 16 bits
- hash += get16BitsAligned(data, i);
- // Calculate some random value with second-lower 16 bits
- tmp = (get16BitsAligned(data, i + 1) << 11) ^ hash;
- hash = (hash << 16) ^ tmp;
- // At this point, it would advance the data, but since it is restricted
- // to longs (64-bit values), it is unnecessary).
- hash += hash >> 11;
- }
- // Handle end cases //
- // There are no end cases, main loop is done in chuncks of 32 bits.
- // Force "avalanching" of final 127 bits //
- hash ^= hash << 3;
- hash += hash >> 5;
- hash ^= hash << 4;
- hash += hash >> 17;
- hash ^= hash << 25;
- hash += hash >> 6;
- //System.out.print(hash);
- return hash;
- }
- /*
- * Returns 16 bits from the long number.
- *
- * @param data
- * @param offset one of 0 to 3
- * @return
- */
- public static int get16BitsAligned(long data, int offset)
- {
- // Normalize offset
- offset = offset%4;
- //System.out.println("offset:"+offset);
- // Align the mask
- long mask = MASK_16_BITS << 16*offset;
- //System.out.println("Mask:"+Long.toHexString(mask));
- //System.out.println("Data:"+Long.toHexString(data));
- // Get the bits
- long result = data & mask;
- // Put bits in position
- return (int) (result >>> (16*offset));
- }
- public static String mainHash(String password, String salt)
- {
- String password2 = password + salt;
- int dechash;
- byte[] bytes = password2.getBytes(StandardCharsets.US_ASCII);
- long result = 0;
- for (int i = 0; i < bytes.length; i++) {
- result <<= 8;
- result |= (bytes[i] & 0xFF);
- }
- dechash = superFastHash(result, password2.length());
- StringBuilder sb = new StringBuilder();
- sb.append(Integer.toHexString(dechash));
- if (sb.length() % 2 > 0) {
- sb.insert(0, '0'); // pad with leading zero if needed
- }
- String hex = sb.toString();
- return hex;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement