refactor PasswordEntitiy model, add decrypt extension
This commit is contained in:
parent
a2b3d450ba
commit
d44aec6527
4 changed files with 51 additions and 25 deletions
38
pass/Password.swift
Normal file
38
pass/Password.swift
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
//
|
||||
// Password.swift
|
||||
// pass
|
||||
//
|
||||
// Created by Mingshen Sun on 2/2/2017.
|
||||
// Copyright © 2017 Bob Sun. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftyUserDefaults
|
||||
|
||||
class Password {
|
||||
var name = ""
|
||||
var password = ""
|
||||
var additions: [String: String]?
|
||||
|
||||
init(name: String, password: String, additions: [String: String]?) {
|
||||
self.name = name
|
||||
self.password = password
|
||||
self.additions = additions
|
||||
}
|
||||
}
|
||||
|
||||
extension PasswordEntity {
|
||||
func decrypt() -> 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) ?? ""
|
||||
password = Password(name: name!, password: plain, additions: nil)
|
||||
} catch let error as NSError {
|
||||
print(error.debugDescription)
|
||||
}
|
||||
return password
|
||||
}
|
||||
}
|
||||
|
|
@ -16,23 +16,7 @@ class PasswordDetailViewController: UIViewController {
|
|||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
let encryptedDataURL = URL(fileURLWithPath: "\(Globals.shared.documentPath)/\(passwordEntity!.rawPath!)")
|
||||
let fm = FileManager.default
|
||||
if fm.fileExists(atPath: encryptedDataURL.path){
|
||||
print("file exist")
|
||||
} else {
|
||||
print("file doesnt exist")
|
||||
}
|
||||
|
||||
do {
|
||||
let encryptedData = try Data(contentsOf: encryptedDataURL)
|
||||
let decryptedData = try PasswordStore.shared.pgp.decryptData(encryptedData, passphrase: Defaults[.pgpKeyPassphrase])
|
||||
let plain = String(data: decryptedData, encoding: .ascii) ?? ""
|
||||
print(plain)
|
||||
passwordTextView.text = plain
|
||||
} catch let error as NSError {
|
||||
print(error.debugDescription)
|
||||
}
|
||||
|
||||
let password = passwordEntity!.decrypt()!
|
||||
passwordTextView.text = password.password
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="11759" systemVersion="16C67" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
|
||||
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="11759" systemVersion="16D32" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
|
||||
<entity name="PasswordEntity" representedClassName="PasswordEntity" syncable="YES" codeGenerationType="class">
|
||||
<attribute name="name" optional="YES" attributeType="String" syncable="YES"/>
|
||||
<attribute name="raw" optional="YES" attributeType="Binary" syncable="YES"/>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue