From c1c8813d66fa8931a6ec956fe29d1912b55a61a2 Mon Sep 17 00:00:00 2001 From: n8fr8 Date: Mon, 8 May 2023 15:51:28 -0400 Subject: [PATCH] more improvements for #24 and #40 - generate and store new random passphrase for key - this happens on first time creation of the private keyring --- .../proofmode/crypto/pgp/PgpUtils.java | 8 ++++ .../org/witness/proofmode/ProofModeApp.kt | 37 ++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/android-libproofmode/src/main/java/org/witness/proofmode/crypto/pgp/PgpUtils.java b/android-libproofmode/src/main/java/org/witness/proofmode/crypto/pgp/PgpUtils.java index 43af439f..66f1f208 100644 --- a/android-libproofmode/src/main/java/org/witness/proofmode/crypto/pgp/PgpUtils.java +++ b/android-libproofmode/src/main/java/org/witness/proofmode/crypto/pgp/PgpUtils.java @@ -297,6 +297,14 @@ public boolean verifyDetachedSignature (InputStream fileStream, InputStream sigS return DetachedSignatureProcessor.verifySignature(fileStream, sigStream, pubKey); } + public static boolean keyRingExists (Context context) + { + File fileSecKeyRing = new File(context.getFilesDir(),FILE_SECRET_KEY_RING); + File filePubKeyRing = new File(context.getFilesDir(),FILE_PUBLIC_KEY_RING); + + return fileSecKeyRing.exists() && filePubKeyRing.exists(); + } + public synchronized void initCrypto (Context context, String password) throws IOException, PGPException { if (pgpSec == null) { diff --git a/app/src/main/java/org/witness/proofmode/ProofModeApp.kt b/app/src/main/java/org/witness/proofmode/ProofModeApp.kt index 6f45ee93..cfe1e7a3 100644 --- a/app/src/main/java/org/witness/proofmode/ProofModeApp.kt +++ b/app/src/main/java/org/witness/proofmode/ProofModeApp.kt @@ -24,6 +24,7 @@ import org.witness.proofmode.notaries.OpenTimestampsNotarizationProvider import timber.log.Timber import java.io.IOException import java.util.concurrent.Executors +import kotlin.random.Random /** * Created by n8fr8 on 10/10/16. @@ -41,8 +42,24 @@ class ProofModeApp : MultiDexApplication() { var pubKey: String? = null try { val prefs = PreferenceManager.getDefaultSharedPreferences(this) - pubKey = PgpUtils.getInstance(applicationContext,prefs.getString(PREFS_KEY_PASSPHRASE, - PREFS_KEY_PASSPHRASE_DEFAULT)).publicKeyFingerprint + + if (PgpUtils.keyRingExists(this)) { + pubKey = PgpUtils.getInstance( + applicationContext, prefs.getString( + PREFS_KEY_PASSPHRASE, + PREFS_KEY_PASSPHRASE_DEFAULT + ) + ).publicKeyFingerprint + } + else + { + var newPassPhrase = getRandPassword(12) + prefs.edit().putString(PREFS_KEY_PASSPHRASE,newPassPhrase).commit() + pubKey = PgpUtils.getInstance( + applicationContext, newPassPhrase + ).publicKeyFingerprint + + } } catch (e: PGPException) { Timber.e(e, "error getting public key") @@ -54,6 +71,22 @@ class ProofModeApp : MultiDexApplication() { } } + fun getRandPassword(n: Int): String + { + val characterSet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + + val random = Random(System.nanoTime()) + val password = StringBuilder() + + for (i in 0 until n) + { + val rIndex = random.nextInt(characterSet.length) + password.append(characterSet[rIndex]) + } + + return password.toString() + } + private fun showToastMessage(message: String) { val handler = Handler(Looper.getMainLooper()) handler.post {