refactor PasswordEntitiy model, add decrypt extension

This commit is contained in:
Bob Sun 2017-02-02 18:02:43 +08:00
parent a2b3d450ba
commit d44aec6527
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
4 changed files with 51 additions and 25 deletions

View file

@ -11,6 +11,7 @@
DC1208561E35D0BA0042942E /* PasswordsTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC1208551E35D0BA0042942E /* PasswordsTableViewController.swift */; };
DC1208581E35EBE60042942E /* ObjectiveGit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DC1208571E35EBE60042942E /* ObjectiveGit.framework */; };
DC3DC8391E2F63240062A988 /* PasswordDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC3DC8381E2F63240062A988 /* PasswordDetailViewController.swift */; };
DC7E6EEA1E432E48006C2443 /* Password.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC7E6EE91E432E48006C2443 /* Password.swift */; };
DC8963BE1E38AD8300828B09 /* GitRepositoryAuthenticationSettingTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC8963BD1E38AD8300828B09 /* GitRepositoryAuthenticationSettingTableViewController.swift */; };
DC8963C01E38EEB900828B09 /* SSHKeySettingTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC8963BF1E38EEB900828B09 /* SSHKeySettingTableViewController.swift */; };
DC917BD71E2E8231000FDF54 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC917BD61E2E8231000FDF54 /* AppDelegate.swift */; };
@ -37,6 +38,7 @@
DC1208571E35EBE60042942E /* ObjectiveGit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ObjectiveGit.framework; path = Carthage/Build/iOS/ObjectiveGit.framework; sourceTree = "<group>"; };
DC3DC8381E2F63240062A988 /* PasswordDetailViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PasswordDetailViewController.swift; sourceTree = "<group>"; };
DC4A746D1E30FBDE00E8EB18 /* Objective-CBridgingHeader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Objective-CBridgingHeader.h"; sourceTree = "<group>"; };
DC7E6EE91E432E48006C2443 /* Password.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Password.swift; sourceTree = "<group>"; };
DC8963BD1E38AD8300828B09 /* GitRepositoryAuthenticationSettingTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GitRepositoryAuthenticationSettingTableViewController.swift; sourceTree = "<group>"; };
DC8963BF1E38EEB900828B09 /* SSHKeySettingTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SSHKeySettingTableViewController.swift; sourceTree = "<group>"; };
DC917BD31E2E8231000FDF54 /* pass.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = pass.app; sourceTree = BUILT_PRODUCTS_DIR; };
@ -103,23 +105,24 @@
DC917BD51E2E8231000FDF54 /* pass */ = {
isa = PBXGroup;
children = (
DC1208551E35D0BA0042942E /* PasswordsTableViewController.swift */,
DC4A746D1E30FBDE00E8EB18 /* Objective-CBridgingHeader.h */,
DC1208551E35D0BA0042942E /* PasswordsTableViewController.swift */,
DC917BD61E2E8231000FDF54 /* AppDelegate.swift */,
DC917BDA1E2E8231000FDF54 /* Main.storyboard */,
DC8963BF1E38EEB900828B09 /* SSHKeySettingTableViewController.swift */,
DC8963BD1E38AD8300828B09 /* GitRepositoryAuthenticationSettingTableViewController.swift */,
DCA0499B1E3362F400522E8F /* PGPKeySettingTableViewController.swift */,
DCA049991E335CC800522E8F /* GitServerSettingTableViewController.swift */,
DCAAF7441E2FA66800AB94BC /* SettingsTableViewController.swift */,
DCC408C81E30BA1300F29B0E /* pass.xcdatamodeld */,
DC3DC8381E2F63240062A988 /* PasswordDetailViewController.swift */,
DC917BDD1E2E8231000FDF54 /* Assets.xcassets */,
DC917BDF1E2E8231000FDF54 /* LaunchScreen.storyboard */,
DC917BE21E2E8231000FDF54 /* Info.plist */,
DCC408A31E2FCC9E00F29B0E /* PasswordStore.swift */,
DC7E6EE91E432E48006C2443 /* Password.swift */,
DCA049971E33586A00522E8F /* DefaultKeys.swift */,
DCA0499D1E33BAC100522E8F /* Globals.swift */,
DCC408C81E30BA1300F29B0E /* pass.xcdatamodeld */,
DC917BDD1E2E8231000FDF54 /* Assets.xcassets */,
DC917BDA1E2E8231000FDF54 /* Main.storyboard */,
DC917BDF1E2E8231000FDF54 /* LaunchScreen.storyboard */,
DC917BE21E2E8231000FDF54 /* Info.plist */,
);
path = pass;
sourceTree = "<group>";
@ -281,6 +284,7 @@
DCC408A41E2FCC9E00F29B0E /* PasswordStore.swift in Sources */,
DC8963C01E38EEB900828B09 /* SSHKeySettingTableViewController.swift in Sources */,
DCA0499C1E3362F400522E8F /* PGPKeySettingTableViewController.swift in Sources */,
DC7E6EEA1E432E48006C2443 /* Password.swift in Sources */,
DC1208561E35D0BA0042942E /* PasswordsTableViewController.swift in Sources */,
DCAAF7451E2FA66800AB94BC /* SettingsTableViewController.swift in Sources */,
DCA0499A1E335CC800522E8F /* GitServerSettingTableViewController.swift in Sources */,

38
pass/Password.swift Normal file
View 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
}
}

View file

@ -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
}
}

View file

@ -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"/>