2017-02-08 19:27:57 +08:00
|
|
|
//
|
|
|
|
|
// Utils.swift
|
|
|
|
|
// pass
|
|
|
|
|
//
|
|
|
|
|
// Created by Mingshen Sun on 8/2/2017.
|
|
|
|
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
2017-02-09 22:12:25 +08:00
|
|
|
import SwiftyUserDefaults
|
2017-02-19 22:10:36 +08:00
|
|
|
import KeychainAccess
|
2017-02-08 19:27:57 +08:00
|
|
|
|
2017-06-13 11:42:49 +08:00
|
|
|
public class Utils {
|
|
|
|
|
public static func removeFileIfExists(atPath path: String) {
|
2017-02-08 19:27:57 +08:00
|
|
|
let fm = FileManager.default
|
|
|
|
|
do {
|
|
|
|
|
if fm.fileExists(atPath: path) {
|
|
|
|
|
try fm.removeItem(atPath: path)
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
print(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-13 11:42:49 +08:00
|
|
|
public static func removeFileIfExists(at url: URL) {
|
2017-02-08 19:27:57 +08:00
|
|
|
removeFileIfExists(atPath: url.path)
|
|
|
|
|
}
|
2017-06-13 11:42:49 +08:00
|
|
|
|
|
|
|
|
public static func getLastSyncedTimeString() -> String {
|
|
|
|
|
guard let lastSyncedTime = SharedDefaults[.lastSyncedTime] else {
|
2017-03-29 00:56:07 +08:00
|
|
|
return "Oops! Sync again?"
|
2017-02-09 22:12:25 +08:00
|
|
|
}
|
2017-03-29 00:56:07 +08:00
|
|
|
let formatter = DateFormatter()
|
|
|
|
|
formatter.dateStyle = .medium
|
|
|
|
|
formatter.timeStyle = .short
|
|
|
|
|
return formatter.string(from: lastSyncedTime)
|
2017-02-09 22:12:25 +08:00
|
|
|
}
|
2017-02-11 22:00:04 +08:00
|
|
|
|
2017-06-13 11:42:49 +08:00
|
|
|
public static func generatePassword(length: Int) -> String{
|
|
|
|
|
switch SharedDefaults[.passwordGeneratorFlavor] {
|
2017-02-20 21:56:23 +08:00
|
|
|
case "Random":
|
|
|
|
|
return randomString(length: length)
|
2017-03-08 00:57:49 -08:00
|
|
|
case "Apple":
|
2017-02-20 21:56:23 +08:00
|
|
|
return Keychain.generatePassword()
|
|
|
|
|
default:
|
|
|
|
|
return randomString(length: length)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 11:42:49 +08:00
|
|
|
public static func randomString(length: Int) -> String {
|
2017-02-11 22:00:04 +08:00
|
|
|
|
2017-04-07 17:46:11 -07:00
|
|
|
let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*_+-="
|
2017-02-11 22:00:04 +08:00
|
|
|
let len = UInt32(letters.length)
|
|
|
|
|
|
|
|
|
|
var randomString = ""
|
|
|
|
|
|
|
|
|
|
for _ in 0 ..< length {
|
|
|
|
|
let rand = arc4random_uniform(len)
|
|
|
|
|
var nextChar = letters.character(at: Int(rand))
|
|
|
|
|
randomString += NSString(characters: &nextChar, length: 1) as String
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return randomString
|
|
|
|
|
}
|
2017-02-16 00:54:42 +08:00
|
|
|
|
2017-06-13 11:42:49 +08:00
|
|
|
public static func getPasswordFromKeychain(name: String) -> String? {
|
2017-06-14 00:25:38 +08:00
|
|
|
let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
|
2017-02-19 22:10:36 +08:00
|
|
|
do {
|
|
|
|
|
return try keychain.getString(name)
|
|
|
|
|
} catch {
|
|
|
|
|
print(error)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 11:42:49 +08:00
|
|
|
public static func addPasswordToKeychain(name: String, password: String?) {
|
2017-06-14 00:25:38 +08:00
|
|
|
let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
|
2017-02-19 22:10:36 +08:00
|
|
|
keychain[name] = password
|
|
|
|
|
}
|
2017-06-14 00:25:38 +08:00
|
|
|
|
2017-06-13 11:42:49 +08:00
|
|
|
public static func removeKeychain(name: String) {
|
2017-06-14 00:25:38 +08:00
|
|
|
let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
|
2017-02-19 22:10:36 +08:00
|
|
|
do {
|
|
|
|
|
try keychain.remove(name)
|
|
|
|
|
} catch {
|
|
|
|
|
print(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-14 00:25:38 +08:00
|
|
|
|
2017-06-13 11:42:49 +08:00
|
|
|
public static func removeAllKeychain() {
|
2017-06-14 00:25:38 +08:00
|
|
|
let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
|
2017-02-19 22:10:36 +08:00
|
|
|
do {
|
|
|
|
|
try keychain.removeAll()
|
|
|
|
|
} catch {
|
|
|
|
|
print(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-27 23:56:24 +08:00
|
|
|
public static func copyToPasteboard(textToCopy: String?) {
|
2017-02-23 17:56:12 +08:00
|
|
|
guard textToCopy != nil else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
UIPasteboard.general.string = textToCopy
|
|
|
|
|
}
|
2017-06-13 11:42:49 +08:00
|
|
|
public static func attributedPassword(plainPassword: String) -> NSAttributedString{
|
2017-02-26 00:58:18 +08:00
|
|
|
let attributedPassword = NSMutableAttributedString.init(string: plainPassword)
|
|
|
|
|
// draw all digits in the password into red
|
|
|
|
|
// draw all punctuation characters in the password into blue
|
|
|
|
|
for (index, element) in plainPassword.unicodeScalars.enumerated() {
|
2017-07-07 11:32:03 +08:00
|
|
|
var charColor = UIColor.darkText
|
2017-02-26 00:58:18 +08:00
|
|
|
if NSCharacterSet.decimalDigits.contains(element) {
|
2017-10-07 22:16:59 -07:00
|
|
|
charColor = Globals.digitColor
|
2017-03-24 01:34:45 +08:00
|
|
|
} else if !NSCharacterSet.letters.contains(element) {
|
2017-10-07 23:05:26 -07:00
|
|
|
charColor = Globals.symbolColor
|
|
|
|
|
} else {
|
2017-10-07 22:16:59 -07:00
|
|
|
charColor = Globals.letterColor
|
2017-02-26 00:58:18 +08:00
|
|
|
}
|
2017-09-23 16:29:03 +08:00
|
|
|
attributedPassword.addAttribute(NSAttributedStringKey.foregroundColor, value: charColor, range: NSRange(location: index, length: 1))
|
2017-02-26 00:58:18 +08:00
|
|
|
}
|
|
|
|
|
return attributedPassword
|
|
|
|
|
}
|
2017-06-13 11:42:49 +08:00
|
|
|
public static func initDefaultKeys() {
|
|
|
|
|
if SharedDefaults[.passwordGeneratorFlavor] == "" {
|
|
|
|
|
SharedDefaults[.passwordGeneratorFlavor] = "Random"
|
2017-03-08 00:57:49 -08:00
|
|
|
}
|
|
|
|
|
}
|
2017-02-08 19:27:57 +08:00
|
|
|
}
|
2017-02-10 00:49:49 +08:00
|
|
|
|