// // Keychain.swift // AT-IRC // // Minimal generic-password Keychain storage for the session blob. // import Foundation import Security enum Keychain { private static let service = "com.fedfork.irc.AT-IRC" static func set(_ data: Data, for key: String, accessible: CFString = kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly) { let query: [String: Any] = [ kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: service, kSecAttrAccount as String: key, ] SecItemDelete(query as CFDictionary) var attributes = query attributes[kSecValueData as String] = data attributes[kSecAttrAccessible as String] = accessible SecItemAdd(attributes as CFDictionary, nil) } static func get(_ key: String) -> Data? { let query: [String: Any] = [ kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: service, kSecAttrAccount as String: key, kSecReturnData as String: true, kSecMatchLimit as String: kSecMatchLimitOne, ] var result: CFTypeRef? guard SecItemCopyMatching(query as CFDictionary, &result) == errSecSuccess else { return nil } return result as? Data } static func delete(_ key: String) { let query: [String: Any] = [ kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: service, kSecAttrAccount as String: key, ] SecItemDelete(query as CFDictionary) } }