remove if save fails (#281)

This commit is contained in:
m2049r 2018-05-25 18:20:27 +02:00 committed by GitHub
parent 5a7aa6cc77
commit e109df34f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 6 deletions

View File

@ -19,6 +19,7 @@ package com.m2049r.xmrwallet.util;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build; import android.os.Build;
import android.security.KeyPairGeneratorSpec; import android.security.KeyPairGeneratorSpec;
import android.security.keystore.KeyGenParameterSpec; import android.security.keystore.KeyGenParameterSpec;
@ -84,10 +85,12 @@ public class KeyStoreHelper {
try { try {
KeyStoreHelper.createKeys(context, walletKeyAlias); KeyStoreHelper.createKeys(context, walletKeyAlias);
byte[] encrypted = KeyStoreHelper.encrypt(walletKeyAlias, data); byte[] encrypted = KeyStoreHelper.encrypt(walletKeyAlias, data);
if (encrypted == null) return false; SharedPreferences.Editor e = context.getSharedPreferences(SecurityConstants.WALLET_PASS_PREFS_NAME, Context.MODE_PRIVATE).edit();
context.getSharedPreferences(SecurityConstants.WALLET_PASS_PREFS_NAME, Context.MODE_PRIVATE).edit() if (encrypted == null) {
.putString(wallet, Base64.encodeToString(encrypted, Base64.DEFAULT)) e.remove(wallet).apply();
.apply(); return false;
}
e.putString(wallet, Base64.encodeToString(encrypted, Base64.DEFAULT)).apply();
return true; return true;
} catch (NoSuchProviderException | NoSuchAlgorithmException | } catch (NoSuchProviderException | NoSuchAlgorithmException |
InvalidAlgorithmParameterException | KeyStoreException ex) { InvalidAlgorithmParameterException | KeyStoreException ex) {
@ -229,8 +232,7 @@ public class KeyStoreHelper {
return (KeyStore.PrivateKeyEntry) entry; return (KeyStore.PrivateKeyEntry) entry;
} catch (IOException | NoSuchAlgorithmException | CertificateException } catch (IOException | NoSuchAlgorithmException | CertificateException
| UnrecoverableEntryException | KeyStoreException ex) { | UnrecoverableEntryException | KeyStoreException ex) {
Timber.e(ex); throw new IllegalStateException(ex);
return null;
} }
} }