Improvements suggested in #598

This commit is contained in:
Sebastian Stenzel
2018-06-21 17:47:47 +02:00
parent 3165c4ba86
commit ef53561bf0
2 changed files with 8 additions and 8 deletions
@@ -28,6 +28,8 @@ import javafx.scene.paint.Color;
@Singleton
public class PasswordStrengthUtil {
private static final int PW_TRUNC_LEN = 100; // truncate very long passwords, since zxcvbn memory and runtime depends vastly on the length
private final Zxcvbn zxcvbn;
private final List<String> sanitizedInputs;
private final Localization localization;
@@ -43,10 +45,9 @@ public class PasswordStrengthUtil {
public int computeRate(String password) {
if (Strings.isNullOrEmpty(password)) {
return -1;
} else if (password.length() > 100) {
return 4; // assume this is strong. zxcvbn memory and runtime depends vastly on the password length
} else {
return zxcvbn.measure(password, sanitizedInputs).getScore();
int numCharsToRate = Math.min(PW_TRUNC_LEN, password.length());
return zxcvbn.measure(password.substring(0, numCharsToRate), sanitizedInputs).getScore();
}
}
@@ -7,15 +7,14 @@ import org.mockito.Mockito;
public class PasswordStrengthUtilTest {
@Test
public void testLongPasswordsWillBeRatedAsStrong() {
@Test(timeout = 5000)
public void testLongPasswords() {
PasswordStrengthUtil util = new PasswordStrengthUtil(Mockito.mock(Localization.class));
StringBuilder longPwBuilder = new StringBuilder();
for (int i = 0; i < 101; i++) {
for (int i = 0; i < 10000; i++) {
longPwBuilder.append('x');
}
int strength = util.computeRate(longPwBuilder.toString());
Assert.assertEquals(4, strength);
util.computeRate(longPwBuilder.toString());
}
}