convert password from SecKeychainFindGenericPassword to NSString
convert password from SecKeychainFindGenericPassword to NSString
- Subject: convert password from SecKeychainFindGenericPassword to NSString
- From: Stuart Malin <email@hidden>
- Date: Sun, 31 Jan 2010 20:51:05 -0500
I acquire a password from a keychain using SecKeychainFindGenericPassword. That provides a non null terminated c string and a length. I had been using [NSString stringWithCString: length:] to get an NSString instance of the password. However, the +stringWithCString:length: method was deprecated way back, so I thought I'd clean up my code and use +stringWithCString:encoding: -- however that doesn't work because the supplied C string isn't null terminated. I did a bit of searching, and found in the archives on CocoaBuilder [1] someone's solution back in September of 2005, which was to copy the acquired password to a new buffer of 1 additional byte and create a new null terminated C string there:
buffer = (char *) malloc((passwordLength + 1) * sizeof(char));
strncpy(buffer, passwordData, passwordLength);
buffer[passwordLength] = 0;
password = [NSString stringWithUTF8String:buffer];
free(buffer);
Of course I would now change the NSString class method to +stringWithCString:encoding.
Is this the best way to go about converting the string returned by the Carbon keychain code to an NSString?
I am presuming I should stop using the deprecated method -- is that right? Or can I safely continue to use it?
TIA for your wise counsel.
[1] http://www.cocoabuilder.com/archive/cocoa/145676-seckeychainfindgenericpassword.html?q=SecKeychainFindGenericPassword#145681 (expand the 22:57 post by Ryan Britton)_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden