diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/crypto/PasswordStrengthEstimator.java b/bramble-api/src/main/java/org/briarproject/bramble/api/crypto/PasswordStrengthEstimator.java index 0fdb4a8ba55402c9cc005a53a6974c7a2f7926b8..f9cf35561df10953dfd6ba89e0cc82b9e9f94677 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/crypto/PasswordStrengthEstimator.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/crypto/PasswordStrengthEstimator.java @@ -6,9 +6,9 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault; public interface PasswordStrengthEstimator { float NONE = 0; - float WEAK = 0.4f; - float QUITE_WEAK = 0.6f; - float QUITE_STRONG = 0.8f; + float WEAK = 0.25f; + float QUITE_WEAK = 0.5f; + float QUITE_STRONG = 0.75f; float STRONG = 1; /** diff --git a/bramble-core/src/main/java/org/briarproject/bramble/crypto/PasswordStrengthEstimatorImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/crypto/PasswordStrengthEstimatorImpl.java index ad1a0148d8b90b06f37238249936e229885b05fc..67f745239b61fee6566f9b2aeeff6a6882ad9571 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/crypto/PasswordStrengthEstimatorImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/crypto/PasswordStrengthEstimatorImpl.java @@ -11,31 +11,14 @@ import javax.annotation.concurrent.Immutable; @NotNullByDefault class PasswordStrengthEstimatorImpl implements PasswordStrengthEstimator { - private static final int LOWER = 26; - private static final int UPPER = 26; - private static final int DIGIT = 10; - private static final int OTHER = 10; - private static final double STRONG = Math.log(Math.pow(LOWER + UPPER + - DIGIT + OTHER, 10)); + // The minimum number of unique characters in a strong password + private static final int STRONG_UNIQUE_CHARS = 12; @Override public float estimateStrength(String password) { HashSet<Character> unique = new HashSet<Character>(); int length = password.length(); for (int i = 0; i < length; i++) unique.add(password.charAt(i)); - boolean lower = false, upper = false, digit = false, other = false; - for (char c : unique) { - if (Character.isLowerCase(c)) lower = true; - else if (Character.isUpperCase(c)) upper = true; - else if (Character.isDigit(c)) digit = true; - else other = true; - } - int alphabetSize = 0; - if (lower) alphabetSize += LOWER; - if (upper) alphabetSize += UPPER; - if (digit) alphabetSize += DIGIT; - if (other) alphabetSize += OTHER; - double score = Math.log(Math.pow(alphabetSize, unique.size())); - return Math.min(1, (float) (score / STRONG)); + return Math.min(1, (float) unique.size() / STRONG_UNIQUE_CHARS); } } diff --git a/bramble-core/src/test/java/org/briarproject/bramble/crypto/PasswordStrengthEstimatorImplTest.java b/bramble-core/src/test/java/org/briarproject/bramble/crypto/PasswordStrengthEstimatorImplTest.java index 28f7e4bd211d534790bbcc85e7233b7c547589eb..8010ba4ad7cee8dab0d598a43fb398322cf9da4a 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/crypto/PasswordStrengthEstimatorImplTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/crypto/PasswordStrengthEstimatorImplTest.java @@ -4,6 +4,7 @@ import org.briarproject.bramble.api.crypto.PasswordStrengthEstimator; import org.briarproject.bramble.test.BrambleTestCase; import org.junit.Test; +import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.NONE; import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.QUITE_STRONG; import static org.junit.Assert.assertTrue; @@ -12,7 +13,7 @@ public class PasswordStrengthEstimatorImplTest extends BrambleTestCase { @Test public void testWeakPasswords() { PasswordStrengthEstimator e = new PasswordStrengthEstimatorImpl(); - assertTrue(e.estimateStrength("") < QUITE_STRONG); + assertTrue(e.estimateStrength("") == NONE); assertTrue(e.estimateStrength("password") < QUITE_STRONG); assertTrue(e.estimateStrength("letmein") < QUITE_STRONG); assertTrue(e.estimateStrength("123456") < QUITE_STRONG); diff --git a/briar-android/src/main/java/org/briarproject/briar/android/login/ChangePasswordActivity.java b/briar-android/src/main/java/org/briarproject/briar/android/login/ChangePasswordActivity.java index 6bd309d5c48b3e8563466dfa5045e7182460f2ec..cea17a2ebb9c9ff47c80b7cb2daa08b22eb8b79b 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/login/ChangePasswordActivity.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/login/ChangePasswordActivity.java @@ -25,7 +25,7 @@ import javax.inject.Inject; import static android.view.View.INVISIBLE; import static android.view.View.VISIBLE; -import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.WEAK; +import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.QUITE_WEAK; public class ChangePasswordActivity extends BaseActivity implements OnClickListener, OnEditorActionListener { @@ -109,13 +109,13 @@ public class ChangePasswordActivity extends BaseActivity strengthMeter.setStrength(strength); UiUtils.setError(newPasswordEntryWrapper, getString(R.string.password_too_weak), - firstPassword.length() > 0 && strength < WEAK); + firstPassword.length() > 0 && strength < QUITE_WEAK); UiUtils.setError(newPasswordConfirmationWrapper, getString(R.string.passwords_do_not_match), secondPassword.length() > 0 && !passwordsMatch); changePasswordButton.setEnabled( !currentPassword.getText().toString().isEmpty() && - passwordsMatch && strength >= WEAK); + passwordsMatch && strength >= QUITE_WEAK); } @Override diff --git a/briar-android/src/main/java/org/briarproject/briar/android/login/SetupActivity.java b/briar-android/src/main/java/org/briarproject/briar/android/login/SetupActivity.java index decf81f76b935f086cf17a75286d771f9e88d916..767a9eb558737a7a93735d5a27f589b060c44eac 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/login/SetupActivity.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/login/SetupActivity.java @@ -28,7 +28,7 @@ import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; import static android.view.View.GONE; import static android.view.View.INVISIBLE; import static android.view.View.VISIBLE; -import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.WEAK; +import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.QUITE_WEAK; import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH; public class SetupActivity extends BaseActivity implements OnClickListener, @@ -115,13 +115,13 @@ public class SetupActivity extends BaseActivity implements OnClickListener, nicknameLength > MAX_AUTHOR_NAME_LENGTH); UiUtils.setError(passwordEntryWrapper, getString(R.string.password_too_weak), - firstPassword.length() > 0 && strength < WEAK); + firstPassword.length() > 0 && strength < QUITE_WEAK); UiUtils.setError(passwordConfirmationWrapper, getString(R.string.passwords_do_not_match), secondPassword.length() > 0 && !passwordsMatch); createAccountButton.setEnabled(nicknameLength > 0 && nicknameLength <= MAX_AUTHOR_NAME_LENGTH - && passwordsMatch && strength >= WEAK); + && passwordsMatch && strength >= QUITE_WEAK); } @Override diff --git a/briar-android/src/test/java/org/briarproject/briar/android/login/ChangePasswordActivityTest.java b/briar-android/src/test/java/org/briarproject/briar/android/login/ChangePasswordActivityTest.java index d163c8c509ff14700ab80d7f959b785f7976d0ac..5721921889c6c0d9af6bdb0c9f8d5b89794920a8 100644 --- a/briar-android/src/test/java/org/briarproject/briar/android/login/ChangePasswordActivityTest.java +++ b/briar-android/src/test/java/org/briarproject/briar/android/login/ChangePasswordActivityTest.java @@ -193,7 +193,7 @@ public class ChangePasswordActivityTest { // Mock answers for UI testing only when(mockedController.estimatePasswordStrength("strong")).thenReturn( STRONG); - when(mockedController.estimatePasswordStrength("qstring")).thenReturn( + when(mockedController.estimatePasswordStrength("qstrong")).thenReturn( QUITE_STRONG); when(mockedController.estimatePasswordStrength("qweak")).thenReturn( QUITE_WEAK); @@ -205,9 +205,9 @@ public class ChangePasswordActivityTest { testStrengthMeter("strong", STRONG, StrengthMeter.GREEN); Mockito.verify(mockedController, Mockito.times(1)) .estimatePasswordStrength(eq("strong")); - testStrengthMeter("qstring", QUITE_STRONG, StrengthMeter.LIME); + testStrengthMeter("qstrong", QUITE_STRONG, StrengthMeter.LIME); Mockito.verify(mockedController, Mockito.times(1)) - .estimatePasswordStrength(eq("qstring")); + .estimatePasswordStrength(eq("qstrong")); testStrengthMeter("qweak", QUITE_WEAK, StrengthMeter.YELLOW); Mockito.verify(mockedController, Mockito.times(1)) .estimatePasswordStrength(eq("qweak")); diff --git a/briar-android/src/test/java/org/briarproject/briar/android/login/SetupActivityTest.java b/briar-android/src/test/java/org/briarproject/briar/android/login/SetupActivityTest.java index 86472493c7cfd2bfb5aa77635b4f10e3836bf372..2d500c998ba5f9c36699e93358f5423ac3857217 100644 --- a/briar-android/src/test/java/org/briarproject/briar/android/login/SetupActivityTest.java +++ b/briar-android/src/test/java/org/briarproject/briar/android/login/SetupActivityTest.java @@ -196,7 +196,7 @@ public class SetupActivityTest { // Mock answers for UI testing only when(mockedController.estimatePasswordStrength("strong")).thenReturn( STRONG); - when(mockedController.estimatePasswordStrength("qstring")).thenReturn( + when(mockedController.estimatePasswordStrength("qstrong")).thenReturn( QUITE_STRONG); when(mockedController.estimatePasswordStrength("qweak")).thenReturn( QUITE_WEAK); @@ -208,9 +208,9 @@ public class SetupActivityTest { testStrengthMeter("strong", STRONG, StrengthMeter.GREEN); Mockito.verify(mockedController, Mockito.times(1)) .estimatePasswordStrength(eq("strong")); - testStrengthMeter("qstring", QUITE_STRONG, StrengthMeter.LIME); + testStrengthMeter("qstrong", QUITE_STRONG, StrengthMeter.LIME); Mockito.verify(mockedController, Mockito.times(1)) - .estimatePasswordStrength(eq("qstring")); + .estimatePasswordStrength(eq("qstrong")); testStrengthMeter("qweak", QUITE_WEAK, StrengthMeter.YELLOW); Mockito.verify(mockedController, Mockito.times(1)) .estimatePasswordStrength(eq("qweak"));