Generate MD5 hash from Objective-C object

10,059

To generate a MD5 hash for an NSObject or a subclass of NSObject, you need to convert it into something that's easily hashable but still represents the state of the instance. A JSON string is one such option. The code looks like this:

Model.h

#import <Foundation/Foundation.h>

@interface Model : NSObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * type;
@property (nonatomic, retain) NSString * unit;
@property (nonatomic, retain) NSArray * fields;

- (NSString *)md5Hash;

@end

Model.m

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

@implementation Model

- (NSString *)md5Hash
{
    // Serialize this Model instance as a JSON string
    NSDictionary *map = @{ @"name": self.name, @"type": self.type,
                           @"unit": self.unit, @"fields": self.fields };

    NSError *error = NULL;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:map
                                                        options:NSJSONWritingPrettyPrinted
                                                          error:&error];
    if (error != nil) {
        NSLog(@"Serialization Error: %@", error);
        return nil;
    }

    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    // Now create the MD5 hashs
    const char *ptr = [jsonString UTF8String];
    unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];

    CC_MD5(ptr, strlen(ptr), md5Buffer);

    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x",md5Buffer[i]];

    return output;
}

@end

Then you can easily retrieve the MD5 hash just by calling the md5Hash method

Model *obj = [Model new];
obj.name = @"...";
obj.type = @"...";
obj.unit = @"...";
obj.fields = @[ ... ];

NSString *hashValue = [obj md5Hash];
Share:
10,059
ebi
Author by

ebi

Updated on June 04, 2022

Comments

  • ebi
    ebi about 2 years

    I'd like to generate an MD5 hash for an NSObject:

    @property (nonatomic, retain) NSString * name;
    @property (nonatomic, retain) NSString * type;
    @property (nonatomic, retain) NSString * unit;
    @property (nonatomic, retain) NSArray * fields;
    

    What is the best way to do so? I've seen examples for hashing from a dictionary or an array, but not from an entire NSObject.

    • Admin
      Admin over 10 years
      dupe1 dupe2 dupe3 dupe4 don't you people even attempt to use Google or the site search! That'd be blasphemy!
    • Abizern
      Abizern over 10 years
      @H2CO3 What do you mean by "you people"? And this question isn't a duplicate as it's about hashing a custom object rather than a string or an array or a dictionary. Yes it's similar, but it's worth answering.
    • Admin
      Admin over 10 years
      @Abizern OP and authors of other dupes. Still not a good enough reason to post the identical question. Or is it?
    • ebi
      ebi over 10 years
      @H2CO3 I saw those other posts already and posted my questions since those are all asking about hashing a string, I did not find any other questions about hashing an NSObject subclass, on s.o. or through google. Next time maybe you should read a bit before jumping the gun.
  • ebi
    ebi over 10 years
    Thanks, this is exactly what I was looking for.
  • ebi
    ebi over 10 years
    Is there any way to do this with nested objects? I tried doing this with my objects, but they all have object properties: property (nonatomic, retain) NSString * name; property (nonatomic, retain) MyRelatedObject * related;