¿Generar SHA1 (de imagen) en iPhone? (Resultados incorrectos)

I have looked everywhere and cannot find a solution to my problem. I have written a program for Android and Windows both that calculates the SHA1 hash of a given image. I confirmed these hashes with HashCalc that I downloaded. However when I try to do the same for iPhone I get a completely different hash. I believe it has something to do with converting the image to a byte array before I compute the hash. Here is my code:

- (void)imagePickerCotroller:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageview setImage:image];

NSData* imageData = UIImageJPEGRepresentation(image, 1);
NSString *hash = makeSHA1(imageData);

[filepath setText: hash];
[self dismissModalViewControllerAnimated:YES];

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

[self dismissModalViewControllerAnimated:YES];

NSString* makeSHA1(NSData *data) {       

uint8_t digest[CC_SHA1_DIGEST_LENGTH];

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

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

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

preguntado el 09 de marzo de 12 a las 22:03

1 Respuestas

I suspect the problem here is that you're converting the data using UIImageJPEGRepresentation(). Even if you started with JPEG data, there isn't a promise that this returns exactly the same bytes as you started with.

My (untested) suggestion is that you ask the CGDataProvider for the raw data. Something like this:

CGImageRef cgImage = [image CGImage];
CGDataProviderRef provider = CGImageGetDataProvider(cgImage);
CFDataRef imageData = CGDataProviderCopyData(provider);
if (imageData != NULL) {

respondido 12 mar '12, 14:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.