ios - kSecAttrAccount 和 kSecAttrGeneric

标签 ios swift security keychain

我想在帐户 marta@domain.com 中保存两个值。字典示例:

  • 值 1。

kSecAttrAccount = marta@domain.com

kSecAttrGeneric = value1Key

kSecValueData = value1

  • 值(value) 2。

kSecAttrAccount = marta@domain.com

kSecAttrGeneric = value2Key

kSecValueData = value2


这可能吗?当我尝试保留第二个值时,我在执行查询时遇到错误。

附上我的代码片段:

func addCredentials(_ credentials: KeychainCredentials) throws {

let account = credentials.account
let password = credentials.password.data(using: String.Encoding.utf8)!
let generic = credentials.generic

 let accessControl = SecAccessControlCreateWithFlags(kCFAllocatorDefault, 
            kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly,
            .userPresence,
            nil) 

 // Build the query
 let query: [String: Any] = [kSecClass as String: kSecClassGenericPassword,
                                    kSecAttrService as String: serviceName,
                                    kSecAttrAccount as String: account,
                                    kSecAttrGeneric as String: generic,
                                    kSecAttrAccessControl as String: accessControl as Any,
                                    kSecValueData as String: password]

let status = SecItemAdd(query as CFDictionary, nil)
guard status == errSecSuccess else { throw KeychainError(status: status) }

}

最佳答案

对于kSecClassGenericPassword,服务和账号的组合必须是唯一的。有关哪些属性是主键的一部分的列表,请参阅 errSecDuplicateItem 的文档.

The system considers an item to be a duplicate for a given keychain when that keychain already has an item of the same class with the same set of composite primary keys. Each class of keychain item has a different set of primary keys, although a few attributes are used in common across all classes. In particular, where applicable, kSecAttrSynchronizable and kSecAttrAccessGroup are part of the set of primary keys. The additional per-class primary keys are listed below:

For generic passwords, the primary keys include kSecAttrAccount and kSecAttrService.

For internet passwords, the primary keys include kSecAttrAccount, kSecAttrSecurityDomain, kSecAttrServer, kSecAttrProtocol, kSecAttrAuthenticationType, kSecAttrPort, and kSecAttrPath.

For certificates, the primary keys include kSecAttrCertificateType, kSecAttrIssuer, and kSecAttrSerialNumber.

For key items, the primary keys include kSecAttrKeyClass, kSecAttrKeyType, kSecAttrApplicationLabel, kSecAttrApplicationTag, kSecAttrKeySizeInBits, and kSecAttrEffectiveKeySize.

For identity items, which are a certificate and a private key bundled together, the primary keys are the same as for a certificate. Because a private key may be certified more than once, the uniqueness of the certificate determines that of the identity.

如果我正在构建它,我可能会将所有键/值放入字典中,并将其序列化为单个 kSecAttrAccount 记录。

或者,您可以将您的帐户和服务值合并到帐户中(如果您有服务),并将 value1Key 放入服务属性中。然后帐户+服务对于给定的 key 将是唯一的。

关于ios - kSecAttrAccount 和 kSecAttrGeneric,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57920962/

相关文章:

ios - 启动我的应用程序时出现错误消息

ios - IOS 快捷方式上的正则表达式用于行匹配

ios - 如何发送[字符串:Any] dictionary in multipart API?

ios - 什么时候在 Swift 中使用@objc?

html - 如何防止浏览器存储密码

ios - 屏幕外信息的安全性如何?

ios - Hook 主页按钮按下

ios - 有没有办法动态实例化类类型

javascript - 反射型XSS : Why must attack payload be reflected to victim's browser?

ios - 如果新的被解雇,如何取消以前的 NSTimer