0

I've declared a string in my header file like so:

@property (nonatomic, retain) NSString *resultOfHash;

I call my getHash method like so:

 NSString *hash = [self getHash];

My getHash method is:

-(NSString *) getHash
{
//Get username form Keychain
KeychainItemWrapper *keyChain = [[KeychainItemWrapper alloc]    initWithIdentifier:KeyChainName accessGroup:nil];
username = [keyChain objectForKey:(__bridge id)kSecAttrAccount];

//get token from NSUserDefauls
NSString *token = [[NSUserDefaults standardUserDefaults]objectForKey:@"Token"];

NSString *toHash = [[username stringByAppendingString:HashExtra] stringByAppendingString:token];

const char *s = [toHash cStringUsingEncoding:NSASCIIStringEncoding];

NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];

uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};

CC_SHA512(keyData.bytes, keyData.length, digest);

NSData *out = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];

//convert to string
resultOfHash = [out description];
//App crashed out above

// get rid of unwanted characters
resultOfHash = [resultOfHash stringByReplacingOccurrencesOfString:@" " withString:@""];
resultOfHash = [resultOfHash stringByReplacingOccurrencesOfString:@"<" withString:@""];
resultOfHash = [resultOfHash stringByReplacingOccurrencesOfString:@">" withString:@""];

//log to make sure it works
NSLog(@"hash is: %@", resultOfHash);

return resultOfHash;
}

My code crashes out at the line: ResultOfHash = [out description]; but I'm not sure why.

When I use a local variable the conversion works fine but then I cannot return the local variable from the getHash method. Example:

Replace ResultOfHash = [out description];

with

NSString *local = [out description];
return local;

and the conversion works fine and when I debug line by line, the debugger will go to my closing bracket on my method and then produce the EXC_BAD_ACCESS error.

I've tried running NSZombie but that didn't find anything at all.

Any help in trying to sort this out would be greatly appreciated.

bneely
  • 9,083
  • 4
  • 38
  • 46

4 Answers4

1

Have a look at the answer in this question. Try converting to NSString using

[NSString *local = [[[NSString alloc] initWithData:out encoding:NSASCIIStringEncoding]; 

I haven't tested this code with this encoding, but it's similar to something I already use.

Update -

I corrected an error in the code above. I somehow left the method signiture out in a distracted copy and paste.

Community
  • 1
  • 1
Draco
  • 1,003
  • 1
  • 10
  • 14
  • Thanks for pointing this out Draco. I didn't know using the description part of NSData was wrong. I'll change that. – user1134552 Jan 09 '12 at 10:12
0

It's crashing because out is unretained. You should add retain:

resultOfHash = [[out description] retain];

or use retained property:

self.resultOfHash = [out description];

Check this, it should be work.

Tomasz Wojtkowiak
  • 4,910
  • 1
  • 28
  • 35
0

You probably need to use an NSMutableString.

axel22
  • 32,045
  • 9
  • 125
  • 137
Krishna K
  • 708
  • 3
  • 10
0

I think the problem is here:

uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
CC_SHA512(keyData.bytes, keyData.length, digest);
NSData *out = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];

You are using CC_SHA512, but only allocate array of size CC_SHA1_DIGEST_LENGTH, which is smaller and will lead to the buffer overrunning.

To correct this, you should use CC_SHA512_DIGEST_LENGTH instead.

tia
  • 9,518
  • 1
  • 30
  • 44
  • Spot on Tia. This code was originally setup for SHA512 and I converted to SHA1 but missed that line. When I changed it to SHA1 and changed resultsofHash to NSMutableString, the code worked. Thanks for that – user1134552 Jan 09 '12 at 10:09