Keychain Duplicate Item When Adding Password

本文探讨了在iOS开发中使用Keychain服务时遇到的一个常见问题:尝试添加第二个通用密码条目时出现的错误-25299(重复项)。文章详细解释了问题的原因在于未正确设置kSecAttrAccount和kSecAttrService属性,并提供了修正后的示例代码。

Keychain Duplicate Item When Adding Password

Apr 28th, 2010

I thought I was done with Keychain access on the iPhone but I hit a problem which made me realise there was still a gap in my knowledge (and in the keychain documentation). The problem I was seeing came when trying to add a second password entry of class kSecClassGenericPassword. A search for the entry was not finding anything but when I tried to add the new entry using SecItemAdd I was getting the error errSecDuplicateItem (-25299) indicating that the item already exists.

When adding an entry to the keychain I was only including the kSecAttrGeneric attribute as an identifier and relying on the value of this identifier when retrieving the entry from the keychain. The problems is that for a keychain entry of class kSecClassGenericPassword the attributes that are used to determine a unique entry are kSecAttrAccount and kSecAttrService. Since I was not including these entries my second password was not considered to be unique. The fact that it had a different value for kSecAttrGeneric did not matter as that is not used to determine uniqueness. Of course you can search on all attributes which explains why the search was coming up empty.

In database terms you could think of their being a unique index on the two attributes kSecAttrAccount, kSecAttrService requiring the combination of those two attributes to be unique for each entry in the keychain.

Unfortunately the problem with the keychain services is that these finer points are very poorly documented. There is a passing reference to the required attributes in the Keychain Services Programming Guide, in the section Keychain Services Ease of Use (my emphasis):

To create a keychain item and add it to a keychain in Mac OS X, for example, you call one of two functions, depending on whether you want to add an Internet password or some other type of password: SecKeychainAddInternetPassword or SecKeychainAddGenericPassword. In your function call, you pass only those attributes for which there is no obvious default value. For example, you must specify the name of the service and the user’s account name, but you do not have to specify the creation date and creator, because the function can figure those out by itself. You also pass the data (usually a password) that you want to store in the keychain.

You have to be paying pretty close attention to spot that. The best reference I could find was on an Apple CDSA mailing list which put me on the right track but as far as I know the key attributes for each keychain class are not included in any Apple documentation that I have been able to find. I will go back and fix the example code in the previous post but the important piece is when building the dictionary used when searching or adding to the keychain:

static NSString *serviceName = @"com.mycompany.myAppServiceName";

- (NSMutableDictionary *)newSearchDictionary:(NSString *)identifier {
  NSMutableDictionary *searchDictionary = [[NSMutableDictionary alloc] init];  

  [searchDictionary setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];

  NSData *encodedIdentifier = [identifier dataUsingEncoding:NSUTF8StringEncoding];
  [searchDictionary setObject:encodedIdentifier forKey:(id)kSecAttrGeneric];
  [searchDictionary setObject:encodedIdentifier forKey:(id)kSecAttrAccount];
  [searchDictionary setObject:serviceName forKey:(id)kSecAttrService];

  return searchDictionary; 
}

In this example I have just used a fixed service name and set the account name to the identifier which I expect to be unique for each password stored.


下载代码方式:https://pan.quark.cn/s/957405011bdf 在计算机视觉技术中,轮廓提取与中心识别被视为两项核心的技术,它们对于图像处理及模式识别领域扮演着不可或缺的角色。本文将深入剖析这两种算法,并围绕"轮廓提取(中心识别)算法"这一核心主题,同时结合所提供的压缩包文件"Contour",对相关知识点进行详尽的阐述。 轮廓提取算法,主要功能在于识别并分离图像中的不同对象。该算法通过探测物体边缘,构建出明确的边界线,从而实现图像内部各组成部分的区分。在多色位图环境下,不同的色彩可能象征着不同的对象或区域,因此,一个性能优越的轮廓提取算法应当具备处理此类复杂场景的能力。常见的轮廓提取算法包括Canny边缘检测、Sobel算子、Laplacian算子和Hough变换等。这些算法各自具备独特的优缺点,究竟选择何种方法,需要依据具体的应用情境以及性能要求来决定。 1. Canny边缘检测:由John F. Canny所研发,这是一种自适应的多级边缘检测方法。Canny算法借助高斯滤波器来降低噪声干扰,随后运用强度梯度和非极大值抑制技术来定位最显著的边缘,最终通过双阈值检测来区分边缘与噪声。 2. Sobel算子:Sobel算子是一种基于梯度的边缘检测工具,它通过计算图像在水平和垂直方向上的梯度来探测边缘。此方法操作简便且效率较高,但可能对图像中的噪声较为敏感。 3. Laplacian算子:Laplacian算子是一种二阶导数算子,能够迅速识别图像中的尖峰(即边缘)。然而,它容易受到噪声的影响,因此通常需要与其他技术结合使用,例如Gaussian滤波。 4. Hough变换:Hough变换是一种在参数空间中进行边缘检测的方法,它能够检测出任意...
【重要提示】本资源设置为0积分下载,若非0积分请勿轻易下载 亲爱的CSDN用户: 首先感谢你点进这个资源页面。我需要提前说明一个重要情况: **本资源原本已设置为“0积分下载”**,即作者希望完全免费共享。但CSDN平台有时会根据文件的下载热度、文件大小、用户权限等因素,**自动将部分资源的积分调整为非0数值**(如1积分、2积分、5积分等)。这是平台系统的自动行为,而非作者本人的设定。 **因此,如果你当前看到该资源的下载所需积分不是0(例如显示为1、2、3……),请谨慎决定是否下载。** 如果你按照非0积分支付并下载后发现资源内容不符合预期、链接失效,或者实际上该资源本应是免费的,作者无法为此承担积分损失或退还操作。**强烈建议:仅在页面显示为0积分时进行下载。** 另外,本资源描述中**并未直接提供具体的下载地址或外部链接**,因为它本身是一个通过CSDN官方上传通道提交的文件/内容包。如果你看到描述中没有外部网盘地址,这是正常的——资源文件应通过CSDN内置的“下载”按钮获取。若因平台积分显示异常导致你支付了积分,请优先联系CSDN客服咨询积分退还政策,作者没有权限修改平台自动设定的积分值。 感谢你的理解与支持。技术分享本应开放,但受限于平台规则,特此提醒如上。祝学习进步!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值