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;
}
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With