1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-12-10 14:21:09 +01:00

Trim passphrases

This commit is contained in:
Paul Schaub 2021-05-20 12:40:12 +02:00
parent a72cff28d8
commit 7e2c89b1b3
2 changed files with 73 additions and 1 deletions

View file

@ -32,7 +32,48 @@ public class Passphrase {
* @param chars may be null for empty passwords.
*/
public Passphrase(@Nullable char[] chars) {
this.chars = chars;
if (chars == null) {
this.chars = null;
} else {
char[] trimmed = removeTrailingAndLeadingWhitespace(chars);
if (trimmed.length == 0) {
this.chars = null;
} else {
this.chars = trimmed;
}
}
}
/**
* Return a copy of the passed in char array, with leading and trailing whitespace characters removed.
*
* @param chars char array
* @return copy of char array with leading and trailing whitespace characters removed
*/
private static char[] removeTrailingAndLeadingWhitespace(char[] chars) {
int i = 0;
while (i < chars.length && isWhitespace(chars[i])) {
i++;
}
int j = chars.length - 1;
while (j >= i && isWhitespace(chars[j])) {
j--;
}
char[] trimmed = new char[chars.length - i - (chars.length - 1 - j)];
System.arraycopy(chars, i, trimmed, 0, trimmed.length);
return trimmed;
}
/**
* Return true, if the passed in char is a whitespace symbol (space, newline, tab).
*
* @param xar char
* @return true if whitespace
*/
private static boolean isWhitespace(char xar) {
return xar == ' ' || xar == '\n' || xar == '\t';
}
/**