| 1 | /* |
| 2 | * FlickrCategory.h |
| 3 | * ---------------- |
| 4 | * FlickrCategory - extending the built-in NSData, NSString, and NSDictionary classes with methods that make them more useful for interacting with the Flickr web services. |
| 5 | * |
| 6 | * Author: Chris Lee <clee@mg8.org> |
| 7 | * License: GPL v2 <http://www.opensource.org/licenses/gpl-license.php> |
| 8 | */ |
| 9 | #import "FlickrCategory.h" |
| 10 | |
| 11 | @implementation NSData (Flickr) |
| 12 | - (NSString *)md5HexHash |
| 13 | { |
| 14 | unsigned char digest[16]; |
| 15 | char finalDigest[32]; |
| 16 | |
| 17 | MD5([self bytes], [self length], digest); |
| 18 | for (unsigned short int i = 0; i < 16; i++) |
| 19 | sprintf(finalDigest + (i * 2), "%02x", digest[i]); |
| 20 | |
| 21 | return [NSString stringWithCString: finalDigest length: 32]; |
| 22 | } |
| 23 | @end |
| 24 | |
| 25 | @implementation NSString (Flickr) |
| 26 | - (NSString *)md5HexHash |
| 27 | { |
| 28 | return [[self dataUsingEncoding: NSUTF8StringEncoding allowLossyConversion: NO] md5HexHash]; |
| 29 | } |
| 30 | @end |
| 31 | |
| 32 | @implementation NSDictionary (Flickr) |
| 33 | - (NSArray *)pairsJoinedByString: (NSString *)j |
| 34 | { |
| 35 | NSArray *sortedKeys = [[self allKeys] sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)]; |
| 36 | NSMutableArray *allKeysAndObjects = [NSMutableArray array]; |
| 37 | |
| 38 | for (unsigned int i = 0; i < [sortedKeys count]; i++) { |
| 39 | NSString *key = [sortedKeys objectAtIndex: i]; |
| 40 | NSString *val = [self objectForKey: key]; |
| 41 | [allKeysAndObjects addObject: [NSString stringWithFormat: @"%@%@%@", key, j, val]]; |
| 42 | } |
| 43 | |
| 44 | return [NSArray arrayWithArray: allKeysAndObjects]; |
| 45 | } |
| 46 | @end |
| 47 | |
| 48 | |