Check existence of PGP keys before encrypt/decrypt

This commit is contained in:
Mingshen Sun 2020-04-14 20:20:16 -07:00
parent 50dec23b02
commit 0cae6af60d
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
6 changed files with 41 additions and 4 deletions

View file

@ -30,7 +30,7 @@ struct GopenPgp: PgpInterface {
}
throw AppError.KeyImport
}
publicKeys[k.getFingerprint()] = k
publicKeys[k.getFingerprint().lowercased()] = k
}
for key in prvKeys {
@ -41,7 +41,7 @@ struct GopenPgp: PgpInterface {
}
throw AppError.KeyImport
}
privateKeys[k.getFingerprint()] = k
privateKeys[k.getFingerprint().lowercased()] = k
}
}
@ -64,6 +64,14 @@ struct GopenPgp: PgpInterface {
return keys
}
func containsPublicKey(with keyID: String) -> Bool {
publicKeys.keys.contains(where: { key in key.hasSuffix(keyID.lowercased()) })
}
func containsPrivateKey(with keyID: String) -> Bool {
privateKeys.keys.contains(where: { key in key.hasSuffix(keyID.lowercased()) })
}
func decrypt(encryptedData: Data, keyID: String, passphrase: String) throws -> Data? {
guard let e = privateKeys.first(where: { (key, _) in key.hasSuffix(keyID.lowercased()) }),
let privateKey = privateKeys[e.key] else {