clarify public vs private keys + make prvate key IDs available

This commit is contained in:
Lysann Tranvouez 2026-03-11 16:16:50 +01:00
parent 4e19d9e714
commit 5a92b6fda7
11 changed files with 56 additions and 30 deletions

View file

@ -167,12 +167,17 @@ struct GopenPGPInterface: PGPInterface {
return encryptedData.getBinary()!
}
var keyID: [String] {
publicKeys.keys.map { $0.uppercased() }
func getKeyIDs(type: PGPKey) -> [String] {
switch type {
case .PUBLIC:
return publicKeys.keys.map { $0.uppercased() }
case .PRIVATE:
return privateKeys.keys.map { $0.uppercased() }
}
}
var shortKeyID: [String] {
publicKeys.keys.map { $0.suffix(8).uppercased() }
func getShortKeyIDs(type: PGPKey) -> [String] {
getKeyIDs(type: type).map { $0.suffix(8).uppercased() }
}
private func findDecryptionKey(message: CryptoPGPMessage, keyIDHint: String?) throws -> CryptoKey? {

View file

@ -34,7 +34,8 @@ struct ObjectivePGPInterface: PGPInterface {
}
func encryptWithAllKeys(plainData: Data) throws -> Data {
try encrypt(plainData: plainData, keyIDs: keyID)
let keys = keyring.keys.filter { $0.isPublic && $0.isSecret }
return try encrypt(plainData: plainData, keyIDs: keys.map(\.keyID.longIdentifier))
}
func encrypt(plainData: Data, keyIDs: [String]) throws -> Data {
@ -60,11 +61,20 @@ struct ObjectivePGPInterface: PGPInterface {
keyring.findKey(keyID)?.isSecret ?? false
}
var keyID: [String] {
keyring.keys.map(\.keyID.longIdentifier)
func getKeyIDs(type: PGPKey) -> [String] {
getKeys(type: type).map(\.keyID.longIdentifier)
}
var shortKeyID: [String] {
keyring.keys.map(\.keyID.shortIdentifier)
func getShortKeyIDs(type: PGPKey) -> [String] {
getKeys(type: type).map(\.keyID.shortIdentifier)
}
private func getKeys(type: PGPKey) -> [Key] {
switch type {
case .PUBLIC:
keyring.keys.filter(\.isPublic)
case .PRIVATE:
keyring.keys.filter(\.isSecret)
}
}
}

View file

@ -43,14 +43,14 @@ public class PGPAgent {
pgpInterface != nil
}
public func getKeyID() throws -> [String] {
public func getKeyIDs(type: PGPKey) throws -> [String] {
try checkAndInit()
return pgpInterface?.keyID ?? []
return pgpInterface?.getKeyIDs(type: type).sorted() ?? []
}
public func getShortKeyID() throws -> [String] {
public func getShortKeyIDs(type: PGPKey) throws -> [String] {
try checkAndInit()
return pgpInterface?.shortKeyID.sorted() ?? []
return pgpInterface?.getShortKeyIDs(type: type).sorted() ?? []
}
public func decrypt(encryptedData: Data, keyID: String, requestPGPKeyPassphrase: @escaping (String) -> String) throws -> Data? {

View file

@ -14,10 +14,8 @@ protocol PGPInterface {
func encrypt(plainData: Data, keyIDs: [String]) throws -> Data
func containsPublicKey(with keyID: String) -> Bool
func containsPrivateKey(with keyID: String) -> Bool
var keyID: [String] { get }
var shortKeyID: [String] { get }
func getKeyIDs(type: PGPKey) -> [String]
func getShortKeyIDs(type: PGPKey) -> [String]
}