I have the following Objective-C function:
+(NSString *)stringToSha1:(NSString *)str{
NSMutableData *dataToHash = [[NSMutableData alloc] init];
[data
Short answer: turn on gcc warnings (-Wall).
Long answer:
NSMutableData *dataToHash = [[NSMutableData alloc] init];
[dataToHash appendData:[str dataUsingEncoding:NSUTF8StringEncoding]];
is broken: You try to use a C string where an NSData argument is expected. Use
NSMutableData *dataToHash = [str dataUsingEncoding:NSUTF8StringEncoding];
instead.
The rest of the method takes the SHA1 buffer and tries interpret this data as an UTF-8 C string, which might crash or give any unexpected result. First, the buffer is not a UTF-8 string. Secondly, it's not null terminated.
What you want is to convert the SHA1 to a base 64 or similar string. Here's a nice post about how to do that.