<commit_before>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package users;
import java.util.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.sql.*;
import org.mariadb.jdbc.Driver;
//import com.mysql.jdbc.Driver;
/**
 *
 * @author NThering
 */

// Responsible for handling user logins.
public class Public {
    /**	Checks the username and password against the stored password hashes on the database.  Returns -1 if login failed and an integer corresponding to that user's permissions if successful. */
    /** Passwords must be stored securely ( NOT IN PLAINTEXT ) so that a data breach would not compromise them. */
    
private static String driver = "org.mariadb.jdbc.Driver";
    private static String URL = "jdbc:mariadb://localhost/article_manager";
    private static String USER = "root";
    private static String PASS = "<PASSWORD>";
    private static String genPassword = null;

    public static Connection conn = null;

    public static int login(String username, String password)
    {
        /**	Checks the username and password against the stored password hashes on the database.  Returns -1 if login failed and an integer corresponding to that user's permissions if successful. */
        /** Passwords must be stored securely ( NOT IN PLAINTEXT ) so that a data breach would not compromise them. */
        try
        {
            Class.forName(driver);
            Connection connection = DriverManager.getConnection(URL, USER, PASS);

            PreparedStatement statement = connection.prepareStatement("SELECT 'username', 'password' FROM users WHERE 'username' = ? " +
                    "AND 'password' = ?");
            ResultSet r1 = statement.executeQuery();
            statement.setString(1, username);
            statement.setString(2, password);
            String usernameCounter;
            byte[] salty = salt();
            if(r1.next())
            {
                if(r1.getString("password").equals(securePassword(password, salty))) {
                    System.out.println("Login successful");
                    return 0; //return 0 for user
                }

            }

            else{
                System.out.println("Invalid Login");
            }
        }

        catch (SQLException e)
        {
            System.out.println("SQL Exception: " + e.toString());
        }
        catch (ClassNotFoundException ce)
        {
            System.out.println("Class Not Found Exception: " + ce.toString());
        } catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			System.exit(-1);
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
			System.exit(-1);
		}
        return -1;
    }

    /** Ensures that the username is unique and can be registered in the database, then registers it if so.  Returns true if registration successful and false if not. */
    public static boolean register(String username, String password) throws NoSuchAlgorithmException, NoSuchProviderException
    {
        try
        {
            Class.forName("org.mariadb.jdbc.Driver");
            Connection connection = DriverManager.getConnection(URL, USER, PASS);

            PreparedStatement st = connection.prepareStatement("select * from users order by username desc");
            PreparedStatement st2 = connection.prepareStatement("INSERT INTO users (username) VALUES (?)");
            ResultSet r1 = st.executeQuery();
            Statement s = connection.createStatement();
            String usernameCounter;
            byte[] salty = salt();
            if(r1.next())
            {
                usernameCounter = r1.getString("username");
                if(usernameCounter.equals(username))
                {
                    System.out.println("Username already exists");
                    return false;
                }

                else{

                    //add username to the database.
                    System.out.println("Username is available!");
                    st2.executeQuery();
                }
            }

            String secure = securePassword(password, salty);

            s.executeUpdate("INSERT INTO 'users'(passwords) VALUES (secure)");

        }

        catch (SQLException e)
        {
            System.out.println("SQL Excpetion: " + e.toString());
        }

        catch (ClassNotFoundException ce)
        {
            System.out.println("Class Not Found Exception: " + ce.toString());
        }
        return true;<commit_msg>Changed cryptographic throws declarations in users.public.register method into catch clauses.<commit_after>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package users;
import java.util.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.sql.*;
import org.mariadb.jdbc.Driver;
//import com.mysql.jdbc.Driver;
/**
 *
 * @author NThering
 */

// Responsible for handling user logins.
public class Public {
    /**	Checks the username and password against the stored password hashes on the database.  Returns -1 if login failed and an integer corresponding to that user's permissions if successful. */
    /** Passwords must be stored securely ( NOT IN PLAINTEXT ) so that a data breach would not compromise them. */
    
private static String driver = "org.mariadb.jdbc.Driver";
    private static String URL = "jdbc:mariadb://localhost/article_manager";
    private static String USER = "root";
    private static String PASS = "<PASSWORD>";
    private static String genPassword = null;

    public static Connection conn = null;

    public static int login(String username, String password)
    {
        /**	Checks the username and password against the stored password hashes on the database.  Returns -1 if login failed and an integer corresponding to that user's permissions if successful. */
        /** Passwords must be stored securely ( NOT IN PLAINTEXT ) so that a data breach would not compromise them. */
        try
        {
            Class.forName(driver);
            Connection connection = DriverManager.getConnection(URL, USER, PASS);

            PreparedStatement statement = connection.prepareStatement("SELECT 'username', 'password' FROM users WHERE 'username' = ? " +
                    "AND 'password' = ?");
            ResultSet r1 = statement.executeQuery();
            statement.setString(1, username);
            statement.setString(2, password);
            String usernameCounter;
            byte[] salty = salt();
            if(r1.next())
            {
                if(r1.getString("password").equals(securePassword(password, salty))) {
                    System.out.println("Login successful");
                    return 0; //return 0 for user
                }

            }

            else{
                System.out.println("Invalid Login");
            }
        }

        catch (SQLException e)
        {
            System.out.println("SQL Exception: " + e.toString());
        }
        catch (ClassNotFoundException ce)
        {
            System.out.println("Class Not Found Exception: " + ce.toString());
        } catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			System.exit(-1);
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
			System.exit(-1);
		}
        return -1;
    }

    /** Ensures that the username is unique and can be registered in the database, then registers it if so.  Returns true if registration successful and false if not. */
    public static boolean register(String username, String password)
    {
        try
        {
            Class.forName("org.mariadb.jdbc.Driver");
            Connection connection = DriverManager.getConnection(URL, USER, PASS);

            PreparedStatement st = connection.prepareStatement("select * from users order by username desc");
            PreparedStatement st2 = connection.prepareStatement("INSERT INTO users (username) VALUES (?)");
            ResultSet r1 = st.executeQuery();
            Statement s = connection.createStatement();
            String usernameCounter;
            byte[] salty = salt();
            if(r1.next())
            {
                usernameCounter = r1.getString("username");
                if(usernameCounter.equals(username))
                {
                    System.out.println("Username already exists");
                    return false;
                }

                else{

                    //add username to the database.
                    System.out.println("Username is available!");
                    st2.executeQuery();
                }
            }

            String secure = securePassword(password, salty);

            s.executeUpdate("INSERT INTO 'users'(passwords) VALUES (secure)");

        }

        catch (SQLException e)
        {
            System.out.println("SQL Exception: " + e.toString());
        }

        catch (ClassNotFoundException ce)
        {
            System.out.println("Class Not Found Exception: " + ce.toString());
        } catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			System.exit(-1);
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
			System.exit(-1);
		}
        return true;
{'fraction_non_alphanumeric': 0.09580838323353294, 'fraction_numerical': 0.003716704521990502, 'mean_word_length': 2.1739842726081258, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3314607', 'n_tokens_mistral': 2422, 'n_tokens_neox': 2282, 'n_words': 749}