On Jun 29, 2011, at 00:02, Wilker wrote:

> I'm trying to implement it on Objective-C but I'm having really trouble on
> it...

There are many things wrong with your implementation.

> +(NSString *)generateHashFromPath:(NSString *)path {
>    const NSUInteger CHUNK_SIZE = 65536;
> 
>    NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:path];
> 
>    if (file == nil) return nil;
> 
>    unsigned long fileSize = [[[NSFileManager defaultManager]
> attributesOfItemAtPath:path error:nil] fileSize];
> 
>    NSMutableData *fileData = [[NSMutableData alloc]
> initWithCapacity:CHUNK_SIZE * 2];
> 
>    [fileData appendData:[file readDataOfLength:CHUNK_SIZE]];
>    [file seekToFileOffset:MAX(0, fileSize - CHUNK_SIZE)];
>    [fileData appendData:[file readDataOfLength:CHUNK_SIZE]];
> 
>    [file closeFile];

This is not the worst way to read the entire contents of a file into a NSData 
object, but it's pretty bad. :)

It would be far easier and more efficient to use [NSData 
dataWithContentsOfFile: path options: NSDataReadingMapped | 
NSDataReadingUncached error: NULL];

>    NSString *dataString = [[NSString alloc] initWithData:fileData
> encoding:NSASCIIStringEncoding];

You say the file contains binary data. Running it through the ASCII string 
encoding is going to throw away every character that isn't ASCII (i.e. it will 
throw away every character with its high order bit set). That isn't what you 
want.

Also, converting a large NSData object to a NSString via any encoding is going 
to be horrendously expensive (in CPU and memory cost). That isn't what you want.

>    return [[SMSubdbSource md5:dataString] lowercaseString];
> }

> +(NSString *)md5:(NSString *)str {
>    const char *cStr = [str UTF8String];

Since you're passing in a (NSString) string of ASCII characters, you're going 
to get out a (C) string of exactly the same ASCII characters, since ASCII 
characters are represented by themselves in UTF8. This isn't what you want.

>    unsigned char result[16];
>    CC_MD5(cStr, (unsigned int) strlen(cStr), result);
>    return [NSString stringWithFormat:
> 
> @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
>            result[0], result[1], result[2], result[3],
>            result[4], result[5], result[6], result[7],
>            result[8], result[9], result[10], result[11],
>            result[12], result[13], result[14], result[15]
>            ];
> }

If you're trying to compute the digest of the binary data, you can compute it 
directly from the NSData object like this:

        CC_MD5 (fileData.bytes, fileData.length, result);


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to