0

I'm trying to learn Java and as part of a task I'm working on, a user will sign up with a username and password which then get hashed using Java's hashCode() and added to a text file in the form of username,password.

Now when a user logs in, I am attempting to hash their input and compare it to the .txt file to ensure the correct credentials have been entered.

I originally did this by searching the .txt file line by line until the entered username appeared using the contains method by which I then split the line at the comma giving me the password for the entered username to check.

How could I do this same thing but now with a hashed int value? Please see my code for reference

Any help would be greatly appreciated!

Currently, I have tried converting the hashed value back to String then using contains which it will not allow.

int UsernameHash = Username.hashCode();
int PasswordHash = Password.hashCode();

@SuppressWarnings("resource")
Scanner FileContent = new Scanner(new File("Users.txt"));

while (FileContent.hasNextLine()) {

    String Line = "";
    Line = FileContent.nextLine();

    if(Line.contains(UsernameHash)) {

        String FullLine = Line;
        String[] loginAuth = FullLine.split(",");
        String Pass = loginAuth[1];

    if(Pass.equals(PasswordHash)) {

        System.out.println("\n Thank You for logging in, please proceed!");
Mischiefz
  • 127
  • 16
  • see this – vijay Apr 01 '19 at 19:18
  • That is absolutely perfect! Thank You for such the quick response without ripping it apart as now i can still understand fully what is happening - Apologies if my explanation was a little basic as I'm just getting into Java really – Sean Hawdon Apr 01 '19 at 19:45

2 Answers2

0

If you use Integer instead of int you gain access to a toString method that can allow you to compare its value to a string's value.

Try this,

 public static void main(String[] args) throws FileNotFoundException
    {
        @SuppressWarnings("resource")
        Scanner fileContent = new Scanner(new File("Users.txt"));
        Integer usernameHash = 42321;

        // Integer usernameHash = 42321;
        while (fileContent.hasNextLine())
        {

            String line = fileContent.nextLine();
            if (line.equals(usernameHash.toString()))
            {
                System.out.println("Match");
            }
        }
    }

It also works with contains

if(line.contains(usernameHash.toString())) {
        System.out.println("Match");
 }

If you need to get the integer value from a String, you can do:

int array[] = new int [2];
String tempPassword = "55325";
array[1] = Integer.parseInt(temp);

If this is not what you are looking for let me know.

Juseeth
  • 467
  • 1
  • 6
  • 18
Julian Peffer
  • 307
  • 1
  • 4
0

First of all, just to share some thought, i know you are just trying to learn java, but we should never use hash code to test the uniqueness of anything. Because hash code is not unique, there will be user names and passwords which will generate same hash code and you will end up allowing non-authorized user into the system. Another for converting primitive types to String object, you can use static method in the String class, String.valueOf(...).

int usernameHash = username.hashCode();  
int passwordHash = password.hashCode();

if (line.contains(String.valueOf(usernameHash)) {
        System.out.println("Match"); 
}
Juseeth
  • 467
  • 1
  • 6
  • 18