DSignatureManager.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // DSignatureManager.swift
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2023/9/28.
  6. //
  7. import Cocoa
  8. class DSignatureManager: NSObject {
  9. public static let manager = DSignatureManager.init()
  10. lazy var signatures: NSArray = []
  11. override init() {
  12. super.init()
  13. self.loadAllKeyChainCertificates()
  14. // self.getAllKeychainItems()
  15. }
  16. func loadAllKeyChainCertificates () {
  17. let keychainQuery: [String: Any] = [
  18. kSecClass as String : kSecClassIdentity,
  19. kSecReturnData as String : true,
  20. kSecReturnAttributes as String : true,
  21. kSecReturnRef as String : true,
  22. kSecMatchLimit as String: kSecMatchLimitAll,
  23. kSecReturnPersistentRef as String: true
  24. ]
  25. var results: AnyObject?
  26. // let status = SecItemCopyMatching(keychainQuery as CFDictionary, &results)
  27. let status = withUnsafeMutablePointer(to: &results) {
  28. SecItemCopyMatching(keychainQuery as CFDictionary, UnsafeMutablePointer($0))
  29. }
  30. var resultArr = results as! NSArray;
  31. for result in resultArr {
  32. let tResult = result as![String:Any]
  33. let identify = tResult[kSecValueRef as String] as! AnyObject
  34. var certificate: SecCertificate?
  35. // let status = SecIdentityCopyCertificate(identify, &certificate)
  36. // if (status == errSecSuccess) {
  37. let label = tResult[kSecAttrLabel as String] as? String
  38. let account = tResult[kSecAttrAccount as String] as? String
  39. guard let Issuer = tResult[kSecAttrIssuer as String] as? Data else { return }
  40. let IssuerKey = String(data: Issuer, encoding: .utf8)
  41. let valueReflabel = tResult[kSecValueRef as String] as? String
  42. guard let data = tResult[kSecValueData as String] as? Data else {
  43. return
  44. }
  45. let dataKey = String(data: data, encoding: .utf8)
  46. if let key = tResult[kSecAttrLabel as String] as? String,
  47. let value = tResult[kSecValueData as String] as? Data {
  48. print(value)
  49. }
  50. else if let key = tResult[kSecAttrLabel as String] as? String,
  51. let value = tResult[kSecValueRef as String] {
  52. print(value)
  53. }
  54. print("adsf")
  55. // {
  56. // values[key] = String(data: value, encoding:.utf8) as AnyObject?
  57. // }
  58. // }
  59. // SecIdentityCopyCertificate(identify, &certificate)
  60. }
  61. }
  62. func getAllKeychainItems() {
  63. let classes = [kSecClassGenericPassword as String, // Generic password items
  64. kSecClassInternetPassword as String, // Internet password items
  65. kSecClassCertificate as String, // Certificate items
  66. kSecClassKey as String, // Cryptographic key items
  67. kSecClassIdentity as String] // Identity items
  68. // classes.forEach { secClass in
  69. let items = getAllKeyChainItemsOfClass( kSecClassIdentity as String )
  70. NSLog(items.description)
  71. // }
  72. }
  73. func getAllKeyChainItemsOfClass(_ secClass: String) -> [String: AnyObject] {
  74. let query: [String: Any] = [
  75. kSecClass as String : secClass,
  76. kSecReturnData as String : true,
  77. kSecReturnAttributes as String : true,
  78. kSecReturnRef as String : true,
  79. kSecMatchLimit as String: kSecMatchLimitAll,
  80. ]
  81. var result: AnyObject?
  82. let lastResultCode = withUnsafeMutablePointer(to: &result) {
  83. SecItemCopyMatching(query as CFDictionary, UnsafeMutablePointer($0))
  84. }
  85. var values = [String: AnyObject]()
  86. if lastResultCode == noErr {
  87. let array = result as? Array<Dictionary<String, Any>>
  88. for item in array! {
  89. let value = item[kSecAttrLabel as String] as? String
  90. if let key = item[kSecAttrAccount as String] as? String,
  91. let value = item[kSecValueData as String] as? Data {
  92. values[key] = String(data: value, encoding:.utf8) as AnyObject?
  93. }
  94. else if let key = item[kSecAttrLabel as String] as? String,
  95. let value = item[kSecValueRef as String] {
  96. print(value)
  97. values[key] = value as AnyObject
  98. }
  99. }
  100. }
  101. return values
  102. }
  103. }