9

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;

}
Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
Cyprian
  • 9,423
  • 4
  • 39
  • 73
  • possible duplicate of [Objective-C Implicit conversion loses integer precision (size\_t to CC\_Long)](http://stackoverflow.com/questions/17013838/objective-c-implicit-conversion-loses-integer-precision-size-t-to-cc-long) – jbouaziz Mar 13 '14 at 20:36

1 Answers1

15

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);
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382