BIP39: Japanese word separator

This commit is contained in:
M66B 2022-03-08 11:51:35 +01:00
parent 6cacc1852e
commit 9a6db58bc9
2 changed files with 14 additions and 12 deletions

View File

@ -1,6 +1,7 @@
package eu.faircode.email;
import android.content.Context;
import android.text.TextUtils;
import androidx.annotation.NonNull;
@ -9,12 +10,23 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class BIP39 {
// https://github.com/bitcoin/bips/tree/master/bip-0039
// https://github.com/bitcoin/bips/pull/1129
static String getWord(@NonNull Locale locale, int index, Context context) {
static String getMnemonic(@NonNull Locale locale, int words, Context context) {
List<String> list = new ArrayList<>();
SecureRandom rnd = new SecureRandom();
for (int i = 0; i < words; i++)
list.add(BIP39.getWord(locale, rnd.nextInt(2048), context));
String delimiter = ("ja".equals(locale.getLanguage()) ? "\u3000" : " ");
return TextUtils.join(delimiter, list);
}
private static String getWord(@NonNull Locale locale, int index, Context context) {
String lang = locale.getLanguage();
if ("zh".equals(lang) && "CN".equals(locale.getCountry()))
lang = "zh_cn";

View File

@ -58,12 +58,8 @@ import androidx.preference.PreferenceManager;
import androidx.webkit.WebViewFeature;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.security.SecureRandom;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class FragmentOptionsPrivacy extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener {
@ -466,13 +462,7 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
if (checked) {
Context context = compoundButton.getContext();
Locale locale = Locale.getDefault();
List<String> words = new ArrayList<>();
SecureRandom rnd = new SecureRandom();
for (int i = 0; i < BIP39_WORDS; i++)
words.add(BIP39.getWord(locale, rnd.nextInt(2048), context));
String mnemonic = TextUtils.join(" ", words);
String mnemonic = BIP39.getMnemonic(Locale.getDefault(), BIP39_WORDS, context);
prefs.edit().putString("wipe_mnemonic", mnemonic).apply();
tvMnemonic.setText(mnemonic);