in my code example i generate a random byte array that I have to use as salt with my password field.
The problem occurs when in my byte array there is a NUL character. I have founded a way to remove this character using strings but for security reasons I'd like to use array and not string to remove this values.
Below a snippets whith my solution. In this example I remove NUL occurences but could be a good choice to replace them. How can I replace it? Thanks.
private static byte[] generateSalt() throws UnsupportedEncodingException, NoSuchAlgorithmException{
final Random RANDOM = new SecureRandom();
byte[] salt = new byte[12];
RANDOM.nextBytes(salt);
String str = new String(salt, StandardCharsets.UTF_8);
str.replaceAll("\u0000", "");
salt = str.getBytes("UTF-8");
return salt;
}