Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -9,10 +9,11 @@ OFArray.m \ OFAutoreleasePool.m \ OFBlock.m \ OFConstantString.m \ OFDataArray.m \ + OFDataArray+Hashing.m \ OFDate.m \ OFDictionary.m \ OFExceptions.m \ OFFile.m \ OFHash.m \ ADDED src/OFDataArray+Hashing.h Index: src/OFDataArray+Hashing.h ================================================================== --- src/OFDataArray+Hashing.h +++ src/OFDataArray+Hashing.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "OFDataArray.h" + +@class OFString; + +#ifdef __cplusplus +extern "C" { +#endif +extern int _OFDataArray_Hashing_reference; +#ifdef __cplusplus +} +#endif + +/** + * The OFDataArray (Hashing) category provides methods to calculate hashes for + * data arrays. + */ +@interface OFDataArray (Hashing) +/** + * \return The MD5 hash of the data array as an autoreleased OFString + */ +- (OFString*)MD5Hash; + +/** + * \return The SHA1 hash of the data array as an autoreleased OFString + */ +- (OFString*)SHA1Hash; +@end ADDED src/OFDataArray+Hashing.m Index: src/OFDataArray+Hashing.m ================================================================== --- src/OFDataArray+Hashing.m +++ src/OFDataArray+Hashing.m @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#import "OFDataArray.h" +#import "OFString.h" +#import "OFMD5Hash.h" +#import "OFSHA1Hash.h" +#import "OFAutoreleasePool.h" + +int _OFDataArray_Hashing_reference; + +@implementation OFDataArray (Hashing) +- (OFString*)MD5Hash +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFMD5Hash *hash = [OFMD5Hash MD5Hash]; + uint8_t *digest; + char ret_c[OF_MD5_DIGEST_SIZE * 2]; + size_t i; + + [hash updateWithBuffer: data + ofSize: count * itemSize]; + digest = [hash digest]; + + for (i = 0; i < OF_MD5_DIGEST_SIZE; i++) { + uint8_t high, low; + + high = digest[i] >> 4; + low = digest[i] & 0x0F; + + ret_c[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0'); + ret_c[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0'); + } + + [pool release]; + + return [OFString stringWithCString: ret_c + length: 32]; +} + +- (OFString*)SHA1Hash +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFMD5Hash *hash = [OFSHA1Hash SHA1Hash]; + uint8_t *digest; + char ret_c[OF_SHA1_DIGEST_SIZE * 2]; + size_t i; + + [hash updateWithBuffer: data + ofSize: count * itemSize]; + digest = [hash digest]; + + for (i = 0; i < OF_SHA1_DIGEST_SIZE; i++) { + uint8_t high, low; + + high = digest[i] >> 4; + low = digest[i] & 0x0F; + + ret_c[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0'); + ret_c[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0'); + } + + [pool release]; + + return [OFString stringWithCString: ret_c + length: 40]; +} +@end Index: src/OFDataArray.h ================================================================== --- src/OFDataArray.h +++ src/OFDataArray.h @@ -174,5 +174,7 @@ @interface OFBigDataArray: OFDataArray { size_t size; } @end + +#import "OFDataArray+Hashing.h" Index: src/OFDataArray.m ================================================================== --- src/OFDataArray.m +++ src/OFDataArray.m @@ -23,10 +23,16 @@ #import "OFDataArray.h" #import "OFString.h" #import "OFFile.h" #import "OFExceptions.h" #import "macros.h" + +/* References for static linking */ +void _references_to_categories_of_OFDataArray() +{ + _OFDataArray_Hashing_reference = 1; +}; @implementation OFDataArray + dataArrayWithItemSize: (size_t)is { return [[[self alloc] initWithItemSize: is] autorelease]; Index: src/OFString+Hashing.h ================================================================== --- src/OFString+Hashing.h +++ src/OFString+Hashing.h @@ -23,11 +23,11 @@ #ifdef __cplusplus } #endif /** - * The OFString (OFHashing) category provides methods to calculate hashes for + * The OFString (Hashing) category provides methods to calculate hashes for * strings. */ @interface OFString (Hashing) /** * \return The MD5 hash of the string as an autoreleased OFString Index: src/OFString+Hashing.m ================================================================== --- src/OFString+Hashing.m +++ src/OFString+Hashing.m @@ -14,12 +14,10 @@ * file. */ #include "config.h" -#include - #import "OFString.h" #import "OFMD5Hash.h" #import "OFSHA1Hash.h" #import "OFAutoreleasePool.h" @@ -29,18 +27,18 @@ - (OFString*)MD5Hash { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFMD5Hash *hash = [OFMD5Hash MD5Hash]; uint8_t *digest; - char ret_c[32]; + char ret_c[OF_MD5_DIGEST_SIZE * 2]; size_t i; [hash updateWithBuffer: string ofSize: length]; digest = [hash digest]; - for (i = 0; i < 16; i++) { + for (i = 0; i < OF_MD5_DIGEST_SIZE; i++) { uint8_t high, low; high = digest[i] >> 4; low = digest[i] & 0x0F; @@ -57,18 +55,18 @@ - (OFString*)SHA1Hash { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFMD5Hash *hash = [OFSHA1Hash SHA1Hash]; uint8_t *digest; - char ret_c[40]; + char ret_c[OF_SHA1_DIGEST_SIZE * 2]; size_t i; [hash updateWithBuffer: string ofSize: length]; digest = [hash digest]; - for (i = 0; i < 20; i++) { + for (i = 0; i < OF_SHA1_DIGEST_SIZE; i++) { uint8_t high, low; high = digest[i] >> 4; low = digest[i] & 0x0F; Index: tests/OFDataArrayTests.m ================================================================== --- tests/OFDataArrayTests.m +++ tests/OFDataArrayTests.m @@ -96,10 +96,14 @@ R([array[0] addNItems: 2 fromCArray: "bc" atIndex: 1]) && [array[0] count] == 5 && !memcmp([array[0] cArray], "abcde", 5)) + TEST(@"-[MD5Hash]", [[array[0] MD5Hash] isEqual: [@"abcde" MD5Hash]]) + + TEST(@"-[SHA1Hash]", [[array[0] SHA1Hash] isEqual: [@"abcde" SHA1Hash]]) + TEST(@"Building strings", (array[0] = [class dataArrayWithItemSize: 1]) && R([array[0] addNItems: 6 fromCArray: (void*)str]) && R([array[0] addItem: ""]) && !strcmp([array[0] cArray], str))