0

I've implemented an SHA-1 hash function in a server program I wrote, which generates a .txt file of username password pairs with the passwords represented by their respective hash values.

I am having trouble retrieving the hash values from the file without corrupting them. All "array copy"-type functions and any sort of movement into byte arrays seems to corrupt them. (Their values are correct when they're initially read in as Strings.

The goal is to have valid passwords after reading them in and entering them into the "sha1.isEqual(byte[],byte[])" function.

Here's the method where I read in the passwords

public void getPasswords()
{   
    String[] splitString = null;

    BufferedReader reader = null;

    try {
        File file = new File("hash_user_pass.txt");
        reader = new BufferedReader(new FileReader(file));
        int i = 0;
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.isEmpty()) continue; 

            splitString = line.split(" ");

            //System.out.println(splitString[0]);
            //System.out.println(splitString[1]);

            usernames.add(splitString[0]);
            passwords.add(splitString[1]);

            //passwords.add(splitString[1].getBytes());
            //System.out.println("Password Bytes: " + passwords.get(i));
            i++;
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

And here's where I compare them to the user's password

    hashed_pass = hash(password);

    if(sha1.isEqual(passwords.get(i),hashed_pass))
    {
        System.out.println("Passwords match.");
    }
Andrew Feather
  • 173
  • 2
  • 14

0 Answers0