Thanks for the answer Quincey,

I did resolved just before you post here :)

Used this (now in separated files):

+(NSString *)md5HexDigestFromChar:(const void *)str
withLength:(CC_LONG)lenght {
    unsigned char result[CC_MD5_DIGEST_LENGTH];

    CC_MD5(str, lenght, result);

    NSMutableString *ret = [NSMutableString
stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [ret appendFormat:@"%02x", result[i]];
    }

    return [ret lowercaseString];
}

+(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];

    return [SMEncodingUtilities md5HexDigestFromChar:[fileData bytes]
withLength:(unsigned int)[fileData length]];
}

You said the way I readed the file is bad... But I don't need to read the
entire file, just 64kb of first, and 64kb of last, are you sure that will be
better to read it entirely?

Thanks for the support :)
---
Wilker LĂșcio
http://about.me/wilkerlucio/bio
Kajabi Consultant
+55 81 82556600



On Wed, Jun 29, 2011 at 4:27 AM, Quincey Morris <quinceymor...@earthlink.net
> wrote:

> 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