Skip to content

Commit

Permalink
more improvements for #24 and #40 - generate and store new random pas…
Browse files Browse the repository at this point in the history
…sphrase for key

- this happens on first time creation of the private keyring
  • Loading branch information
n8fr8 committed May 8, 2023
1 parent 0901a32 commit c1c8813
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
37 changes: 35 additions & 2 deletions app/src/main/java/org/witness/proofmode/ProofModeApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
Expand All @@ -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 {
Expand Down

0 comments on commit c1c8813

Please sign in to comment.