put PasswordEntity into a new file

This commit is contained in:
Bob Sun 2017-02-11 23:45:00 +08:00
parent a131634ef6
commit 6e3a544ba4
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
3 changed files with 35 additions and 21 deletions

View file

@ -78,24 +78,3 @@ class Password {
}
}
extension PasswordEntity {
func decrypt() throws -> Password? {
var password: Password?
let encryptedDataPath = URL(fileURLWithPath: "\(Globals.repositoryPath)/\(rawPath!)")
let encryptedData = try Data(contentsOf: encryptedDataPath)
let decryptedData = try PasswordStore.shared.pgp.decryptData(encryptedData, passphrase: Defaults[.pgpKeyPassphrase])
let plainText = String(data: decryptedData, encoding: .ascii) ?? ""
password = Password(name: name!, plainText: plainText)
return password
}
func encrypt(password: Password) throws -> Data {
name = password.name
rawPath = ""
let plainData = password.getPlainData()
let pgp = PasswordStore.shared.pgp
let encryptedData = try pgp.encryptData(plainData, usingPublicKey: pgp.getKeysOf(.public)[0], armored: false)
return encryptedData
}
}

View file

@ -0,0 +1,31 @@
//
// PasswordEntity.swift
// pass
//
// Created by Mingshen Sun on 11/2/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import Foundation
import SwiftyUserDefaults
extension PasswordEntity {
func decrypt() throws -> Password? {
var password: Password?
let encryptedDataPath = URL(fileURLWithPath: "\(Globals.repositoryPath)/\(rawPath!)")
let encryptedData = try Data(contentsOf: encryptedDataPath)
let decryptedData = try PasswordStore.shared.pgp.decryptData(encryptedData, passphrase: Defaults[.pgpKeyPassphrase])
let plainText = String(data: decryptedData, encoding: .ascii) ?? ""
password = Password(name: name!, plainText: plainText)
return password
}
func encrypt(password: Password) throws -> Data {
name = password.name
rawPath = ""
let plainData = password.getPlainData()
let pgp = PasswordStore.shared.pgp
let encryptedData = try pgp.encryptData(plainData, usingPublicKey: pgp.getKeysOf(.public)[0], armored: false)
return encryptedData
}
}