0

how do I call the "checkpassowrd" method in the "LoginUser" class?(password must be byte []) I have to write other information in order to send the question but I think the code is very clear

first class

public final class Password {
protected void setPassword (byte[] pass) throws Exception{
    byte[] salt = generateSalt(12);
    byte[] input = appendArrays(pass, salt);
    clearArray(pass);
    saveBytes(salt, "salt.txt");
    saveBytes(input, "password.txt");
    clearArray(salt);
    clearArray(input);
}

protected boolean checkpassword(byte[] pass) throws Exception{
    byte[] salt = loadByte("salt.txt");
    byte[] input = appendArrays(pass, salt);
    clearArray(pass);
    byte[] pass_stored = loadByte("password.txt");
    boolean arraysEqual = Arrays.equals(input, pass_stored);
    clearArray(input);
    clearArray(pass_stored);
    return arraysEqual;
}

protected byte[] generateSalt(int n) {
    final Random r = new SecureRandom();
    byte[] salt = new byte[32];
    r.nextBytes(salt);
    return salt;
}

protected byte[] appendArrays(byte[]a, byte[]b) {
    byte[] result = new byte[a.length + b.length]; 
    System.arraycopy(a, 0, result, 0, a.length); 
    System.arraycopy(b, 0, result, a.length, b.length); 
    return result;
}

protected void clearArray(byte[]a) {
    for (int i = 0; i<a.length; i++) {
        a[i] = 0;
    }
}

protected void saveBytes(byte[]a, String path) {
    try (FileOutputStream db = new FileOutputStream("fullPathToFile")) {
        db.write(a);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

protected byte[] loadByte(String path) throws IOException {
    byte[] array = Files.readAllBytes(Paths.get("path"));
    return array;
}}

second class

public final class User { private String username; Password password; protected boolean valid; protected Password getPassword() {
    return password;
}

protected void setPassword(Password newPassword) {
    password = newPassword;
}

protected String getUsername() {
    return username;
}

protected void setUserName(String newUsername) {
    username = newUsername;
}

protected boolean isValid() {
    return valid;
}

protected void setValid(boolean newValid) {
    valid = newValid;
}

third class. I have to write other information in order to send the question but I think the code is very clear

public final class LoginUser { 
    public User login(User user) {
        String username = user.getUsername();    
        Password password = user.getPassword();

        boolean verify = checkpassword(password);

        return user;
    }
}
Dave Newton
  • 158,873
  • 26
  • 254
  • 302

1 Answers1

0

Since checkpassword() is an instance method in the Password class, you need an instance of Password to call it on.

In your LoginUser class, just do:

boolean verify = password.checkpassword(password);

Or, alternatively, you could make checkpassword() static and just call it on the Password class like so:

protected static boolean checkpassword(byte[] pass) throws Exception{

And then call it like this:

boolean verify = Password.checkpassword(password);
Diana Shao
  • 61
  • 1
  • 8