Skip to content

Commit 6758f17

Browse files
committed
Adding SHA1 hashing for file keys instead of NSString's -hash method.
1 parent f3063f2 commit 6758f17

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

JMImageCache.m

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
return [url absoluteString];
2323
}
2424
static inline NSString *cachePathForKey(NSString *key) {
25-
NSString *fileName = [NSString stringWithFormat:@"JMImageCache-%u", [key hash]];
25+
NSString *fileName = [NSString stringWithFormat:@"JMImageCache-%@", [JMImageCache SHA1FromString:key]];
2626
return [JMImageCacheDirectory() stringByAppendingPathComponent:fileName];
2727
}
2828

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

265+
#pragma mark - Hash methods
266+
267+
+ (NSString *)SHA1FromString:(NSString *)string
268+
{
269+
unsigned char digest[CC_SHA1_DIGEST_LENGTH];
270+
271+
NSData *stringBytes = [string dataUsingEncoding:NSUTF8StringEncoding];
272+
273+
if (CC_SHA1([stringBytes bytes], (CC_LONG)[stringBytes length], digest)) {
274+
275+
NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
276+
277+
for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
278+
[output appendFormat:@"%02x", digest[i]];
279+
}
280+
281+
return output;
282+
}
283+
return nil;
284+
}
285+
265286
@end

0 commit comments

Comments
 (0)