Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert/use CC_SHA1 method so that it can be used in a 64 bit system

I am converting my app to work on a 64 bit system and I got stuck on trying to properly use a built in CC_SHA1 method.

I get the warning:

Implicit conversion loses integer precision: ‘NSUInteger’ (aka ‘unsigned long’) to ‘CC_LONG’ (aka ‘unsigned int’)

when trying to pass: data.length in CC_SHA1 method.

data.length is NSUInteger

CC_SHA1 method definition is:

extern unsigned char *CC_SHA1(const void *data, CC_LONG len, unsigned char *md)

where CC_LONG is a 32 bit unsigned integer.

How can I change it to use CC_LONG64 instead?

typedef uint64_t CC_LONG64;

This is the conversion method where I get the above warning:

+(NSString*)sha1:(NSString*)input
{
    const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:input.length];

    uint8_t digest[CC_SHA1_DIGEST_LENGTH];

    CC_SHA1(data.bytes, data.length, digest);

    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];

    for(NSInteger i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];

    return output;

}
like image 594
Cyprian Avatar asked Dec 20 '13 23:12

Cyprian


1 Answers

Assuming that the data length is less than 2^32, you can add an explicit cast without losing any information. This should remove the warning:

CC_SHA1(data.bytes, (CC_LONG)data.length, digest);
like image 118
Martin R Avatar answered Oct 02 '22 14:10

Martin R