Format code with SwiftFormat automatically in every build

This commit is contained in:
Danny Moesch 2020-06-28 21:25:40 +02:00 committed by Mingshen Sun
parent f167ab7549
commit 7f9f0e43b2
100 changed files with 1124 additions and 1063 deletions

View file

@ -9,10 +9,9 @@
import Crypto
struct GopenPGPInterface: PGPInterface {
private static let errorMapping: [String: Error] = [
"gopenpgp: error in unlocking key: openpgp: invalid data: private key checksum failure": AppError.WrongPassphrase,
"openpgp: incorrect key": AppError.KeyExpiredOrIncompatible,
"gopenpgp: error in unlocking key: openpgp: invalid data: private key checksum failure": AppError.WrongPassphrase,
"openpgp: incorrect key": AppError.KeyExpiredOrIncompatible,
]
private var publicKeys: [String: CryptoKey] = [:]
@ -43,26 +42,25 @@ struct GopenPGPInterface: PGPInterface {
}
privateKeys[k.getFingerprint().lowercased()] = k
}
}
func extractKeysFromArmored(str: String) -> [String] {
var keys: [String] = []
var key: String = ""
for line in str.splitByNewline() {
if line.trimmed.uppercased().hasPrefix("-----BEGIN PGP") {
key = ""
key += line
} else if line.trimmed.uppercased().hasPrefix("-----END PGP") {
key += line
keys.append(key)
} else {
key += line
}
var keys: [String] = []
var key: String = ""
for line in str.splitByNewline() {
if line.trimmed.uppercased().hasPrefix("-----BEGIN PGP") {
key = ""
key += line
} else if line.trimmed.uppercased().hasPrefix("-----END PGP") {
key += line
keys.append(key)
} else {
key += line
}
key += "\n"
}
return keys
}
}
return keys
}
func containsPublicKey(with keyID: String) -> Bool {
publicKeys.keys.contains(where: { key in key.hasSuffix(keyID.lowercased()) })
@ -73,7 +71,7 @@ struct GopenPGPInterface: PGPInterface {
}
func decrypt(encryptedData: Data, keyID: String, passphrase: String) throws -> Data? {
guard let e = privateKeys.first(where: { (key, _) in key.hasSuffix(keyID.lowercased()) }),
guard let e = privateKeys.first(where: { key, _ in key.hasSuffix(keyID.lowercased()) }),
let privateKey = privateKeys[e.key] else {
throw AppError.Decryption
}
@ -97,7 +95,7 @@ struct GopenPGPInterface: PGPInterface {
}
func encrypt(plainData: Data, keyID: String) throws -> Data {
guard let e = publicKeys.first(where: { (key, _) in key.hasSuffix(keyID.lowercased()) }),
guard let e = publicKeys.first(where: { key, _ in key.hasSuffix(keyID.lowercased()) }),
let publicKey = publicKeys[e.key] else {
throw AppError.Encryption
}
@ -124,11 +122,11 @@ struct GopenPGPInterface: PGPInterface {
}
var keyID: [String] {
return publicKeys.keys.map({ $0.uppercased() })
publicKeys.keys.map { $0.uppercased() }
}
var shortKeyID: [String] {
return publicKeys.keys.map({ $0.suffix(8).uppercased()})
publicKeys.keys.map { $0.suffix(8).uppercased() }
}
private func createPgpMessage(from encryptedData: Data) -> CryptoPGPMessage? {

View file

@ -9,7 +9,6 @@
import ObjectivePGP
struct ObjectivePGPInterface: PGPInterface {
private let publicKey: Key
private let privateKey: Key
@ -30,11 +29,11 @@ struct ObjectivePGPInterface: PGPInterface {
self.privateKey = privateKey
}
func decrypt(encryptedData: Data, keyID: String, passphrase: String) throws -> Data? {
return try ObjectivePGP.decrypt(encryptedData, andVerifySignature: false, using: keyring.keys) { _ in passphrase }
func decrypt(encryptedData: Data, keyID _: String, passphrase: String) throws -> Data? {
try ObjectivePGP.decrypt(encryptedData, andVerifySignature: false, using: keyring.keys) { _ in passphrase }
}
func encrypt(plainData: Data, keyID: String) throws -> Data {
func encrypt(plainData: Data, keyID _: String) throws -> Data {
let encryptedData = try ObjectivePGP.encrypt(plainData, addSignature: false, using: keyring.keys, passphraseForKey: nil)
if Defaults.encryptInArmored {
return Armor.armored(encryptedData, as: .message).data(using: .ascii)!
@ -51,10 +50,10 @@ struct ObjectivePGPInterface: PGPInterface {
}
var keyID: [String] {
return keyring.keys.map({ $0.keyID.longIdentifier })
keyring.keys.map(\.keyID.longIdentifier)
}
var shortKeyID: [String] {
return keyring.keys.map({ $0.keyID.shortIdentifier })
keyring.keys.map(\.keyID.shortIdentifier)
}
}

View file

@ -7,7 +7,6 @@
//
public class PGPAgent {
public static let shared: PGPAgent = PGPAgent()
private let keyStore: KeyStore
@ -20,7 +19,7 @@ public class PGPAgent {
public func initKeys() throws {
guard let publicKey: String = keyStore.get(for: PgpKey.PUBLIC.getKeychainKey()),
let privateKey: String = keyStore.get(for: PgpKey.PRIVATE.getKeychainKey()) else {
let privateKey: String = keyStore.get(for: PgpKey.PRIVATE.getKeychainKey()) else {
pgpInterface = nil
throw AppError.KeyImport
}
@ -52,7 +51,7 @@ public class PGPAgent {
throw AppError.Decryption
}
var keyID = keyID;
var keyID = keyID
if !pgpInterface.containsPrivateKey(with: keyID) {
if pgpInterface.keyID.count == 1 {
keyID = pgpInterface.keyID.first!
@ -62,8 +61,8 @@ public class PGPAgent {
}
// Remember the previous status and set the current status
let previousDecryptStatus = self.latestDecryptStatus
self.latestDecryptStatus = false
let previousDecryptStatus = latestDecryptStatus
latestDecryptStatus = false
// Get the PGP key passphrase.
var passphrase = ""
@ -77,7 +76,7 @@ public class PGPAgent {
return nil
}
// The decryption step has succeed.
self.latestDecryptStatus = true
latestDecryptStatus = true
return result
}
@ -98,7 +97,7 @@ public class PGPAgent {
}
public var isPrepared: Bool {
return keyStore.contains(key: PgpKey.PUBLIC.getKeychainKey())
keyStore.contains(key: PgpKey.PUBLIC.getKeychainKey())
&& keyStore.contains(key: PgpKey.PRIVATE.getKeychainKey())
}

View file

@ -7,7 +7,6 @@
//
protocol PGPInterface {
func decrypt(encryptedData: Data, keyID: String, passphrase: String) throws -> Data?
func encrypt(plainData: Data, keyID: String) throws -> Data