Prechádzať zdrojové kódy

Improvements suggested in #598

Sebastian Stenzel 6 rokov pred
rodič
commit
ef53561bf0

+ 4 - 3
main/ui/src/main/java/org/cryptomator/ui/util/PasswordStrengthUtil.java

@@ -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();
 		}
 	}
 

+ 4 - 5
main/ui/src/test/java/org/cryptomator/ui/util/PasswordStrengthUtilTest.java

@@ -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());
 	}
 
 }