Cleanup the previously set keys before init
This commit is contained in:
parent
f2726fa803
commit
584b634689
1 changed files with 12 additions and 1 deletions
|
|
@ -52,6 +52,16 @@ public class PGPAgent {
|
||||||
}
|
}
|
||||||
|
|
||||||
public func initPGPKey(_ keyType: PgpKey) throws {
|
public func initPGPKey(_ keyType: PgpKey) throws {
|
||||||
|
// Clean up the previously set public/private key.
|
||||||
|
switch keyType {
|
||||||
|
case .PUBLIC:
|
||||||
|
self.publicKey = nil
|
||||||
|
self.publicKeyV2 = nil
|
||||||
|
case .PRIVATE:
|
||||||
|
self.privateKey = nil
|
||||||
|
self.privateKeyV2 = nil
|
||||||
|
}
|
||||||
|
|
||||||
// Read the key data from keychain.
|
// Read the key data from keychain.
|
||||||
guard let pgpKeyData: Data = AppKeychain.get(for: keyType.getKeychainKey()) else {
|
guard let pgpKeyData: Data = AppKeychain.get(for: keyType.getKeychainKey()) else {
|
||||||
throw AppError.KeyImport
|
throw AppError.KeyImport
|
||||||
|
|
@ -62,6 +72,7 @@ public class PGPAgent {
|
||||||
|
|
||||||
// Try GopenpgpwrapperReadKey first.
|
// Try GopenpgpwrapperReadKey first.
|
||||||
if let key = GopenpgpwrapperReadKey(pgpKeyData) {
|
if let key = GopenpgpwrapperReadKey(pgpKeyData) {
|
||||||
|
print("GopenpgpwrapperReadKey \(keyType)")
|
||||||
switch keyType {
|
switch keyType {
|
||||||
case .PUBLIC:
|
case .PUBLIC:
|
||||||
self.publicKey = key
|
self.publicKey = key
|
||||||
|
|
@ -76,6 +87,7 @@ public class PGPAgent {
|
||||||
// [ObjectivePGP.readKeys MAY CRASH!!!]
|
// [ObjectivePGP.readKeys MAY CRASH!!!]
|
||||||
if let keys = try? ObjectivePGP.readKeys(from: pgpKeyData),
|
if let keys = try? ObjectivePGP.readKeys(from: pgpKeyData),
|
||||||
let key = keys.first {
|
let key = keys.first {
|
||||||
|
print("ObjectivePGP \(keyType)")
|
||||||
keyring.import(keys: keys)
|
keyring.import(keys: keys)
|
||||||
switch keyType {
|
switch keyType {
|
||||||
case .PUBLIC:
|
case .PUBLIC:
|
||||||
|
|
@ -121,7 +133,6 @@ public class PGPAgent {
|
||||||
// Try ObjectivePGP.
|
// Try ObjectivePGP.
|
||||||
if privateKeyV2 != nil {
|
if privateKeyV2 != nil {
|
||||||
if let decryptedData = try? ObjectivePGP.decrypt(encryptedData, andVerifySignature: false, using: keyring.keys, passphraseForKey: {(_) in passphrase}) {
|
if let decryptedData = try? ObjectivePGP.decrypt(encryptedData, andVerifySignature: false, using: keyring.keys, passphraseForKey: {(_) in passphrase}) {
|
||||||
print(decryptedData.base64EncodedString())
|
|
||||||
return decryptedData
|
return decryptedData
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue