TIMEncryptedStorage
是一个独立的框架,专为 Trifork Identity Manager 设计,用作加密存储处理器。
此框架处理与 Trifork Identity Manager KeyService 的通信,并从 iOS Keychain 存储/获取加密/解密的数据。此外,它还处理通过来自密钥服务的长密钥对数据的生物识别访问。
它是 TIM-iOS 软件包的关键部分,该软件包是功能完善的 Trifork Identity Manager 框架,它还处理 OpenID Connect 操作、访问令牌和刷新令牌。
将此 repo 添加到你的 SPM 📦
https://github.com/trifork/TIMEncryptedStorage-iOS
TIMEncryptedStorage
依赖于安全存储和密钥服务实例。默认的配置方式如下例所示。
import TIMEncryptedStorage // Required for TIMKeyServiceConfiguration
let config = TIMKeyServiceConfiguration(
realmBaseUrl: "<TIM Keyservice URL>",
version: .v1
)
let encryptedStorage = TIMEncryptedStorage(
secureStorage: TIMKeychain(),
keyService: TIMKeyService(configuration: config),
encryptionMethod: .aesGcm
)
您可能希望为了测试目的而实现您自己版本的 SecureStorage
和 TIMKeyServiceProtocol
协议。
以下示例使用 TIMEncryptedStorage
的 Combine
接口,该接口返回 Future
类。如果您正在开发部署目标低于 iOS 13 的应用程序,则存在相同的带有完成闭包的接口(尽管这些接口从 iOS 13 开始已被弃用)。
// Store data encrypted for the first time with a new secret "1234"
let myRawData = Data("someData".utf8)
encryptedStorage.storeWithNewKey(id: "my-id", data: myRawData, secret: "1234")
.sink { (_) in } receiveValue: { (result) in
print("Key created with id: \(result.keyId)")
print("Key created with longSecret: \(result.longSecret)")
}
.store(in: &myStore)
注意: 您不必为您保存的每个项目都创建新密钥。一旦创建,同一个密钥可以用于加密多种类型的数据(如果您允许相同的密钥来解锁它)。使用 TIMEncryptedStorage.store(id:data:keyId:secret:)
来实现这一点。
let keyId = "<keyId from store with newKey>"
encryptedStorage.get(id: "my-id", keyId: keyId, secret: "1234")
.sink { (_) in } receiveValue: { (data) in
let string = String(data: data, encoding: .utf8)
print("Loaded data from \(keyId): \(string)")
}
.store(in: &myStore)