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.
|
|
|
|
|
//
|
|
|
|
|
|
2020-07-04 22:05:20 +02:00
|
|
|
public enum Utils {
|
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
|
|
|
|
|
}
|
2019-06-24 20:46:17 +02:00
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
public static func attributedPassword(plainPassword: String) -> NSAttributedString {
|
|
|
|
|
let attributedPassword = NSMutableAttributedString(string: plainPassword)
|
2017-02-26 00:58:18 +08:00
|
|
|
// 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) {
|
2019-10-01 22:36:22 +02:00
|
|
|
charColor = Colors.systemRed
|
2017-03-24 01:34:45 +08:00
|
|
|
} else if !NSCharacterSet.letters.contains(element) {
|
2019-10-01 22:36:22 +02:00
|
|
|
charColor = Colors.systemBlue
|
2017-10-07 23:05:26 -07:00
|
|
|
} else {
|
2019-10-01 22:36:22 +02:00
|
|
|
charColor = Colors.label
|
2017-02-26 00:58:18 +08:00
|
|
|
}
|
2019-05-01 17:49:27 +02:00
|
|
|
attributedPassword.addAttribute(NSAttributedString.Key.foregroundColor, value: charColor, range: NSRange(location: index, length: 1))
|
2017-02-26 00:58:18 +08:00
|
|
|
}
|
|
|
|
|
return attributedPassword
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2018-09-23 22:16:13 +08:00
|
|
|
public static func alert(title: String, message: String, controller: UIViewController, handler: ((UIAlertAction) -> Void)? = nil, completion: (() -> Void)? = nil) {
|
2019-05-01 17:49:27 +02:00
|
|
|
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
|
|
|
|
|
alert.addAction(UIAlertAction(title: "Ok".localize(), style: UIAlertAction.Style.default, handler: handler))
|
2018-09-23 22:16:13 +08:00
|
|
|
controller.present(alert, animated: true, completion: completion)
|
|
|
|
|
}
|
2020-04-13 15:16:03 -07:00
|
|
|
|
|
|
|
|
public static func createRequestPGPKeyPassphraseHandler(controller: UIViewController) -> (String) -> String {
|
2020-06-28 21:25:40 +02:00
|
|
|
{ keyID in
|
2020-04-13 15:16:03 -07:00
|
|
|
let sem = DispatchSemaphore(value: 0)
|
|
|
|
|
var passphrase = ""
|
|
|
|
|
DispatchQueue.main.async {
|
2020-04-13 19:15:52 -07:00
|
|
|
let title = "Passphrase".localize() + " (\(keyID.suffix(8)))"
|
|
|
|
|
let message = "FillInPgpPassphrase.".localize()
|
|
|
|
|
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
2020-07-05 00:16:22 +02:00
|
|
|
alert.addAction(
|
|
|
|
|
UIAlertAction.ok { _ in
|
|
|
|
|
passphrase = alert.textFields?.first?.text ?? ""
|
|
|
|
|
sem.signal()
|
|
|
|
|
}
|
|
|
|
|
)
|
2020-06-28 21:25:40 +02:00
|
|
|
alert.addTextField { textField in
|
2020-04-13 19:15:52 -07:00
|
|
|
textField.text = AppKeychain.shared.get(for: AppKeychain.getPGPKeyPassphraseKey(keyID: keyID)) ?? ""
|
2020-04-13 15:16:03 -07:00
|
|
|
textField.isSecureTextEntry = true
|
2020-04-18 22:35:17 -07:00
|
|
|
}
|
|
|
|
|
controller.present(alert, animated: true)
|
2020-04-13 15:16:03 -07:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
_ = sem.wait(timeout: DispatchTime.distantFuture)
|
2020-04-13 15:16:03 -07:00
|
|
|
if Defaults.isRememberPGPPassphraseOn {
|
2020-04-13 19:15:52 -07:00
|
|
|
AppKeychain.shared.add(string: passphrase, for: AppKeychain.getPGPKeyPassphraseKey(keyID: keyID))
|
2020-04-13 15:16:03 -07:00
|
|
|
}
|
|
|
|
|
return passphrase
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-08 19:27:57 +08:00
|
|
|
}
|