Skip to content

Commit

Permalink
Adding SHA1 hashing for file keys instead of NSString's -hash method.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucianoks committed Sep 9, 2014
1 parent f3063f2 commit 6758f17
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion JMImageCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
return [url absoluteString];
}
static inline NSString *cachePathForKey(NSString *key) {
NSString *fileName = [NSString stringWithFormat:@"JMImageCache-%u", [key hash]];
NSString *fileName = [NSString stringWithFormat:@"JMImageCache-%@", [JMImageCache SHA1FromString:key]];
return [JMImageCacheDirectory() stringByAppendingPathComponent:fileName];
}

Expand Down Expand Up @@ -262,4 +262,25 @@ - (void) performDiskWriteOperation:(NSInvocation *)invoction {
[self.diskOperationQueue addOperation:operation];
}

#pragma mark - Hash methods

+ (NSString *)SHA1FromString:(NSString *)string
{
unsigned char digest[CC_SHA1_DIGEST_LENGTH];

NSData *stringBytes = [string dataUsingEncoding:NSUTF8StringEncoding];

if (CC_SHA1([stringBytes bytes], (CC_LONG)[stringBytes length], digest)) {

NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];

for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
[output appendFormat:@"%02x", digest[i]];
}

return output;
}
return nil;
}

@end

0 comments on commit 6758f17

Please sign in to comment.