Add a password strength meter to SetupActivity. Dev task #42.

This commit is contained in:
akwizgran
2014-01-09 01:29:00 +00:00
parent 1a53e9e908
commit ea47420e99
8 changed files with 168 additions and 14 deletions

View File

@@ -13,6 +13,7 @@ import javax.inject.Singleton;
import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.crypto.CryptoExecutor;
import org.briarproject.api.crypto.PasswordStrengthEstimator;
import org.briarproject.api.lifecycle.LifecycleManager;
import com.google.inject.AbstractModule;
@@ -40,6 +41,8 @@ public class CryptoModule extends AbstractModule {
protected void configure() {
bind(CryptoComponent.class).to(
CryptoComponentImpl.class).in(Singleton.class);
bind(PasswordStrengthEstimator.class).to(
PasswordStrengthEstimatorImpl.class);
}
@Provides @Singleton @CryptoExecutor

View File

@@ -0,0 +1,34 @@
package org.briarproject.crypto;
import java.util.HashSet;
import org.briarproject.api.crypto.PasswordStrengthEstimator;
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));
public float estimateStrength(char[] password) {
HashSet<Character> unique = new HashSet<Character>();
for(char c : password) unique.add(c);
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));
}
}