Wrap GopenPGP errors into more understandable app errors
This commit is contained in:
parent
fa820e277f
commit
44cb864642
5 changed files with 23 additions and 4 deletions
|
|
@ -10,6 +10,11 @@ import Crypto
|
|||
|
||||
struct GopenPgp: PgpInterface {
|
||||
|
||||
private static let errorMapping: [String: Error] = [
|
||||
"openpgp: invalid data: private key checksum failure": AppError.WrongPassphrase,
|
||||
"openpgp: incorrect key": AppError.KeyExpiredOrIncompatible,
|
||||
]
|
||||
|
||||
private let publicKey: CryptoKeyRing
|
||||
private let privateKey: CryptoKeyRing
|
||||
|
||||
|
|
@ -22,9 +27,17 @@ struct GopenPgp: PgpInterface {
|
|||
}
|
||||
|
||||
func decrypt(encryptedData: Data, passphrase: String) throws -> Data? {
|
||||
try privateKey.unlock(withPassphrase: passphrase)
|
||||
do {
|
||||
try privateKey.unlock(withPassphrase: passphrase)
|
||||
} catch {
|
||||
throw Self.errorMapping[error.localizedDescription, default: error]
|
||||
}
|
||||
let message = createPgpMessage(from: encryptedData)
|
||||
return try privateKey.decrypt(message, verifyKey: nil, verifyTime: 0).data
|
||||
do {
|
||||
return try privateKey.decrypt(message, verifyKey: nil, verifyTime: 0).data
|
||||
} catch {
|
||||
throw Self.errorMapping[error.localizedDescription, default: error]
|
||||
}
|
||||
}
|
||||
|
||||
func encrypt(plainData: Data) throws -> Data {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue