0

So I'm using the random string generation solution from this question.

Then I'm saving the result to the keychain, except that I get EXC_BAD_ACCESS(code=EXC_I386_GPFLT), and the result variable containing OSStatus is 32767.

What am I doing wrong? I have tried running strlen() on both characters and generatedKey - it always returns some ridiculous numbers, as well as running simple [characters length]. I believe there is the problem - it should return 256.

I know this is probably silly, but I'm lost and Google does not help.

int len = 256;

NSString *characters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
NSMutableString *generatedKey = [NSMutableString stringWithCapacity: len];
for (int i=0; i<len; i++) {
    [generatedKey appendFormat: @"%C", [characters characterAtIndex: arc4random_uniform([characters length]) % [characters length]]];
}

const char *encryptedPass = [generatedKey UTF8String];
OSStatus result = SecKeychainItemCreateFromContent(kSecGenericPasswordItemClass, &list, len, encryptedPass, someKeychain, accessRef, &someKeychainItem);
Community
  • 1
  • 1
Michał Siwek
  • 794
  • 1
  • 10
  • 25
  • you shouldn't use `strlen` here. also what is the value of `[characters length]`? and you really should use `arc4random_buf` to fill buffer with random bytes – Bryan Chen Jun 20 '14 at 14:46
  • `[characters length]` returns `140735225418784`. I'll take a look at `arc4random_buf` in a minute, thanks. Anyway, problem is still here. – Michał Siwek Jun 20 '14 at 14:58
  • Why are you doing `arc4random_uniform([characters length]) % [characters length]`? The division will never change the answer you already got from `arc4random_uniform()`. – jscs Jun 20 '14 at 19:35
  • 1
    Your string creation code works as expected here. There must be a problem with the other arguments to your call to `SecKeychainItemCreateFromContent()` – jscs Jun 20 '14 at 19:37
  • You're right, there was a problem with one of the attributes in the list, thanks for pointing it out. I'm still curious how to obtain the length of the generatedKey, however. – Michał Siwek Jun 20 '14 at 23:04
  • 1
    @MichałSiwek - You find the length of an `NSMutableString` the same way as for its superclass `NSString`, and you're already doing the latter... – CRD Jun 21 '14 at 04:21

0 Answers0