Skip to content

Commit c1c8813

Browse files
committed
more improvements for #24 and #40 - generate and store new random passphrase for key
- this happens on first time creation of the private keyring
1 parent 0901a32 commit c1c8813

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

android-libproofmode/src/main/java/org/witness/proofmode/crypto/pgp/PgpUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,14 @@ public boolean verifyDetachedSignature (InputStream fileStream, InputStream sigS
297297
return DetachedSignatureProcessor.verifySignature(fileStream, sigStream, pubKey);
298298
}
299299

300+
public static boolean keyRingExists (Context context)
301+
{
302+
File fileSecKeyRing = new File(context.getFilesDir(),FILE_SECRET_KEY_RING);
303+
File filePubKeyRing = new File(context.getFilesDir(),FILE_PUBLIC_KEY_RING);
304+
305+
return fileSecKeyRing.exists() && filePubKeyRing.exists();
306+
}
307+
300308
public synchronized void initCrypto (Context context, String password) throws IOException, PGPException {
301309
if (pgpSec == null) {
302310

app/src/main/java/org/witness/proofmode/ProofModeApp.kt

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.witness.proofmode.notaries.OpenTimestampsNotarizationProvider
2424
import timber.log.Timber
2525
import java.io.IOException
2626
import java.util.concurrent.Executors
27+
import kotlin.random.Random
2728

2829
/**
2930
* Created by n8fr8 on 10/10/16.
@@ -41,8 +42,24 @@ class ProofModeApp : MultiDexApplication() {
4142
var pubKey: String? = null
4243
try {
4344
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
44-
pubKey = PgpUtils.getInstance(applicationContext,prefs.getString(PREFS_KEY_PASSPHRASE,
45-
PREFS_KEY_PASSPHRASE_DEFAULT)).publicKeyFingerprint
45+
46+
if (PgpUtils.keyRingExists(this)) {
47+
pubKey = PgpUtils.getInstance(
48+
applicationContext, prefs.getString(
49+
PREFS_KEY_PASSPHRASE,
50+
PREFS_KEY_PASSPHRASE_DEFAULT
51+
)
52+
).publicKeyFingerprint
53+
}
54+
else
55+
{
56+
var newPassPhrase = getRandPassword(12)
57+
prefs.edit().putString(PREFS_KEY_PASSPHRASE,newPassPhrase).commit()
58+
pubKey = PgpUtils.getInstance(
59+
applicationContext, newPassPhrase
60+
).publicKeyFingerprint
61+
62+
}
4663

4764
} catch (e: PGPException) {
4865
Timber.e(e, "error getting public key")
@@ -54,6 +71,22 @@ class ProofModeApp : MultiDexApplication() {
5471
}
5572
}
5673

74+
fun getRandPassword(n: Int): String
75+
{
76+
val characterSet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
77+
78+
val random = Random(System.nanoTime())
79+
val password = StringBuilder()
80+
81+
for (i in 0 until n)
82+
{
83+
val rIndex = random.nextInt(characterSet.length)
84+
password.append(characterSet[rIndex])
85+
}
86+
87+
return password.toString()
88+
}
89+
5790
private fun showToastMessage(message: String) {
5891
val handler = Handler(Looper.getMainLooper())
5992
handler.post {

0 commit comments

Comments
 (0)