handle wrong GPG passphrase

This commit is contained in:
Bob Sun 2017-02-06 14:28:57 +08:00
parent c50cf8e0a7
commit fd8cb300d7
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
3 changed files with 46 additions and 31 deletions

View file

@ -10,10 +10,16 @@ import Foundation
import SwiftyUserDefaults import SwiftyUserDefaults
class Password { class Password {
var name = "" var name: String
var password = "" var password: String
var additions: [String: String] var additions: [String: String]
init() {
name = ""
password = ""
additions = [:]
}
init(name: String, password: String, additions: [String: String]) { init(name: String, password: String, additions: [String: String]) {
self.name = name self.name = name
self.password = password self.password = password
@ -22,29 +28,25 @@ class Password {
} }
extension PasswordEntity { extension PasswordEntity {
func decrypt() -> Password? { func decrypt() throws -> Password? {
var password: Password? var password: Password?
let encryptedDataPath = URL(fileURLWithPath: "\(Globals.shared.documentPath)/\(rawPath!)") let encryptedDataPath = URL(fileURLWithPath: "\(Globals.shared.documentPath)/\(rawPath!)")
do { let encryptedData = try Data(contentsOf: encryptedDataPath)
let encryptedData = try Data(contentsOf: encryptedDataPath) let decryptedData = try PasswordStore.shared.pgp.decryptData(encryptedData, passphrase: Defaults[.pgpKeyPassphrase])
let decryptedData = try PasswordStore.shared.pgp.decryptData(encryptedData, passphrase: Defaults[.pgpKeyPassphrase]) let plain = String(data: decryptedData, encoding: .ascii) ?? ""
let plain = String(data: decryptedData, encoding: .ascii) ?? "" var decrypted_password = ""
var decrypted_password = "" var decrypted_addtions = [String: String]()
var decrypted_addtions = [String: String]() plain.enumerateLines(invoking: { line, _ in
plain.enumerateLines(invoking: { line, _ in let item = line.characters.split(separator: ":").map(String.init)
let item = line.characters.split(separator: ":").map(String.init) if item.count == 1 {
if item.count == 1 { decrypted_password = item[0]
decrypted_password = item[0] } else {
} else { let key = item[0]
let key = item[0] let value = item[1].trimmingCharacters(in: .whitespaces)
let value = item[1].trimmingCharacters(in: .whitespaces) decrypted_addtions[key] = value
decrypted_addtions[key] = value }
} })
}) password = Password(name: name!, password: decrypted_password, additions: decrypted_addtions)
password = Password(name: name!, password: decrypted_password, additions: decrypted_addtions)
} catch let error as NSError {
print(error.debugDescription)
}
return password return password
} }
} }

View file

@ -10,6 +10,7 @@ import UIKit
class PasswordDetailTableViewController: UITableViewController, UIGestureRecognizerDelegate { class PasswordDetailTableViewController: UITableViewController, UIGestureRecognizerDelegate {
var passwordEntity: PasswordEntity? var passwordEntity: PasswordEntity?
var password = Password()
struct TableCell { struct TableCell {
var title: String var title: String
@ -26,7 +27,15 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
tableView.register(UINib(nibName: "LabelTableViewCell", bundle: nil), forCellReuseIdentifier: "labelCell") tableView.register(UINib(nibName: "LabelTableViewCell", bundle: nil), forCellReuseIdentifier: "labelCell")
let password = passwordEntity!.decrypt()! do {
password = try passwordEntity!.decrypt()!
} catch {
let alert = UIAlertController(title: "Cannot Show Password", message: error.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {(UIAlertAction) -> Void in
self.navigationController!.popViewController(animated: true)
}))
self.present(alert, animated: true, completion: nil)
}
var tableDataIndex = 0 var tableDataIndex = 0
tableData.append(TableSection(title: "", item: [])) tableData.append(TableSection(title: "", item: []))

View file

@ -121,8 +121,12 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
} else { } else {
password = passwordEntities![index] password = passwordEntities![index]
} }
let decryptedPassword = password.decrypt()! do {
UIPasteboard.general.string = decryptedPassword.password let decryptedPassword = try password.decrypt()!
UIPasteboard.general.string = decryptedPassword.password
} catch {
print(error)
}
} }
func generateSections(item: [PasswordEntity]) { func generateSections(item: [PasswordEntity]) {
@ -174,14 +178,14 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
if let viewController = segue.destination as? PasswordDetailTableViewController { if let viewController = segue.destination as? PasswordDetailTableViewController {
let selectedIndexPath = self.tableView.indexPath(for: sender as! UITableViewCell)! let selectedIndexPath = self.tableView.indexPath(for: sender as! UITableViewCell)!
let index = sections[selectedIndexPath.section].index + selectedIndexPath.row let index = sections[selectedIndexPath.section].index + selectedIndexPath.row
let password: PasswordEntity let passwordEntity: PasswordEntity
if searchController.isActive && searchController.searchBar.text != "" { if searchController.isActive && searchController.searchBar.text != "" {
password = filteredPasswordEntities[index] passwordEntity = filteredPasswordEntities[index]
} else { } else {
password = passwordEntities![index] passwordEntity = passwordEntities![index]
} }
viewController.passwordEntity = password viewController.passwordEntity = passwordEntity
viewController.navigationItem.title = password.name viewController.navigationItem.title = passwordEntity.name
} }
} }
} }