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
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue