Choose kdf and encapsulate code

This commit is contained in:
J-Jamet committed 2018-04-28 15:58:04 +02:00
1 parent fbf3dec421
commit 3d584b76f7
47 files changed
+702 -278

No files matched your search

@@ -19,6 +19,9 @@
*/
package com.kunzisoft.keepass.crypto.keyDerivation;
import android.content.res.Resources;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.crypto.CryptoUtil;
import com.kunzisoft.keepass.crypto.finalkey.FinalKey;
import com.kunzisoft.keepass.crypto.finalkey.FinalKeyFactory;
@@ -45,8 +48,11 @@ public class AesKdf extends KdfEngine {
uuid = CIPHER_UUID;
}
public String getName() {
return DEFAULT_NAME;
@Override
public String getName(Resources resources) {
if (resources == null)
return DEFAULT_NAME;
return resources.getString(R.string.kdf_AES);
}
@Override
@@ -93,4 +99,9 @@ public class AesKdf extends KdfEngine {
public void setKeyRounds(KdfParameters p, long keyRounds) {
p.setUInt64(ParamRounds, keyRounds);
}
@Override
public long getDefaultKeyRounds() {
return DEFAULT_ROUNDS;
}
}
@@ -19,6 +19,9 @@
*/
package com.kunzisoft.keepass.crypto.keyDerivation;
import android.content.res.Resources;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.utils.Types;
import java.io.IOException;
@@ -26,6 +29,7 @@ import java.security.SecureRandom;
import java.util.UUID;
public class Argon2Kdf extends KdfEngine {
public static final UUID CIPHER_UUID = Types.bytestoUUID(
new byte[]{(byte) 0xEF, (byte) 0x63, (byte) 0x6D, (byte) 0xDF, (byte) 0x8C, (byte) 0x29, (byte) 0x44, (byte) 0x4B,
(byte) 0x91, (byte) 0xF7, (byte) 0xA9, (byte) 0xA4, (byte)0x03, (byte) 0xE3, (byte) 0x0A, (byte) 0x0C
@@ -58,13 +62,17 @@ public class Argon2Kdf extends KdfEngine {
private static final long DefaultMemory = 1024 * 1024;
private static final long DefaultParallelism = 2;
private static final String DEFAULT_NAME = "Argon2";
public Argon2Kdf() {
uuid = CIPHER_UUID;
}
@Override
public String getName() {
return "Argon2";
public String getName(Resources resources) {
if (resources == null)
return DEFAULT_NAME;
return resources.getString(R.string.kdf_Argon2);
}
@Override
@@ -113,4 +121,9 @@ public class Argon2Kdf extends KdfEngine {
p.setUInt64(ParamIterations, keyRounds);
}
@Override
public long getDefaultKeyRounds() {
return DefaultIterations;
}
}
@@ -19,10 +19,12 @@
*/
package com.kunzisoft.keepass.crypto.keyDerivation;
import com.kunzisoft.keepass.database.ObjectNameResource;
import java.io.IOException;
import java.util.UUID;
public abstract class KdfEngine {
public abstract class KdfEngine implements ObjectNameResource{
public UUID uuid;
public KdfParameters getDefaultParameters() {
@@ -33,10 +35,10 @@ public abstract class KdfEngine {
public abstract void randomize(KdfParameters p);
public abstract String getName();
public abstract long getKeyRounds(KdfParameters p);
public abstract void setKeyRounds(KdfParameters p, long keyRounds);
public abstract long getDefaultKeyRounds();
}
@@ -27,6 +27,7 @@ import android.preference.PreferenceManager;
import android.util.Log;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.crypto.keyDerivation.KdfEngine;
import com.kunzisoft.keepass.database.exception.ContentFileNotFoundException;
import com.kunzisoft.keepass.database.exception.InvalidDBException;
import com.kunzisoft.keepass.database.exception.InvalidPasswordException;
@@ -354,7 +355,7 @@ public class Database {
return getPwDatabase().getEncryptionAlgorithm();
}
public List<PwEncryptionAlgorithm> getAvailableEncryptionAlgorithm() {
public List<PwEncryptionAlgorithm> getAvailableEncryptionAlgorithms() {
switch (getPwDatabase().getVersion()) {
case V4:
return ((PwDatabaseV4) getPwDatabase()).getAvailableEncryptionAlgorithms();
@@ -378,8 +379,31 @@ public class Database {
return getPwDatabase().getEncryptionAlgorithm().getName(resources);
}
public String getKeyDerivationName() {
return getPwDatabase().getKeyDerivationName();
public List<KdfEngine> getAvailableKdfEngines() {
switch (getPwDatabase().getVersion()) {
case V4:
return ((PwDatabaseV4) getPwDatabase()).getAvailableKdfEngines();
case V3:
return ((PwDatabaseV3) getPwDatabase()).getAvailableKdfEngines();
}
return new ArrayList<>();
}
public KdfEngine getKdfEngine() {
return getPwDatabase().getKdfEngine();
}
public void assignKdfEngine(KdfEngine kdfEngine) {
switch (getPwDatabase().getVersion()) {
case V4:
((PwDatabaseV4) getPwDatabase()).setKdfEngine(kdfEngine);
((PwDatabaseV4) getPwDatabase()).setKdfParameters(kdfEngine.getDefaultParameters());
((PwDatabaseV4) getPwDatabase()).setNumberKeyEncryptionRounds(kdfEngine.getDefaultKeyRounds());
}
}
public String getKeyDerivationName(Resources resources) {
return getPwDatabase().getKeyDerivationName(resources);
}
public String getNumberKeyEncryptionRoundsAsString() {
@@ -0,0 +1,29 @@
/*
* Copyright 2018 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePass DX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.database;
import android.content.res.Resources;
/**
* Interface to generify items with a name resource, that can be (for example) visible in a list
*/
public interface ObjectNameResource {
String getName(Resources resources);
}
@@ -19,8 +19,11 @@
*/
package com.kunzisoft.keepass.database;
import android.content.res.Resources;
import com.kunzisoft.keepass.crypto.finalkey.FinalKey;
import com.kunzisoft.keepass.crypto.finalkey.FinalKeyFactory;
import com.kunzisoft.keepass.crypto.keyDerivation.KdfEngine;
import com.kunzisoft.keepass.database.exception.InvalidKeyFileException;
import com.kunzisoft.keepass.database.exception.KeyFileEmptyException;
import com.kunzisoft.keepass.stream.NullOutputStream;
@@ -34,7 +37,6 @@ import java.io.UnsupportedEncodingException;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -283,7 +285,11 @@ public abstract class PwDatabase<PwGroupDB extends PwGroup<PwGroupDB, PwGroupDB,
public abstract List<PwEncryptionAlgorithm> getAvailableEncryptionAlgorithms();
public abstract String getKeyDerivationName();
public abstract List<KdfEngine> getAvailableKdfEngines();
public abstract KdfEngine getKdfEngine();
public abstract String getKeyDerivationName(Resources resources);
public abstract List<PwGroupDB> getGrpRoots();
@@ -45,7 +45,10 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
package com.kunzisoft.keepass.database;
import android.content.res.Resources;
import com.kunzisoft.keepass.crypto.keyDerivation.AesKdf;
import com.kunzisoft.keepass.crypto.keyDerivation.KdfEngine;
import com.kunzisoft.keepass.database.exception.InvalidKeyFileException;
import java.io.IOException;
@@ -62,6 +65,7 @@ import java.util.Random;
public class PwDatabaseV3 extends PwDatabase<PwGroupV3, PwEntryV3> {
private static final int DEFAULT_ENCRYPTION_ROUNDS = 300;
private KdfEngine kdfEngine = new AesKdf(); // Always the same
// all entries
private List<PwEntryV3> entries = new ArrayList<>();
@@ -96,8 +100,20 @@ public class PwDatabaseV3 extends PwDatabase<PwGroupV3, PwEntryV3> {
}
@Override
public String getKeyDerivationName() {
return AesKdf.DEFAULT_NAME;
public KdfEngine getKdfEngine() {
return kdfEngine;
}
@Override
public List<KdfEngine> getAvailableKdfEngines() {
List<KdfEngine> list = new ArrayList<>();
list.add(kdfEngine);
return list;
}
@Override
public String getKeyDerivationName(Resources resources) {
return kdfEngine.getName(resources);
}
@Override
@@ -19,6 +19,7 @@
*/
package com.kunzisoft.keepass.database;
import android.content.res.Resources;
import android.webkit.URLUtil;
import com.kunzisoft.keepass.collections.VariantDictionary;
@@ -150,6 +151,33 @@ public class PwDatabaseV4 extends PwDatabase<PwGroupV4, PwEntryV4> {
this.compressionAlgorithm = compressionAlgorithm;
}
@Override
public KdfEngine getKdfEngine() {
return kdfEngine;
}
public void setKdfEngine(KdfEngine kdfEngine) {
this.kdfEngine = kdfEngine;
}
@Override
public String getKeyDerivationName(Resources resources) {
return kdfEngine.getName(resources);
}
@Override
public List<KdfEngine> getAvailableKdfEngines() {
return KdfFactory.kdfList;
}
public KdfParameters getKdfParameters() {
return kdfParameters;
}
public void setKdfParameters(KdfParameters kdfParameters) {
this.kdfParameters = kdfParameters;
}
@Override
public long getNumberKeyEncryptionRounds() {
if (getKdfEngine() != null && getKdfParameters() != null)
@@ -334,15 +362,6 @@ public class PwDatabaseV4 extends PwDatabase<PwGroupV4, PwEntryV4> {
this.customData.put(label, value);
}
public KdfEngine getKdfEngine() {
return kdfEngine;
}
@Override
public String getKeyDerivationName() {
return kdfEngine.getName();
}
@Override
public byte[] getMasterKey(String key, InputStream keyInputStream)
throws InvalidKeyFileException, IOException {
@@ -703,14 +722,6 @@ public class PwDatabaseV4 extends PwDatabase<PwGroupV4, PwEntryV4> {
return groups.get(recycleId);
}
public KdfParameters getKdfParameters() {
return kdfParameters;
}
public void setKdfParameters(KdfParameters kdfParameters) {
this.kdfParameters = kdfParameters;
}
public VariantDictionary getPublicCustomData() {
return publicCustomData;
}
@@ -29,7 +29,7 @@ import com.kunzisoft.keepass.crypto.engine.TwofishEngine;
import java.util.UUID;
public enum PwEncryptionAlgorithm {
public enum PwEncryptionAlgorithm implements ObjectNameResource {
AES_Rijndael,
Twofish,
@@ -39,11 +39,11 @@ public enum PwEncryptionAlgorithm {
switch (this) {
default:
case AES_Rijndael:
return resources.getString(R.string.rijndael);
return resources.getString(R.string.encryption_rijndael);
case Twofish:
return resources.getString(R.string.twofish);
return resources.getString(R.string.encryption_twofish);
case ChaCha20:
return resources.getString(R.string.chacha20);
return resources.getString(R.string.encryption_chacha20);
}
}
@@ -51,8 +51,9 @@ import com.kunzisoft.keepass.dialogs.UnavailableFeatureDialogFragment;
import com.kunzisoft.keepass.dialogs.UnderDevelopmentFeatureDialogFragment;
import com.kunzisoft.keepass.fingerprint.FingerPrintHelper;
import com.kunzisoft.keepass.icons.IconPackChooser;
import com.kunzisoft.keepass.settings.preferenceDialogFragment.DatabaseAlgorithmPreferenceDialogFragmentCompat;
import com.kunzisoft.keepass.settings.preferenceDialogFragment.DatabaseEncryptionAlgorithmPreferenceDialogFragmentCompat;
import com.kunzisoft.keepass.settings.preferenceDialogFragment.DatabaseDescriptionPreferenceDialogFragmentCompat;
import com.kunzisoft.keepass.settings.preferenceDialogFragment.DatabaseKeyDerivationPreferenceDialogFragmentCompat;
import com.kunzisoft.keepass.settings.preferenceDialogFragment.DatabaseNamePreferenceDialogFragmentCompat;
import com.kunzisoft.keepass.settings.preferenceDialogFragment.RoundsPreferenceDialogFragmentCompat;
import com.kunzisoft.keepass.stylish.Stylish;
@@ -70,6 +71,8 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat
private int count = 0;
private Preference roundPref;
public static NestedSettingsFragment newInstance(Screen key) {
NestedSettingsFragment fragment = new NestedSettingsFragment();
// supply arguments to bundle.
@@ -321,11 +324,10 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat
// Key derivation function
Preference kdfPref = findPreference(getString(R.string.key_derivation_function_key));
kdfPref.setSummary(db.getKeyDerivationName());
preferenceInDevelopment(kdfPref);
kdfPref.setSummary(db.getKeyDerivationName(getResources()));
// Round encryption
Preference roundPref = findPreference(getString(R.string.transform_rounds_key));
roundPref = findPreference(getString(R.string.transform_rounds_key));
roundPref.setSummary(db.getNumberKeyEncryptionRoundsAsString());
} else {
@@ -432,7 +434,13 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat
dialogFragment = DatabaseDescriptionPreferenceDialogFragmentCompat.newInstance(preference.getKey());
}
else if (preference.getKey().equals(getString(R.string.encryption_algorithm_key))) {
dialogFragment = DatabaseAlgorithmPreferenceDialogFragmentCompat.newInstance(preference.getKey());
dialogFragment = DatabaseEncryptionAlgorithmPreferenceDialogFragmentCompat.newInstance(preference.getKey());
}
else if (preference.getKey().equals(getString(R.string.key_derivation_function_key))) {
DatabaseKeyDerivationPreferenceDialogFragmentCompat keyDerivationDialogFragment = DatabaseKeyDerivationPreferenceDialogFragmentCompat.newInstance(preference.getKey());
if (roundPref != null)
keyDerivationDialogFragment.setRoundPreference(roundPref);
dialogFragment = keyDerivationDialogFragment;
}
else if (preference.getKey().equals(getString(R.string.transform_rounds_key))) {
dialogFragment = RoundsPreferenceDialogFragmentCompat.newInstance(preference.getKey());
@@ -6,21 +6,21 @@ import android.util.AttributeSet;
import com.kunzisoft.keepass.R;
public class AlgorithmListPreference extends DialogPreference {
public class DialogListExplanationPreference extends DialogPreference {
public AlgorithmListPreference(Context context) {
public DialogListExplanationPreference(Context context) {
this(context, null);
}
public AlgorithmListPreference(Context context, AttributeSet attrs) {
public DialogListExplanationPreference(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.dialogPreferenceStyle);
}
public AlgorithmListPreference(Context context, AttributeSet attrs, int defStyleAttr) {
public DialogListExplanationPreference(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, defStyleAttr);
}
public AlgorithmListPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
public DialogListExplanationPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@@ -1,180 +0,0 @@
package com.kunzisoft.keepass.settings.preferenceDialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.database.PwEncryptionAlgorithm;
import com.kunzisoft.keepass.database.edit.OnFinish;
import java.util.ArrayList;
import java.util.List;
public class DatabaseAlgorithmPreferenceDialogFragmentCompat extends DatabaseSavePreferenceDialogFragmentCompat {
private AlgorithmAdapter algorithmAdapter;
private PwEncryptionAlgorithm algorithmSelected;
public static DatabaseAlgorithmPreferenceDialogFragmentCompat newInstance(
String key) {
final DatabaseAlgorithmPreferenceDialogFragmentCompat
fragment = new DatabaseAlgorithmPreferenceDialogFragmentCompat();
final Bundle b = new Bundle(1);
b.putString(ARG_KEY, key);
fragment.setArguments(b);
return fragment;
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
RecyclerView recyclerView = view.findViewById(R.id.pref_dialog_list);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
algorithmAdapter = new AlgorithmAdapter();
recyclerView.setAdapter(algorithmAdapter);
setAlgorithm(database.getAvailableEncryptionAlgorithm(), database.getEncryptionAlgorithm());
}
public void setAlgorithm(List<PwEncryptionAlgorithm> algorithmList, PwEncryptionAlgorithm algorithm) {
if (algorithmAdapter != null)
algorithmAdapter.setAlgorithms(algorithmList, algorithm);
algorithmSelected = algorithm;
}
private class AlgorithmAdapter extends RecyclerView.Adapter<AlgorithmViewHolder> {
private LayoutInflater inflater;
private List<PwEncryptionAlgorithm> algorithms;
private PwEncryptionAlgorithm algorithmUsed;
AlgorithmAdapter() {
this.inflater = LayoutInflater.from(getContext());
this.algorithms = new ArrayList<>();
this.algorithmUsed = null;
}
@NonNull
@Override
public AlgorithmViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.pref_dialog_list_radio_item, parent, false);
return new AlgorithmViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull AlgorithmViewHolder holder, int position) {
PwEncryptionAlgorithm algorithm = this.algorithms.get(position);
holder.radioButton.setText(algorithm.getName(getResources()));
if (algorithmUsed != null && algorithmUsed.equals(algorithm))
holder.radioButton.setChecked(true);
else
holder.radioButton.setChecked(false);
holder.radioButton.setOnClickListener(new OnAlgorithmClickListener(algorithm));
}
@Override
public int getItemCount() {
return algorithms.size();
}
public void setAlgorithms(List<PwEncryptionAlgorithm> algorithms, PwEncryptionAlgorithm algorithmUsed) {
this.algorithms.clear();
this.algorithms.addAll(algorithms);
this.algorithmUsed = algorithmUsed;
}
void setAlgorithmUsed(PwEncryptionAlgorithm algorithmUsed) {
this.algorithmUsed = algorithmUsed;
}
}
private class AlgorithmViewHolder extends RecyclerView.ViewHolder {
RadioButton radioButton;
public AlgorithmViewHolder(View itemView) {
super(itemView);
radioButton = itemView.findViewById(R.id.pref_dialog_list_radio);
}
}
private class OnAlgorithmClickListener implements View.OnClickListener {
private PwEncryptionAlgorithm algorithmClicked;
public OnAlgorithmClickListener(PwEncryptionAlgorithm algorithm) {
this.algorithmClicked = algorithm;
}
@Override
public void onClick(View view) {
algorithmSelected = algorithmClicked;
algorithmAdapter.setAlgorithmUsed(algorithmSelected);
algorithmAdapter.notifyDataSetChanged();
}
}
public PwEncryptionAlgorithm getAlgorithmSelected() {
return algorithmSelected;
}
@Override
public void onDialogClosed(boolean positiveResult) {
if ( positiveResult ) {
assert getContext() != null;
if (getAlgorithmSelected() != null) {
PwEncryptionAlgorithm newAlgorithm = getAlgorithmSelected();
PwEncryptionAlgorithm oldAlgorithm = database.getEncryptionAlgorithm();
database.assignEncryptionAlgorithm(newAlgorithm);
Handler handler = new Handler();
setAfterSaveDatabase(new AfterDescriptionSave(getContext(), handler, newAlgorithm, oldAlgorithm));
}
}
super.onDialogClosed(positiveResult);
}
private class AfterDescriptionSave extends OnFinish {
private PwEncryptionAlgorithm mNewAlgorithm;
private PwEncryptionAlgorithm mOldAlgorithm;
private Context mCtx;
AfterDescriptionSave(Context ctx, Handler handler, PwEncryptionAlgorithm newAlgorithm, PwEncryptionAlgorithm oldAlgorithm) {
super(handler);
mCtx = ctx;
mNewAlgorithm = newAlgorithm;
mOldAlgorithm = oldAlgorithm;
}
@Override
public void run() {
PwEncryptionAlgorithm algorithmToShow = mNewAlgorithm;
if (!mSuccess) {
displayMessage(mCtx);
database.assignEncryptionAlgorithm(mOldAlgorithm);
}
getPreference().setSummary(algorithmToShow.getName(mCtx.getResources()));
super.run();
}
}
}
@@ -1,3 +1,22 @@
/*
* Copyright 2018 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePass DX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.settings.preferenceDialogFragment;
import android.content.Context;
@@ -0,0 +1,115 @@
/*
* Copyright 2018 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePass DX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.settings.preferenceDialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.database.PwEncryptionAlgorithm;
import com.kunzisoft.keepass.database.edit.OnFinish;
import com.kunzisoft.keepass.settings.preferenceDialogFragment.adapter.ListRadioItemAdapter;
public class DatabaseEncryptionAlgorithmPreferenceDialogFragmentCompat extends DatabaseSavePreferenceDialogFragmentCompat
implements ListRadioItemAdapter.RadioItemSelectedCallback<PwEncryptionAlgorithm> {
private PwEncryptionAlgorithm algorithmSelected;
public static DatabaseEncryptionAlgorithmPreferenceDialogFragmentCompat newInstance(
String key) {
final DatabaseEncryptionAlgorithmPreferenceDialogFragmentCompat
fragment = new DatabaseEncryptionAlgorithmPreferenceDialogFragmentCompat();
final Bundle b = new Bundle(1);
b.putString(ARG_KEY, key);
fragment.setArguments(b);
return fragment;
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
RecyclerView recyclerView = view.findViewById(R.id.pref_dialog_list);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
ListRadioItemAdapter<PwEncryptionAlgorithm> encryptionAlgorithmAdapter = new ListRadioItemAdapter<>(getActivity());
encryptionAlgorithmAdapter.setRadioItemSelectedCallback(this);
recyclerView.setAdapter(encryptionAlgorithmAdapter);
algorithmSelected = database.getEncryptionAlgorithm();
encryptionAlgorithmAdapter.setItems(database.getAvailableEncryptionAlgorithms(), algorithmSelected);
}
@Override
public void onDialogClosed(boolean positiveResult) {
if ( positiveResult ) {
assert getContext() != null;
if (algorithmSelected != null) {
PwEncryptionAlgorithm newAlgorithm = algorithmSelected;
PwEncryptionAlgorithm oldAlgorithm = database.getEncryptionAlgorithm();
database.assignEncryptionAlgorithm(newAlgorithm);
Handler handler = new Handler();
setAfterSaveDatabase(new AfterDescriptionSave(getContext(), handler, newAlgorithm, oldAlgorithm));
}
}
super.onDialogClosed(positiveResult);
}
@Override
public void onItemSelected(PwEncryptionAlgorithm item) {
this.algorithmSelected = item;
}
private class AfterDescriptionSave extends OnFinish {
private PwEncryptionAlgorithm mNewAlgorithm;
private PwEncryptionAlgorithm mOldAlgorithm;
private Context mCtx;
AfterDescriptionSave(Context ctx, Handler handler, PwEncryptionAlgorithm newAlgorithm, PwEncryptionAlgorithm oldAlgorithm) {
super(handler);
mCtx = ctx;
mNewAlgorithm = newAlgorithm;
mOldAlgorithm = oldAlgorithm;
}
@Override
public void run() {
PwEncryptionAlgorithm algorithmToShow = mNewAlgorithm;
if (!mSuccess) {
displayMessage(mCtx);
database.assignEncryptionAlgorithm(mOldAlgorithm);
}
getPreference().setSummary(algorithmToShow.getName(mCtx.getResources()));
super.run();
}
}
}
@@ -0,0 +1,122 @@
/*
* Copyright 2018 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePass DX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.settings.preferenceDialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.preference.Preference;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.crypto.keyDerivation.KdfEngine;
import com.kunzisoft.keepass.database.edit.OnFinish;
import com.kunzisoft.keepass.settings.preferenceDialogFragment.adapter.ListRadioItemAdapter;
public class DatabaseKeyDerivationPreferenceDialogFragmentCompat extends DatabaseSavePreferenceDialogFragmentCompat
implements ListRadioItemAdapter.RadioItemSelectedCallback<KdfEngine> {
private KdfEngine kdfEngineSelected;
private Preference roundPreference;
public static DatabaseKeyDerivationPreferenceDialogFragmentCompat newInstance(
String key) {
final DatabaseKeyDerivationPreferenceDialogFragmentCompat
fragment = new DatabaseKeyDerivationPreferenceDialogFragmentCompat();
final Bundle b = new Bundle(1);
b.putString(ARG_KEY, key);
fragment.setArguments(b);
return fragment;
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
RecyclerView recyclerView = view.findViewById(R.id.pref_dialog_list);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
ListRadioItemAdapter<KdfEngine> kdfAdapter = new ListRadioItemAdapter<>(getActivity());
kdfAdapter.setRadioItemSelectedCallback(this);
recyclerView.setAdapter(kdfAdapter);
kdfEngineSelected = database.getKdfEngine();
kdfAdapter.setItems(database.getAvailableKdfEngines(), kdfEngineSelected);
}
@Override
public void onDialogClosed(boolean positiveResult) {
if ( positiveResult ) {
assert getContext() != null;
if (kdfEngineSelected != null) {
KdfEngine newKdfEngine = kdfEngineSelected;
KdfEngine oldKdfEngine = database.getKdfEngine();
database.assignKdfEngine(newKdfEngine);
Handler handler = new Handler();
setAfterSaveDatabase(new AfterDescriptionSave(getContext(), handler, newKdfEngine, oldKdfEngine));
}
}
super.onDialogClosed(positiveResult);
}
public void setRoundPreference(Preference preference) {
this.roundPreference = preference;
}
@Override
public void onItemSelected(KdfEngine item) {
kdfEngineSelected = item;
}
private class AfterDescriptionSave extends OnFinish {
private KdfEngine mNewKdfEngine;
private KdfEngine mOldKdfEngine;
private Context mCtx;
AfterDescriptionSave(Context ctx, Handler handler, KdfEngine newKdfEngine, KdfEngine oldKdfEngine) {
super(handler);
this.mCtx = ctx;
this.mNewKdfEngine = newKdfEngine;
this.mOldKdfEngine = oldKdfEngine;
}
@Override
public void run() {
KdfEngine kdfEngineToShow = mNewKdfEngine;
if (!mSuccess) {
displayMessage(mCtx);
database.assignKdfEngine(mOldKdfEngine);
}
getPreference().setSummary(kdfEngineToShow.getName(mCtx.getResources()));
roundPreference.setSummary(String.valueOf(kdfEngineToShow.getDefaultKeyRounds()));
super.run();
}
}
}
@@ -1,3 +1,22 @@
/*
* Copyright 2018 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePass DX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.settings.preferenceDialogFragment;
import android.content.Context;
@@ -1,3 +1,22 @@
/*
* Copyright 2018 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePass DX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.settings.preferenceDialogFragment;
import android.view.View;
@@ -1,3 +1,22 @@
/*
* Copyright 2018 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePass DX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.settings.preferenceDialogFragment;
import android.view.View;
@@ -1,3 +1,22 @@
/*
* Copyright 2018 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePass DX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.settings.preferenceDialogFragment;
import android.support.v7.preference.PreferenceDialogFragmentCompat;
@@ -0,0 +1,109 @@
/*
* Copyright 2018 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePass DX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.settings.preferenceDialogFragment.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.database.ObjectNameResource;
import java.util.ArrayList;
import java.util.List;
public class ListRadioItemAdapter<T extends ObjectNameResource> extends RecyclerView.Adapter<ListRadioViewHolder> {
private Context context;
private LayoutInflater inflater;
private List<T> radioItemList;
private T radioItemUsed;
private RadioItemSelectedCallback<T> radioItemSelectedCallback;
public ListRadioItemAdapter(Context context) {
this.context = context;
this.inflater = LayoutInflater.from(context);
this.radioItemList = new ArrayList<>();
this.radioItemUsed = null;
}
@NonNull
@Override
public ListRadioViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.pref_dialog_list_radio_item, parent, false);
return new ListRadioViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ListRadioViewHolder holder, int position) {
T item = this.radioItemList.get(position);
holder.radioButton.setText(item.getName(context.getResources()));
if (radioItemUsed != null && radioItemUsed.equals(item))
holder.radioButton.setChecked(true);
else
holder.radioButton.setChecked(false);
holder.radioButton.setOnClickListener(new OnItemClickListener(item));
}
@Override
public int getItemCount() {
return radioItemList.size();
}
public void setItems(List<T> algorithms, T algorithmUsed) {
this.radioItemList.clear();
this.radioItemList.addAll(algorithms);
this.radioItemUsed = algorithmUsed;
}
private void setRadioItemUsed(T radioItemUsed) {
this.radioItemUsed = radioItemUsed;
}
private class OnItemClickListener implements View.OnClickListener {
private T itemClicked;
OnItemClickListener(T item) {
this.itemClicked = item;
}
@Override
public void onClick(View view) {
if (radioItemSelectedCallback != null)
radioItemSelectedCallback.onItemSelected(itemClicked);
setRadioItemUsed(itemClicked);
notifyDataSetChanged();
}
}
public void setRadioItemSelectedCallback(RadioItemSelectedCallback<T> radioItemSelectedCallback) {
this.radioItemSelectedCallback = radioItemSelectedCallback;
}
public interface RadioItemSelectedCallback<T> {
void onItemSelected(T item);
}
}
@@ -0,0 +1,37 @@
/*
* Copyright 2018 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePass DX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.settings.preferenceDialogFragment.adapter;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RadioButton;
import com.kunzisoft.keepass.R;
public class ListRadioViewHolder extends RecyclerView.ViewHolder {
public RadioButton radioButton;
public ListRadioViewHolder(View itemView) {
super(itemView);
radioButton = itemView.findViewById(R.id.pref_dialog_list_radio);
}
}