Add ignore .gpg-id switch default ON

This commit is contained in:
Mingshen Sun 2021-01-07 21:58:38 -08:00
parent 6280b1522b
commit a62792bd11
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
9 changed files with 91 additions and 13 deletions

View file

@ -96,6 +96,36 @@ public class PGPAgent {
return try pgpInterface.encrypt(plainData: plainData, keyID: keyID)
}
public func decrypt(encryptedData: Data, requestPGPKeyPassphrase: (String) -> String) throws -> Data? {
// Remember the previous status and set the current status
let previousDecryptStatus = self.latestDecryptStatus
self.latestDecryptStatus = false
// Init keys.
try checkAndInit()
// Get the PGP key passphrase.
var passphrase = ""
if previousDecryptStatus == false {
passphrase = requestPGPKeyPassphrase("default")
} else {
passphrase = keyStore.get(for: Globals.pgpKeyPassphrase) ?? requestPGPKeyPassphrase("default")
}
// Decrypt.
guard let result = try pgpInterface!.decrypt(encryptedData: encryptedData, keyID: nil, passphrase: passphrase) else {
return nil
}
// The decryption step has succeed.
self.latestDecryptStatus = true
return result
}
public func encrypt(plainData: Data) throws -> Data {
try checkAndInit()
guard let pgpInterface = pgpInterface else {
throw AppError.encryption
}
return try pgpInterface.encrypt(plainData: plainData, keyID: nil)
}
public var isPrepared: Bool {
keyStore.contains(key: PgpKey.PUBLIC.getKeychainKey())
&& keyStore.contains(key: PgpKey.PRIVATE.getKeychainKey())