@@ -21,12 +21,12 @@ #import "OFSecureData.h" #import "OFHashAlreadyCalculatedException.h" #import "OFOutOfRangeException.h" -#define DIGEST_SIZE 20 -#define BLOCK_SIZE 64 +static const size_t digestSize = 20; +static const size_t blockSize = 64; OF_DIRECT_MEMBERS @interface OFSHA1Hash () - (void)of_resetState; @end @@ -39,11 +39,11 @@ static OF_INLINE void byteSwapVectorIfLE(uint32_t *vector, uint_fast8_t length) { #ifndef OF_BIG_ENDIAN for (uint_fast8_t i = 0; i < length; i++) - vector[i] = OF_BSWAP32(vector[i]); + vector[i] = OFByteSwap32(vector[i]); #endif } static void processBlock(uint32_t *state, uint32_t *buffer) @@ -60,21 +60,21 @@ byteSwapVectorIfLE(buffer, 16); for (i = 16; i < 80; i++) { uint32_t tmp = buffer[i - 3] ^ buffer[i - 8] ^ buffer[i - 14] ^ buffer[i - 16]; - buffer[i] = OF_ROL(tmp, 1); + buffer[i] = OFRotateLeft(tmp, 1); } #define LOOP_BODY(f, k) \ { \ - uint32_t tmp = OF_ROL(new[0], 5) + \ + uint32_t tmp = OFRotateLeft(new[0], 5) + \ f(new[0], new[1], new[2], new[3]) + \ new[4] + k + buffer[i]; \ new[4] = new[3]; \ new[3] = new[2]; \ - new[2] = OF_ROL(new[1], 30); \ + new[2] = OFRotateLeft(new[1], 30); \ new[1] = new[0]; \ new[0] = tmp; \ } for (i = 0; i < 20; i++) @@ -99,19 +99,19 @@ @synthesize calculated = _calculated; @synthesize allowsSwappableMemory = _allowsSwappableMemory; + (size_t)digestSize { - return DIGEST_SIZE; + return digestSize; } + (size_t)blockSize { - return BLOCK_SIZE; + return blockSize; } -+ (instancetype)cryptoHashWithAllowsSwappableMemory: (bool)allowsSwappableMemory ++ (instancetype)hashWithAllowsSwappableMemory: (bool)allowsSwappableMemory { return [[[self alloc] initWithAllowsSwappableMemory: allowsSwappableMemory] autorelease]; } @@ -152,16 +152,16 @@ [super dealloc]; } - (size_t)digestSize { - return DIGEST_SIZE; + return digestSize; } - (size_t)blockSize { - return BLOCK_SIZE; + return blockSize; } - (id)copy { OFSHA1Hash *copy = [[OFSHA1Hash alloc] of_init]; @@ -181,12 +181,11 @@ _iVars->state[2] = 0x98BADCFE; _iVars->state[3] = 0x10325476; _iVars->state[4] = 0xC3D2E1F0; } -- (void)updateWithBuffer: (const void *)buffer_ - length: (size_t)length +- (void)updateWithBuffer: (const void *)buffer_ length: (size_t)length { const unsigned char *buffer = buffer_; if (_calculated) @throw [OFHashAlreadyCalculatedException @@ -221,25 +220,25 @@ { if (_calculated) return (const unsigned char *)_iVars->state; _iVars->buffer.bytes[_iVars->bufferLength] = 0x80; - of_explicit_memset(_iVars->buffer.bytes + _iVars->bufferLength + 1, 0, + OFZeroMemory(_iVars->buffer.bytes + _iVars->bufferLength + 1, 64 - _iVars->bufferLength - 1); if (_iVars->bufferLength >= 56) { processBlock(_iVars->state, _iVars->buffer.words); - of_explicit_memset(_iVars->buffer.bytes, 0, 64); + OFZeroMemory(_iVars->buffer.bytes, 64); } _iVars->buffer.words[14] = - OF_BSWAP32_IF_LE((uint32_t)(_iVars->bits >> 32)); + OFToBigEndian32((uint32_t)(_iVars->bits >> 32)); _iVars->buffer.words[15] = - OF_BSWAP32_IF_LE((uint32_t)(_iVars->bits & 0xFFFFFFFF)); + OFToBigEndian32((uint32_t)(_iVars->bits & 0xFFFFFFFF)); processBlock(_iVars->state, _iVars->buffer.words); - of_explicit_memset(&_iVars->buffer, 0, sizeof(_iVars->buffer)); + OFZeroMemory(&_iVars->buffer, sizeof(_iVars->buffer)); byteSwapVectorIfLE(_iVars->state, 5); _calculated = true; return (const unsigned char *)_iVars->state; } @@ -246,10 +245,10 @@ - (void)reset { [self of_resetState]; _iVars->bits = 0; - of_explicit_memset(&_iVars->buffer, 0, sizeof(_iVars->buffer)); + OFZeroMemory(&_iVars->buffer, sizeof(_iVars->buffer)); _iVars->bufferLength = 0; _calculated = false; } @end