mirror of
https://github.com/nalinbhardwaj/Vespass.git
synced 2026-01-14 00:07:56 -05:00
63 lines
1.7 KiB
Swift
63 lines
1.7 KiB
Swift
//
|
|
// KeychainStore.swift
|
|
// Vespass
|
|
//
|
|
// Created by Nalin Bhardwaj on 22/12/22.
|
|
// Copyright © 2022 Vespass. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import Security
|
|
|
|
let KEYCHAIN_STORE_PREFIX = "com.nibnalin.vespass.keychain.demo.2."
|
|
|
|
class KeyChain {
|
|
class func save(key: String, data: Data) -> OSStatus {
|
|
let query = [
|
|
kSecClass as String : kSecClassGenericPassword as String,
|
|
kSecAttrAccount as String : KEYCHAIN_STORE_PREFIX + key,
|
|
kSecValueData as String : data ] as [String : Any]
|
|
|
|
SecItemDelete(query as CFDictionary)
|
|
|
|
return SecItemAdd(query as CFDictionary, nil)
|
|
}
|
|
|
|
class func load(key: String) -> Data? {
|
|
let query = [
|
|
kSecClass as String : kSecClassGenericPassword,
|
|
kSecAttrAccount as String : KEYCHAIN_STORE_PREFIX + key,
|
|
kSecReturnData as String : kCFBooleanTrue!,
|
|
kSecMatchLimit as String : kSecMatchLimitOne ] as [String : Any]
|
|
|
|
var dataTypeRef: AnyObject? = nil
|
|
|
|
let status: OSStatus = SecItemCopyMatching(query as CFDictionary, &dataTypeRef)
|
|
|
|
if status == noErr {
|
|
return dataTypeRef as! Data?
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
class func createUniqueID() -> String {
|
|
let uuid: CFUUID = CFUUIDCreate(nil)
|
|
let cfStr: CFString = CFUUIDCreateString(nil, uuid)
|
|
|
|
let swiftString: String = cfStr as String
|
|
return swiftString
|
|
}
|
|
}
|
|
|
|
extension Data {
|
|
init<T>(from value: T) {
|
|
var value = value
|
|
self.init(buffer: UnsafeBufferPointer(start: &value, count: 1))
|
|
}
|
|
|
|
func to<T>(type: T.Type) -> T {
|
|
return self.withUnsafeBytes { $0.load(as: T.self) }
|
|
}
|
|
}
|