TLDR;
Is parsing a char array through the String class in the form of String.valueOf(charArray);
more safe than making an object of the String class?
I am trying to understand/extend the discussion that is had in:
Why is char[] preferred over String for passwords?
I have just recently read up a lot about how to handle passwords "securely" ish in Java, but there is still one issue I do not quite know how to solve. When you take in a password from a jPasswordField it comes as an array of chars. This is great and all because you can more easily overwrite the array with zeroes than having to wait for the garbage collector to come sweep up things like strings and whatnot.
My issue lies in the following: When I want to run an update statement in SQL to update a password, I simply CAN NOT put in the array of chars, because it is the wrong format. When I do something like Arrays.toString(arrayName);
I get it with format:
[e, a, c, h, o, f, t, h, e, l, e, t, t, e, r, s]
The only way I can put it into a database column with... let's say VARCHAR(100) without all of the commas and the spaces seems to be to write something like:
String stringPassword="";
int i=0;
for(char a:newPassword){
stringPassword=stringPassword+newPassword[i];
i++;
}
If I do something like this though, have I not just destroyed the whole point of using the array of chars? Googling around hasn't really helped me solve this one.
Thanks in advance for your time.
EDIT 1: Thanks KKaar for the possible solution:
char[] passwordArray;
String.valueOf(passwordArray);
This works just fine syntax-wise for the purposes I need it for, but I still don't know if it is safe. It doesn't make a variable out of it if I make the String.valueOf(passwordArray)
in the preparedStatement syntax, but it still parses the values through the String class. Does that mean it is still in danger, or is it safe-ish?