forked from alexruperez/SecurePropertyStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SingletonStorage.swift
29 lines (22 loc) · 956 Bytes
/
SingletonStorage.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import CryptoKit
import Foundation
import Keychain
import Storage
open class SingletonStorage: DelegatedStorage {
open class var standard: SingletonStorage { shared }
private static let shared = SingletonStorage()
public convenience init(_ delegate: StorageDelegate = SingletonStorageDelegate(),
authenticationTag: Data? = nil) {
self.init(delegate,
symmetricKey: SymmetricKey.generate(),
nonce: AES.GCM.Nonce.generate(),
authenticationTag: authenticationTag)
}
}
open class SingletonStorageDelegate: StorageDelegate {
private var shared = [AnyHashable: Any]()
public init() {}
open func data<D: StorageData>(forKey key: StoreKey) -> D? { shared[key] as? D }
open func set<D: StorageData>(_ data: D?, forKey key: StoreKey) { shared[key] = data }
open func remove(forKey key: StoreKey) { shared.removeValue(forKey: key) }
}