Set Key Rounds for database V4

This commit is contained in:
J-Jamet committed 2018-04-04 20:37:23 +02:00
1 parent b3ff38fb35
commit 825d88014d
12 files changed
+64 -56

No files matched your search

@@ -88,4 +88,9 @@ public class AesKdf extends KdfEngine {
public long getKeyRounds(KdfParameters p) {
return p.getUInt64(ParamRounds);
}
@Override
public void setKeyRounds(KdfParameters p, long keyRounds) {
p.setUInt64(ParamRounds, keyRounds);
}
}
@@ -107,4 +107,10 @@ public class Argon2Kdf extends KdfEngine {
public long getKeyRounds(KdfParameters p) {
return p.getUInt64(ParamIterations);
}
@Override
public void setKeyRounds(KdfParameters p, long keyRounds) {
p.setUInt64(ParamIterations, keyRounds);
}
}
@@ -37,4 +37,6 @@ public abstract class KdfEngine {
public abstract long getKeyRounds(KdfParameters p);
public abstract void setKeyRounds(KdfParameters p, long keyRounds);
}
@@ -68,7 +68,6 @@ public class PwDatabaseV4 extends PwDatabase<PwGroupV4, PwEntryV4> {
private PwCompressionAlgorithm compressionAlgorithm = PwCompressionAlgorithm.Gzip;
private KdfEngine kdfEngine;
// TODO: Refactor me away to get directly from kdfParameters
private long numKeyEncRounds = AesKdf.DEFAULT_ROUNDS; // By default take the AES rounds
private PwDate nameChanged = new PwDate();
private PwDate settingsChanged = new PwDate();
@@ -143,7 +142,8 @@ public class PwDatabaseV4 extends PwDatabase<PwGroupV4, PwEntryV4> {
@Override
public void setNumberKeyEncryptionRounds(long rounds) throws NumberFormatException {
// TODO set kdfParameter
if (getKdfEngine() != null && getKdfParameters() != null)
getKdfEngine().setKeyRounds(getKdfParameters(), rounds);
numKeyEncRounds = rounds;
}
@@ -48,6 +48,7 @@ public class MainPreferenceFragment extends PreferenceFragmentCompat implements
public void onDisplayPreferenceDialog(Preference preference) {
// Try if the preference is one of our custom Preferences
if (preference instanceof RoundsPreference) {
assert getFragmentManager() != null;
DialogFragment dialogFragment = RoundsFixPreferenceDialogFragmentCompat.newInstance(preference.getKey());
dialogFragment.setTargetFragment(this, 0);
dialogFragment.show(getFragmentManager(), null);
@@ -274,22 +274,15 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat
// Encryption Algorithme
Preference algorithmPref = findPreference(getString(R.string.encryption_algorithm_key));
algorithmPref.setSummary(pwDatabase
.getEncryptionAlgorithm().getName(getResources()));
algorithmPref.setSummary(pwDatabase.getEncryptionAlgorithm().getName(getResources()));
// Key derivation function
Preference kdfPref = findPreference(getString(R.string.key_derivation_function_key));
kdfPref.setSummary(pwDatabase
.getKeyDerivationName());
kdfPref.setSummary(pwDatabase.getKeyDerivationName());
// Round encryption
Preference roundPref = findPreference(getString(R.string.transform_rounds_key));
roundPref.setSummary(Long.toString(pwDatabase.getNumberKeyEncryptionRounds()));
roundPref.setEnabled(false); //TODO refactor round pref
roundPref.setOnPreferenceChangeListener((preference, newValue) -> {
preference.setSummary(Long.toString(pwDatabase.getNumberKeyEncryptionRounds()));
return true;
});
if (db.isRecycleBinAvailabledAndEnabled()) {
SwitchPreference recycleBinPref = (SwitchPreference) findPreference(getString(R.string.recycle_bin_key));
@@ -24,6 +24,21 @@ public class RoundsFixPreferenceDialogFragmentCompat extends PreferenceDialogFra
return fragment;
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
TextView textDescriptionView = view.findViewById(R.id.rounds_explanation);
mRoundsView = view.findViewById(R.id.rounds);
DialogPreference preference = getPreference();
if (preference instanceof RoundsPreference) {
textDescriptionView.setText(((RoundsPreference) preference).getExplanations());
long numRounds = ((RoundsPreference) preference).getRounds();
mRoundsView.setText(String.valueOf(numRounds));
}
}
@Override
public void onDialogClosed(boolean positiveResult) {
if ( positiveResult ) {
@@ -47,19 +62,4 @@ public class RoundsFixPreferenceDialogFragmentCompat extends PreferenceDialogFra
}
}
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
TextView textDescriptionView = (TextView) view.findViewById(R.id.rounds_explanation);
mRoundsView = (TextView) view.findViewById(R.id.rounds);
DialogPreference preference = getPreference();
if (preference instanceof RoundsPreference) {
textDescriptionView.setText(((RoundsPreference) preference).getExplanations());
long numRounds = ((RoundsPreference) preference).getRounds();
mRoundsView.setText(String.valueOf(numRounds));
}
}
}
@@ -34,12 +34,15 @@ public class RoundsPreference extends DialogPreference {
public RoundsPreference(Context context) {
this(context, null);
}
public RoundsPreference(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.dialogPreferenceStyle);
}
public RoundsPreference(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, defStyleAttr);
}
public RoundsPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
TypedArray a = context.getTheme().obtainStyledAttributes(
@@ -47,7 +50,7 @@ public class RoundsPreference extends DialogPreference {
R.styleable.RoundsDialog,
0, 0);
try {
explanations = a.getString(R.styleable.RoundsDialog_description);
setExplanations(a.getString(R.styleable.RoundsDialog_explanations));
} finally {
a.recycle();
}
@@ -3,18 +3,16 @@ package com.keepassdroid.settings;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.preference.DialogPreference;
import android.support.v7.preference.PreferenceDialogFragmentCompat;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.keepassdroid.database.Database;
import com.keepassdroid.tasks.ProgressTask;
import com.keepassdroid.app.App;
import com.keepassdroid.database.PwDatabase;
import com.keepassdroid.database.edit.OnFinish;
import com.keepassdroid.database.edit.SaveDB;
import com.keepassdroid.tasks.ProgressTask;
import com.kunzisoft.keepass.R;
public class RoundsPreferenceDialogFragmentCompat extends PreferenceDialogFragmentCompat {
@@ -33,9 +31,23 @@ public class RoundsPreferenceDialogFragmentCompat extends PreferenceDialogFragme
return fragment;
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
mRoundsView = view.findViewById(R.id.rounds);
// Get the number or rounds from the related Preference
mPM = App.getDB().getPwDatabase();
long numRounds = mPM.getNumberKeyEncryptionRounds();
mRoundsView.setText(String.valueOf(numRounds));
}
@Override
public void onDialogClosed(boolean positiveResult) {
if ( positiveResult ) {
assert getContext() != null;
long rounds;
try {
@@ -59,50 +71,37 @@ public class RoundsPreferenceDialogFragmentCompat extends PreferenceDialogFragme
}
Handler handler = new Handler();
SaveDB save = new SaveDB(getContext(), App.getDB(), new AfterSave(getContext(), handler, oldRounds));
SaveDB save = new SaveDB(getContext(), App.getDB(), new AfterSave(getContext(), handler, rounds, oldRounds));
ProgressTask pt = new ProgressTask(getContext(), save, R.string.saving_database);
pt.run();
}
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
mRoundsView = view.findViewById(R.id.rounds);
// Get the time from the related Preference
Database db = App.getDB();
mPM = db.getPwDatabase();
long numRounds = mPM.getNumberKeyEncryptionRounds();
DialogPreference preference = getPreference();
if (preference instanceof RoundsPreference) {
numRounds = ((RoundsPreference) preference).getRounds();
}
mRoundsView.setText(String.valueOf(numRounds));
}
private class AfterSave extends OnFinish {
private long mNewRounds;
private long mOldRounds;
private Context mCtx;
public AfterSave(Context ctx, Handler handler, long oldRounds) {
public AfterSave(Context ctx, Handler handler, long newRounds, long oldRounds) {
super(handler);
mCtx = ctx;
mNewRounds = newRounds;
mOldRounds = oldRounds;
}
@Override
public void run() {
long roundsToShow = mNewRounds;
if (!mSuccess) {
displayMessage(mCtx);
mPM.setNumberKeyEncryptionRounds(mOldRounds);
}
getPreference().setSummary(String.valueOf(roundsToShow));
super.run();
}
}