Skip to content

Commit

Permalink
Move to framework Base64 class
Browse files Browse the repository at this point in the history
  • Loading branch information
bpellin committed Feb 14, 2020
1 parent 8d94476 commit 601976b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 263 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@
*/
package com.keepassdroid.tests.database;

import java.io.InputStream;
import java.util.UUID;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Base64;

import androidx.test.platform.app.InstrumentationRegistry;

import biz.source_code.base64Coder.Base64Coder;

import com.keepassdroid.database.PwDatabaseV4;
import com.keepassdroid.database.PwEntryV4;
import com.keepassdroid.database.load.ImporterV4;
Expand All @@ -38,6 +34,9 @@
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.UUID;

import static org.junit.Assert.assertEquals;

public class SprEngineTest {
Expand Down Expand Up @@ -78,7 +77,7 @@ private UUID decodeUUID(String encoded) {
return PwDatabaseV4.UUID_ZERO;
}

byte[] buf = Base64Coder.decode(encoded);
byte[] buf = Base64.decode(encoded, Base64.DEFAULT);
return Types.bytestoUUID(buf);
}

Expand Down
225 changes: 0 additions & 225 deletions app/src/main/java/biz/source_code/base64Coder/Base64Coder.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@

import android.app.KeyguardManager;
import android.content.Context;
import android.security.keystore.KeyProperties;
import android.util.Base64;

import androidx.biometric.BiometricManager;
import androidx.biometric.BiometricPrompt;
import androidx.core.os.CancellationSignal;
import android.security.keystore.KeyProperties;

import com.keepassdroid.compat.KeyGenParameterSpecCompat;
import com.keepassdroid.compat.KeyguardManagerCompat;
Expand All @@ -41,8 +42,6 @@
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

import biz.source_code.base64Coder.Base64Coder;

public class BiometricHelper {

private static final String ALIAS_KEY = "example-key";
Expand Down Expand Up @@ -167,11 +166,11 @@ public void encryptData(final String value) {
try {
// actual do encryption here
byte[] encrypted = cipher.doFinal(value.getBytes());
final String encryptedValue = new String(Base64Coder.encode(encrypted));
final String encryptedValue = new String(Base64.encodeToString(encrypted, Base64.DEFAULT));

// passes updated iv spec on to callback so this can be stored for decryption
final IvParameterSpec spec = cipher.getParameters().getParameterSpec(IvParameterSpec.class);
final String ivSpecValue = new String(Base64Coder.encode(spec.getIV()));
final String ivSpecValue = new String(Base64.encode(spec.getIV(), Base64.DEFAULT));
biometricCallback.handleEncryptedResult(encryptedValue, ivSpecValue);

} catch (final Exception e) {
Expand Down Expand Up @@ -215,7 +214,7 @@ private void initDecryptKey(
final SecretKey key = (SecretKey) keyStore.getKey(ALIAS_KEY, null);

// important to restore spec here that was used for decryption
final byte[] iv = Base64Coder.decode(ivSpecValue);
final byte[] iv = Base64.decode(ivSpecValue, Base64.DEFAULT);
final IvParameterSpec spec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, key, spec);
cryptoInitOk = true;
Expand All @@ -232,7 +231,7 @@ public void decryptData(final String encryptedValue) {
}
try {
// actual decryption here
final byte[] encrypted = Base64Coder.decode(encryptedValue);
final byte[] encrypted = Base64.decode(encryptedValue, Base64.DEFAULT);
byte[] decrypted = cipher.doFinal(encrypted);
final String decryptedString = new String(decrypted);

Expand Down
7 changes: 3 additions & 4 deletions app/src/main/java/com/keepassdroid/database/PwDatabaseV4.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2017 Brian Pellin.
* Copyright 2010-2020 Brian Pellin.
*
* This file is part of KeePassDroid.
*
Expand All @@ -19,6 +19,7 @@
*/
package com.keepassdroid.database;

import android.util.Base64;
import android.webkit.URLUtil;

import com.keepassdroid.collections.VariantDictionary;
Expand Down Expand Up @@ -53,8 +54,6 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import biz.source_code.base64Coder.Base64Coder;


public class PwDatabaseV4 extends PwDatabase {

Expand Down Expand Up @@ -254,7 +253,7 @@ protected byte[] loadXmlKeyFile(InputStream keyInputStream) {
Node text = children2.item(k);
if (text.getNodeType() == Node.TEXT_NODE) {
Text txt = (Text) text;
return Base64Coder.decode(txt.getNodeValue());
return Base64.decode(txt.getNodeValue(), Base64.DEFAULT);
}
}
}
Expand Down
Loading

0 comments on commit 601976b

Please sign in to comment.