I have this code:
NSMutableData *derivedKey = [NSMutableData dataWithLength:32];
// code missing..fill somehow the derivedKey with 32 random bytes
// This line doesn't crash
NSData *iv = [derivedKey subdataWithRange:NSMakeRange(0, 32)];
..
// This line crashes
NSData *iv = [derivedKey subdataWithRange:NSMakeRange(16, 32)];
Any suggestion why this happens ?
It seems that somehow only the whole range from 0 – 32 passes
I wanna simple a new NSData variable which contains only the second half of the bytes
It crashes, because the second parameter of NSRangeMake is length of the range. So what you are trying to do is to take 32 bytes starting with offset 16, which exceeds data size (final byte would be 48 in order).
So simply, change it to:
Check ref:
http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSMakeRange