Michael Ash wrote:

@implementation NSDictionary (SHA1Hashing)
- (NSData *)gc_sha1hash {
    NSMutableData *subhashes = [NSMutableData data];
    for(id key in [[self allKeys]
sortedArrayUsingSelector:@selector(compare:)]) {
        [subhashes appendData:[key gc_sha1hash]];
        [subhashes appendData:[[self objectForKey:key] gc_sha1hash]];
    }
    return [subhashes gc_sha1hash];
}
@end

Then define -gc_sha1hash for all the other classes you expect to
encounter as well. (The prefix is just to ensure that you don't
overwrite a hypothetical Apple implementation of a -sha1hash method.)


As a design, it might be more flexible to define a -gc_canonicalData method that returns an NSData containing unhashed canonical bytes, rather than -gc_sha1hash that returns a hash. This would make it easier to use different secure-hash algorithms, should that be desired. SHA1 isn't exactly state-of-the-art.

I'd also make the code that calculates the hash from a given NSDictionary or NSArray return an error (or throw an exception) if anything in the collection doesn't respond to -gc_canonicalData. Or it could silently skip those (not hash them), depending on the context.

  -- GG

_______________________________________________

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