• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
problem with applying md5 to data
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

problem with applying md5 to data


  • Subject: problem with applying md5 to data
  • From: Wilker <email@hidden>
  • Date: Wed, 29 Jun 2011 04:02:05 -0300

Hi guys,

I need to generate a hash from a file, the hash algorithm is here:
http://thesubdb.com/api/

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

This is my current implementation:

//
//  SMSubdbSource.m
//  Subtitle Master
//
//  Created by Wilker LĂșcio da Silva on 6/23/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SMSubdbSource.h"
#import <CommonCrypto/CommonDigest.h>

@implementation SMSubdbSource

+(NSString *)md5:(NSString *)str {
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, (unsigned int) strlen(cStr), result);
    return [NSString stringWithFormat:

@"XXXXXXXXXXXXXXXX",
            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]
            ];
}

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

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

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

@end

It works well if file only contains ASCII simple chars, but the real files
(that contains video binary data) generates wrong hash... I mean the problem
should be when trying to generate the string and MD5 digest from it...

How I can make it work correctly?

---
Wilker LĂșcio
http://about.me/wilkerlucio/bio
Kajabi Consultant
+55 81 82556600
_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:

This email sent to email@hidden

  • Follow-Ups:
    • Re: problem with applying md5 to data
      • From: Quincey Morris <email@hidden>
  • Prev by Date: how to get viewable text position after scroll a UITextView
  • Next by Date: NSDocument new file logic
  • Previous by thread: how to get viewable text position after scroll a UITextView
  • Next by thread: Re: problem with applying md5 to data
  • Index(es):
    • Date
    • Thread