123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- //
- // DSignatureManager.swift
- // PDF Reader Pro Edition
- //
- // Created by Niehaoyu on 2023/9/28.
- //
- import Cocoa
- class DSignatureManager: NSObject {
-
- public static let manager = DSignatureManager.init()
-
- lazy var signatures: NSArray = []
-
- override init() {
- super.init()
-
- self.loadAllKeyChainCertificates()
- // self.getAllKeychainItems()
-
- }
-
- func loadAllKeyChainCertificates () {
- let keychainQuery: [String: Any] = [
- kSecClass as String : kSecClassIdentity,
- kSecReturnData as String : true,
- kSecReturnAttributes as String : true,
- kSecReturnRef as String : true,
- kSecMatchLimit as String: kSecMatchLimitAll,
- kSecReturnPersistentRef as String: true
- ]
-
-
- var results: AnyObject?
- // let status = SecItemCopyMatching(keychainQuery as CFDictionary, &results)
-
- let status = withUnsafeMutablePointer(to: &results) {
- SecItemCopyMatching(keychainQuery as CFDictionary, UnsafeMutablePointer($0))
- }
-
- var resultArr = results as! NSArray;
- for result in resultArr {
- let tResult = result as![String:Any]
- let identify = tResult[kSecValueRef as String] as! AnyObject
-
- var certificate: SecCertificate?
- // let status = SecIdentityCopyCertificate(identify, &certificate)
- // if (status == errSecSuccess) {
-
- let label = tResult[kSecAttrLabel as String] as? String
-
- let account = tResult[kSecAttrAccount as String] as? String
-
- guard let Issuer = tResult[kSecAttrIssuer as String] as? Data else { return }
- let IssuerKey = String(data: Issuer, encoding: .utf8)
-
- let valueReflabel = tResult[kSecValueRef as String] as? String
-
- guard let data = tResult[kSecValueData as String] as? Data else {
- return
- }
- let dataKey = String(data: data, encoding: .utf8)
-
- if let key = tResult[kSecAttrLabel as String] as? String,
- let value = tResult[kSecValueData as String] as? Data {
- print(value)
- }
- else if let key = tResult[kSecAttrLabel as String] as? String,
- let value = tResult[kSecValueRef as String] {
- print(value)
-
- }
-
- print("adsf")
- // {
- // values[key] = String(data: value, encoding:.utf8) as AnyObject?
- // }
- // }
- // SecIdentityCopyCertificate(identify, &certificate)
- }
-
- }
-
- func getAllKeychainItems() {
-
- let classes = [kSecClassGenericPassword as String, // Generic password items
- kSecClassInternetPassword as String, // Internet password items
- kSecClassCertificate as String, // Certificate items
- kSecClassKey as String, // Cryptographic key items
- kSecClassIdentity as String] // Identity items
-
-
- // classes.forEach { secClass in
- let items = getAllKeyChainItemsOfClass( kSecClassIdentity as String )
- NSLog(items.description)
- // }
- }
-
- func getAllKeyChainItemsOfClass(_ secClass: String) -> [String: AnyObject] {
-
- let query: [String: Any] = [
- kSecClass as String : secClass,
- kSecReturnData as String : true,
- kSecReturnAttributes as String : true,
- kSecReturnRef as String : true,
- kSecMatchLimit as String: kSecMatchLimitAll,
- ]
-
- var result: AnyObject?
-
- let lastResultCode = withUnsafeMutablePointer(to: &result) {
- SecItemCopyMatching(query as CFDictionary, UnsafeMutablePointer($0))
- }
-
- var values = [String: AnyObject]()
- if lastResultCode == noErr {
- let array = result as? Array<Dictionary<String, Any>>
-
- for item in array! {
-
- let value = item[kSecAttrLabel as String] as? String
-
- if let key = item[kSecAttrAccount as String] as? String,
- let value = item[kSecValueData as String] as? Data {
- values[key] = String(data: value, encoding:.utf8) as AnyObject?
- }
- else if let key = item[kSecAttrLabel as String] as? String,
- let value = item[kSecValueRef as String] {
- print(value)
- values[key] = value as AnyObject
- }
-
- }
- }
- return values
- }
-
- }
|