mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-26 22:32:07 +01:00
Trim passphrases
This commit is contained in:
parent
a72cff28d8
commit
7e2c89b1b3
2 changed files with 73 additions and 1 deletions
|
@ -32,7 +32,48 @@ public class Passphrase {
|
||||||
* @param chars may be null for empty passwords.
|
* @param chars may be null for empty passwords.
|
||||||
*/
|
*/
|
||||||
public Passphrase(@Nullable char[] chars) {
|
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';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -17,6 +17,7 @@ package org.pgpainless.key.protection;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
@ -37,4 +38,34 @@ public class PassphraseTest {
|
||||||
assertFalse(passphrase.isValid());
|
assertFalse(passphrase.isValid());
|
||||||
assertThrows(IllegalStateException.class, passphrase::getChars);
|
assertThrows(IllegalStateException.class, passphrase::getChars);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTrimming() {
|
||||||
|
Passphrase leadingSpace = Passphrase.fromPassword(" space");
|
||||||
|
assertArrayEquals("space".toCharArray(), leadingSpace.getChars());
|
||||||
|
assertFalse(leadingSpace.isEmpty());
|
||||||
|
|
||||||
|
Passphrase trailingSpace = Passphrase.fromPassword("space ");
|
||||||
|
assertArrayEquals("space".toCharArray(), trailingSpace.getChars());
|
||||||
|
assertFalse(trailingSpace.isEmpty());
|
||||||
|
|
||||||
|
Passphrase leadingTrailingWhitespace = new Passphrase("\t Such whitespace, much wow\n ".toCharArray());
|
||||||
|
assertArrayEquals("Such whitespace, much wow".toCharArray(), leadingTrailingWhitespace.getChars());
|
||||||
|
assertFalse(leadingTrailingWhitespace.isEmpty());
|
||||||
|
|
||||||
|
Passphrase fromEmptyChars = new Passphrase(" ".toCharArray());
|
||||||
|
assertNull(fromEmptyChars.getChars());
|
||||||
|
assertTrue(fromEmptyChars.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEmptyPassphrase() {
|
||||||
|
Passphrase empty = Passphrase.emptyPassphrase();
|
||||||
|
assertNull(empty.getChars());
|
||||||
|
assertTrue(empty.isEmpty());
|
||||||
|
|
||||||
|
Passphrase trimmedEmpty = Passphrase.fromPassword(" ");
|
||||||
|
assertNull(trimmedEmpty.getChars());
|
||||||
|
assertTrue(trimmedEmpty.isEmpty());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue