diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-10-20 22:04:50 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-10-20 22:07:18 +0200 |
commit | 83ead3b90165775af08dd57f1a86de208d124261 (patch) | |
tree | c7829efb87b01eb9d1b5861f8c4da8af0cf2c1c2 /src/main/java/de/pixart/messenger | |
parent | b6d64f1f4338c6c8e0c3ed91983da6aa16024fe0 (diff) |
refactor magic creates password gen to CryptoHelper
Diffstat (limited to 'src/main/java/de/pixart/messenger')
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/MagicCreateActivity.java | 16 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/utils/CryptoHelper.java | 18 |
2 files changed, 15 insertions, 19 deletions
diff --git a/src/main/java/de/pixart/messenger/ui/MagicCreateActivity.java b/src/main/java/de/pixart/messenger/ui/MagicCreateActivity.java index b8f11be12..10c68cc1d 100644 --- a/src/main/java/de/pixart/messenger/ui/MagicCreateActivity.java +++ b/src/main/java/de/pixart/messenger/ui/MagicCreateActivity.java @@ -22,6 +22,7 @@ import java.util.List; import de.pixart.messenger.Config; import de.pixart.messenger.R; import de.pixart.messenger.entities.Account; +import de.pixart.messenger.utils.CryptoHelper; import rocks.xmpp.addr.Jid; public class MagicCreateActivity extends XmppActivity implements TextWatcher, AdapterView.OnItemSelectedListener { @@ -29,12 +30,8 @@ public class MagicCreateActivity extends XmppActivity implements TextWatcher, Ad private TextView mFullJidDisplay; private EditText mUsername; private Spinner mServer; - private SecureRandom mRandom; String domain = null; - private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456780+-/#$!?"; - private static final int PW_LENGTH = 10; - @Override protected void refreshUiReal() { @@ -74,7 +71,6 @@ public class MagicCreateActivity extends XmppActivity implements TextWatcher, Ad mServer.setSelection(defaultServer); mServer.setOnItemSelectedListener(this); adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice); - mRandom = new SecureRandom(); Button next = findViewById(R.id.create_account); next.setOnClickListener(v -> { try { @@ -90,7 +86,7 @@ public class MagicCreateActivity extends XmppActivity implements TextWatcher, Ad 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); @@ -115,14 +111,6 @@ public class MagicCreateActivity extends XmppActivity implements TextWatcher, Ad 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) { diff --git a/src/main/java/de/pixart/messenger/utils/CryptoHelper.java b/src/main/java/de/pixart/messenger/utils/CryptoHelper.java index d34823d86..e16d0d012 100644 --- a/src/main/java/de/pixart/messenger/utils/CryptoHelper.java +++ b/src/main/java/de/pixart/messenger/utils/CryptoHelper.java @@ -35,15 +35,15 @@ import rocks.xmpp.addr.Jid; public final class CryptoHelper { + 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(); - public static final String FILETRANSFER = "?FILETRANSFERv1:"; 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}; - public static String bytesToHex(byte[] bytes) { char[] hexChars = new char[bytes.length * 2]; for (int j = 0; j < bytes.length; j++) { @@ -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(); @@ -293,4 +301,4 @@ public final class CryptoHelper { final String u = url.toLowerCase(); return !u.contains(" ") && (u.startsWith("https://") || u.startsWith("http://") || u.startsWith("p1s3://")) && u.endsWith(".pgp"); } -} +}
\ No newline at end of file |