Skip to content

Commit

Permalink
Added some logic to KeysStorageImpl.| #372
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed May 25, 2021
1 parent 804ab80 commit 4dedb27
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface KeysStorage {

fun getPGPSecretKeyRingByFingerprint(fingerprint: String): PGPSecretKeyRing?

fun getPGPSecretKeyRingsByFingerprints(fingerprints: Iterable<String>): List<PGPSecretKeyRing>
fun getPGPSecretKeyRingsByFingerprints(fingerprints: Collection<String>): List<PGPSecretKeyRing>

fun getPGPSecretKeyRingsByUserId(user: String): List<PGPSecretKeyRing>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.pgpainless.util.Passphrase
import java.time.Instant
import java.util.*
import java.util.concurrent.TimeUnit
import javax.mail.internet.InternetAddress

/**
* This class implements [KeysStorage]. Here we collect information about imported private keys
Expand Down Expand Up @@ -89,7 +90,7 @@ class KeysStorageImpl private constructor(context: Context) : KeysStorage {

override fun getPgpKeyDetailsList(): List<PgpKeyDetails> {
val list = mutableListOf<PgpKeyDetails>()
for (secretKey in secretKeyRingsLiveData.value ?: emptyList()) {
for (secretKey in getPGPSecretKeyRings()) {
val pgpKeyDetails = secretKey.toPgpKeyDetails()
val passphrase = getPassphraseByFingerprint(pgpKeyDetails.fingerprint)
list.add(pgpKeyDetails.copy(tempPassphrase = passphrase?.chars))
Expand All @@ -104,12 +105,37 @@ class KeysStorageImpl private constructor(context: Context) : KeysStorage {
}
}

override fun getPGPSecretKeyRingsByFingerprints(fingerprints: Iterable<String>): List<PGPSecretKeyRing> {
TODO("Not yet implemented")
override fun getPGPSecretKeyRingsByFingerprints(fingerprints: Collection<String>):
List<PGPSecretKeyRing> {
val list = mutableListOf<PGPSecretKeyRing>()
val set = fingerprints.map { it.toUpperCase(Locale.US) }.toSet()
for (secretKey in getPGPSecretKeyRings()) {
val openPgpV4Fingerprint = OpenPgpV4Fingerprint(secretKey)
if (openPgpV4Fingerprint.toString() in set) {
list.add(secretKey)
}
}
return list
}

override fun getPGPSecretKeyRingsByUserId(user: String): List<PGPSecretKeyRing> {
TODO("Not yet implemented")
val list = mutableListOf<PGPSecretKeyRing>()
for (secretKey in getPGPSecretKeyRings()) {
for (userId in secretKey.publicKey.userIDs) {
try {
val internetAddresses = InternetAddress.parse(userId)
for (internetAddress in internetAddresses) {
if (user.equals(internetAddress.address, true)) {
list.add(secretKey)
continue
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
return list
}

override fun getPassphraseByFingerprint(fingerprint: String): Passphrase? {
Expand Down

0 comments on commit 4dedb27

Please sign in to comment.