Index: src/OFDataArray+Hashing.m ================================================================== --- src/OFDataArray+Hashing.m +++ src/OFDataArray+Hashing.m @@ -16,31 +16,33 @@ #include "config.h" #import "OFDataArray.h" #import "OFString.h" +#import "OFHash.h" #import "OFMD5Hash.h" #import "OFSHA1Hash.h" #import "autorelease.h" int _OFDataArray_Hashing_reference; @implementation OFDataArray (Hashing) -- (OFString*)MD5Hash +- (OFString*)OF_hashAsStringWithHash: (Class )hashClass { void *pool = objc_autoreleasePoolPush(); - OFMD5Hash *hash = [OFMD5Hash hash]; - uint8_t *digest; - char cString[OF_MD5_DIGEST_SIZE * 2]; + id hash = [hashClass hash]; + size_t digestSize = [hashClass digestSize]; + const uint8_t *digest; + char cString[digestSize * 2]; size_t i; [hash updateWithBuffer: _items length: _count * _itemSize]; digest = [hash digest]; - for (i = 0; i < OF_MD5_DIGEST_SIZE; i++) { + for (i = 0; i < digestSize; i++) { uint8_t high, low; high = digest[i] >> 4; low = digest[i] & 0x0F; @@ -50,37 +52,18 @@ objc_autoreleasePoolPop(pool); return [OFString stringWithCString: cString encoding: OF_STRING_ENCODING_ASCII - length: OF_MD5_DIGEST_SIZE * 2]; + length: digestSize * 2]; +} + +- (OFString*)MD5Hash +{ + return [self OF_hashAsStringWithHash: [OFMD5Hash class]]; } - (OFString*)SHA1Hash { - void *pool = objc_autoreleasePoolPush(); - OFSHA1Hash *hash = [OFSHA1Hash hash]; - uint8_t *digest; - char cString[OF_SHA1_DIGEST_SIZE * 2]; - size_t i; - - [hash updateWithBuffer: _items - length: _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; - - cString[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0'); - cString[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0'); - } - - objc_autoreleasePoolPop(pool); - - return [OFString stringWithCString: cString - encoding: OF_STRING_ENCODING_ASCII - length: OF_SHA1_DIGEST_SIZE * 2]; + return [self OF_hashAsStringWithHash: [OFSHA1Hash class]]; } @end Index: src/OFHash.h ================================================================== --- src/OFHash.h +++ src/OFHash.h @@ -60,14 +60,14 @@ * The size of the buffer depends on the hash used. The buffer is part of the * receiver's memory pool. * * @return A buffer containing the hash */ -- (uint8_t*)digest OF_RETURNS_INNER_POINTER; +- (const uint8_t*)digest OF_RETURNS_INNER_POINTER; /*! * @brief Returns a boolean whether the hash has already been calculated. * * @return A boolean whether the hash has already been calculated */ - (bool)isCalculated; @end Index: src/OFMD5Hash.h ================================================================== --- src/OFMD5Hash.h +++ src/OFMD5Hash.h @@ -14,12 +14,10 @@ * file. */ #import "OFHash.h" -#define OF_MD5_DIGEST_SIZE 16 - /*! * @brief A class which provides functions to create an MD5 hash. */ @interface OFMD5Hash: OFObject { Index: src/OFMD5Hash.m ================================================================== --- src/OFMD5Hash.m +++ src/OFMD5Hash.m @@ -213,11 +213,11 @@ /* Handle any remaining bytes of data. */ memcpy(_in.u8, buffer, length); } -- (uint8_t*)digest +- (const uint8_t*)digest { uint8_t *p; size_t count; if (_calculated) @@ -258,13 +258,13 @@ md5_transform(_buffer, _in.u32); BSWAP32_VEC_IF_BE(_buffer, 4); _calculated = true; - return (uint8_t*)_buffer; + return (const uint8_t*)_buffer; } - (bool)isCalculated { return _calculated; } @end Index: src/OFSHA1Hash.h ================================================================== --- src/OFSHA1Hash.h +++ src/OFSHA1Hash.h @@ -14,19 +14,17 @@ * file. */ #import "OFHash.h" -#define OF_SHA1_DIGEST_SIZE 20 - /*! * @brief A class which provides functions to create an SHA1 hash. */ @interface OFSHA1Hash: OFObject { uint32_t _state[5]; uint64_t _count; char _buffer[64]; - uint8_t _digest[OF_SHA1_DIGEST_SIZE]; + uint8_t _digest[20]; bool _calculated; } @end Index: src/OFSHA1Hash.m ================================================================== --- src/OFSHA1Hash.m +++ src/OFSHA1Hash.m @@ -168,11 +168,11 @@ exceptionWithHash: self]; sha1_update(_state, &_count, _buffer, buffer, length); } -- (uint8_t*)digest +- (const uint8_t*)digest { size_t i; char finalcount[8]; if (_calculated) @@ -186,11 +186,11 @@ while ((_count & 504) != 448) sha1_update(_state, &_count, _buffer, "\0", 1); /* Should cause a sha1_transform() */ sha1_update(_state, &_count, _buffer, finalcount, 8); - for (i = 0; i < OF_SHA1_DIGEST_SIZE; i++) + for (i = 0; i < 20; i++) _digest[i] = (char)((_state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255); _calculated = true; Index: src/OFString+Hashing.m ================================================================== --- src/OFString+Hashing.m +++ src/OFString+Hashing.m @@ -15,71 +15,54 @@ */ #include "config.h" #import "OFString.h" +#import "OFHash.h" #import "OFMD5Hash.h" #import "OFSHA1Hash.h" #import "autorelease.h" int _OFString_Hashing_reference; @implementation OFString (Hashing) -- (OFString*)MD5Hash +- (OFString*)OF_hashAsStringWithHash: (Class )hashClass { void *pool = objc_autoreleasePoolPush(); - OFMD5Hash *hash = [OFMD5Hash hash]; - uint8_t *digest; - char ret[OF_MD5_DIGEST_SIZE * 2]; + id hash = [hashClass hash]; + size_t digestSize = [hashClass digestSize]; + const uint8_t *digest; + char cString[digestSize * 2]; size_t i; [hash updateWithBuffer: [self UTF8String] length: [self UTF8StringLength]]; 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[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0'); - ret[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0'); - } - - objc_autoreleasePoolPop(pool); - - return [OFString stringWithCString: ret - encoding: OF_STRING_ENCODING_ASCII - length: 32]; -} - -- (OFString*)SHA1Hash -{ - void *pool = objc_autoreleasePoolPush(); - OFSHA1Hash *hash = [OFSHA1Hash hash]; - uint8_t *digest; - char ret[OF_SHA1_DIGEST_SIZE * 2]; - size_t i; - - [hash updateWithBuffer: [self UTF8String] - length: [self UTF8StringLength]]; - 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[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0'); - ret[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0'); - } - - objc_autoreleasePoolPop(pool); - - return [OFString stringWithCString: ret - encoding: OF_STRING_ENCODING_ASCII - length: 40]; + for (i = 0; i < digestSize; i++) { + uint8_t high, low; + + high = digest[i] >> 4; + low = digest[i] & 0x0F; + + cString[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0'); + cString[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0'); + } + + objc_autoreleasePoolPop(pool); + + return [OFString stringWithCString: cString + encoding: OF_STRING_ENCODING_ASCII + length: digestSize * 2]; +} + +- (OFString*)MD5Hash +{ + return [self OF_hashAsStringWithHash: [OFMD5Hash class]]; +} + +- (OFString*)SHA1Hash +{ + return [self OF_hashAsStringWithHash: [OFSHA1Hash class]]; } @end Index: tests/OFMD5HashTests.m ================================================================== --- tests/OFMD5HashTests.m +++ tests/OFMD5HashTests.m @@ -27,11 +27,11 @@ #import "TestsAppDelegate.h" static OFString *module = @"OFMD5Hash"; -const uint8_t testfile_md5[OF_MD5_DIGEST_SIZE] = +const uint8_t testfile_md5[16] = "\x00\x8B\x9D\x1B\x58\xDF\xF8\xFE\xEE\xF3\xAE\x8D\xBB\x68\x2D\x38"; @implementation TestsAppDelegate (OFMD5HashTests) - (void)MD5HashTests { @@ -49,16 +49,15 @@ [md5 updateWithBuffer: buf length: len]; } [f close]; - TEST(@"-[digest]", - !memcmp([md5 digest], testfile_md5, OF_MD5_DIGEST_SIZE)) + TEST(@"-[digest]", !memcmp([md5 digest], testfile_md5, 16)) EXPECT_EXCEPTION(@"Detect invalid call of " @"-[updateWithBuffer:length]", OFHashAlreadyCalculatedException, [md5 updateWithBuffer: "" length: 1]) [pool drain]; } @end Index: tests/OFSHA1HashTests.m ================================================================== --- tests/OFSHA1HashTests.m +++ tests/OFSHA1HashTests.m @@ -27,11 +27,11 @@ #import "TestsAppDelegate.h" static OFString *module = @"OFHash"; -const uint8_t testfile_sha1[OF_SHA1_DIGEST_SIZE] = +const uint8_t testfile_sha1[20] = "\xC9\x9A\xB8\x7E\x1E\xC8\xEC\x65\xD5\xEB\xE4\x2E\x0D\xA6\x80\x96\xF5" "\x94\xE7\x17"; @implementation TestsAppDelegate (SHA1HashTests) - (void)SHA1HashTests @@ -50,16 +50,15 @@ [sha1 updateWithBuffer: buf length: len]; } [f close]; - TEST(@"-[digest]", - !memcmp([sha1 digest], testfile_sha1, OF_SHA1_DIGEST_SIZE)) + TEST(@"-[digest]", !memcmp([sha1 digest], testfile_sha1, 20)) EXPECT_EXCEPTION(@"Detect invalid call of " @"-[updateWithBuffer:length:]", OFHashAlreadyCalculatedException, [sha1 updateWithBuffer: "" length: 1]) [pool drain]; } @end