refactor magic creates password gen to CryptoHelper

This commit is contained in:
Daniel Gultsch 2018-10-20 00:05:10 +02:00
parent 157ebbac52
commit f9cafc144b
2 changed files with 243 additions and 247 deletions

View file

@ -16,16 +16,13 @@ import java.security.SecureRandom;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.utils.CryptoHelper;
import rocks.xmpp.addr.Jid;
public class MagicCreateActivity extends XmppActivity implements TextWatcher {
private TextView mFullJidDisplay;
private EditText mUsername;
private SecureRandom mRandom;
private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456780+-/#$!?";
private static final int PW_LENGTH = 10;
@Override
protected void refreshUiReal() {
@ -57,7 +54,6 @@ public class MagicCreateActivity extends XmppActivity implements TextWatcher {
configureActionBar(getSupportActionBar());
mFullJidDisplay = findViewById(R.id.full_jid);
mUsername = findViewById(R.id.username);
mRandom = new SecureRandom();
Button next = findViewById(R.id.create_account);
next.setOnClickListener(v -> {
try {
@ -70,7 +66,7 @@ public class MagicCreateActivity extends XmppActivity implements TextWatcher {
mUsername.setError(null);
Account account = xmppConnectionService.findAccountByJid(jid);
if (account == null) {
account = new Account(jid, createPassword());
account = new Account(jid, CryptoHelper.createPassword(new SecureRandom()));
account.setOption(Account.OPTION_REGISTER, true);
account.setOption(Account.OPTION_DISABLED, true);
account.setOption(Account.OPTION_MAGIC_CREATE, true);
@ -92,14 +88,6 @@ public class MagicCreateActivity extends XmppActivity implements TextWatcher {
mUsername.addTextChangedListener(this);
}
private String createPassword() {
StringBuilder builder = new StringBuilder(PW_LENGTH);
for (int i = 0; i < PW_LENGTH; ++i) {
builder.append(CHARS.charAt(mRandom.nextInt(CHARS.length() - 1)));
}
return builder.toString();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

View file

@ -36,13 +36,13 @@ import rocks.xmpp.addr.Jid;
public final class CryptoHelper {
private static final char[] VOWELS = "aeiou".toCharArray();
private static final char[] CONSONANTS = "bcfghjklmnpqrstvwxyz".toCharArray();
private final static char[] hexArray = "0123456789abcdef".toCharArray();
public static final Pattern UUID_PATTERN = Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}");
final public static byte[] ONE = new byte[]{0, 0, 0, 1};
private static final char[] CHARS = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz123456789+-/#$!?".toCharArray();
private static final int PW_LENGTH = 10;
private static final char[] VOWELS = "aeiou".toCharArray();
private static final char[] CONSONANTS = "bcfghjklmnpqrstvwxyz".toCharArray();
private final static char[] hexArray = "0123456789abcdef".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
@ -54,6 +54,14 @@ public final class CryptoHelper {
return new String(hexChars);
}
public static String createPassword(SecureRandom random) {
StringBuilder builder = new StringBuilder(PW_LENGTH);
for (int i = 0; i < PW_LENGTH; ++i) {
builder.append(CHARS[random.nextInt(CHARS.length - 1)]);
}
return builder.toString();
}
public static String pronounceable(SecureRandom random) {
char[] output = new char[random.nextInt(4) * 2 + 5];
boolean vowel = random.nextBoolean();