handle wrong GPG passphrase
This commit is contained in:
parent
c50cf8e0a7
commit
fd8cb300d7
3 changed files with 46 additions and 31 deletions
|
|
@ -10,10 +10,16 @@ import Foundation
|
|||
import SwiftyUserDefaults
|
||||
|
||||
class Password {
|
||||
var name = ""
|
||||
var password = ""
|
||||
var name: String
|
||||
var password: String
|
||||
var additions: [String: String]
|
||||
|
||||
init() {
|
||||
name = ""
|
||||
password = ""
|
||||
additions = [:]
|
||||
}
|
||||
|
||||
init(name: String, password: String, additions: [String: String]) {
|
||||
self.name = name
|
||||
self.password = password
|
||||
|
|
@ -22,29 +28,25 @@ class Password {
|
|||
}
|
||||
|
||||
extension PasswordEntity {
|
||||
func decrypt() -> Password? {
|
||||
func decrypt() throws -> Password? {
|
||||
var password: Password?
|
||||
let encryptedDataPath = URL(fileURLWithPath: "\(Globals.shared.documentPath)/\(rawPath!)")
|
||||
do {
|
||||
let encryptedData = try Data(contentsOf: encryptedDataPath)
|
||||
let decryptedData = try PasswordStore.shared.pgp.decryptData(encryptedData, passphrase: Defaults[.pgpKeyPassphrase])
|
||||
let plain = String(data: decryptedData, encoding: .ascii) ?? ""
|
||||
var decrypted_password = ""
|
||||
var decrypted_addtions = [String: String]()
|
||||
plain.enumerateLines(invoking: { line, _ in
|
||||
let item = line.characters.split(separator: ":").map(String.init)
|
||||
if item.count == 1 {
|
||||
decrypted_password = item[0]
|
||||
} else {
|
||||
let key = item[0]
|
||||
let value = item[1].trimmingCharacters(in: .whitespaces)
|
||||
decrypted_addtions[key] = value
|
||||
}
|
||||
})
|
||||
password = Password(name: name!, password: decrypted_password, additions: decrypted_addtions)
|
||||
} catch let error as NSError {
|
||||
print(error.debugDescription)
|
||||
}
|
||||
let encryptedData = try Data(contentsOf: encryptedDataPath)
|
||||
let decryptedData = try PasswordStore.shared.pgp.decryptData(encryptedData, passphrase: Defaults[.pgpKeyPassphrase])
|
||||
let plain = String(data: decryptedData, encoding: .ascii) ?? ""
|
||||
var decrypted_password = ""
|
||||
var decrypted_addtions = [String: String]()
|
||||
plain.enumerateLines(invoking: { line, _ in
|
||||
let item = line.characters.split(separator: ":").map(String.init)
|
||||
if item.count == 1 {
|
||||
decrypted_password = item[0]
|
||||
} else {
|
||||
let key = item[0]
|
||||
let value = item[1].trimmingCharacters(in: .whitespaces)
|
||||
decrypted_addtions[key] = value
|
||||
}
|
||||
})
|
||||
password = Password(name: name!, password: decrypted_password, additions: decrypted_addtions)
|
||||
return password
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue