Differences From Artifact [9801b1cb04]:
- File
src/OFMD5Hash.m
— part of check-in
[13ee56edf3]
at
2014-06-21 21:43:43
on branch trunk
— Move all macros from OFObject.h to macros.h
This means that OFObject.h imports macros.h now, making it unnecessary
to manually import macros.h in almost every file. And while at it, also
import autorelease.h in OFObject.h, so that this doesn't need to be
manually imported in almost every file as well. (user: js, size: 7157) [annotate] [blame] [check-ins using]
To Artifact [307a03b74d]:
- File
src/OFMD5Hash.m
— part of check-in
[c4d2f177cb]
at
2014-08-31 17:59:11
on branch trunk
— Rewrite OFMD5Hash
The old one was based on a public domain implementation, which is
optimized for old compilers and quite unreadable. The new one is written
from scratch and designed to be readable and to exploit optimizations of
modern compilers. (user: js, size: 4615) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
18 19 20 21 22 23 24 | #include <string.h> #import "OFMD5Hash.h" #import "OFHashAlreadyCalculatedException.h" | < | | | | > | > > > | > > > > | > > > > > > > > > > > > > > > > > > > > > | | | < < < > | > > < < < | | > | | | | < | | | | | | | | | < > | | | | < | < < < < < < < < < < < < < < < < > > | | | | | | < < < < < < < < < < < < < < < < < < < < < < < < < < > | | | | | 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | #include <string.h> #import "OFMD5Hash.h" #import "OFHashAlreadyCalculatedException.h" #define F(a, b, c) (((a) & (b)) | (~(a) & (c))) #define G(a, b, c) (((a) & (c)) | ((b) & ~(c))) #define H(a, b, c) ((a) ^ (b) ^ (c)) #define I(a, b, c) ((b) ^ ((a) | ~(c))) static const uint32_t sinTable[] = { 0xD76AA478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE, 0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501, 0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE, 0x6B901122, 0xFD987193, 0xA679438E, 0x49B40821, 0xF61E2562, 0xC040B340, 0x265E5A51, 0xE9B6C7AA, 0xD62F105D, 0x02441453, 0xD8A1E681, 0xE7D3FBC8, 0x21E1CDE6, 0xC33707D6, 0xF4D50D87, 0x455A14ED, 0xA9E3E905, 0xFCEFA3F8, 0x676F02D9, 0x8D2A4C8A, 0xFFFA3942, 0x8771F681, 0x6D9D6122, 0xFDE5380C, 0xA4BEEA44, 0x4BDECFA9, 0xF6BB4B60, 0xBEBFBC70, 0x289B7EC6, 0xEAA127FA, 0xD4EF3085, 0x04881D05, 0xD9D4D039, 0xE6DB99E5, 0x1FA27CF8, 0xC4AC5665, 0xF4292244, 0x432AFF97, 0xAB9423A7, 0xFC93A039, 0x655B59C3, 0x8F0CCC92, 0xFFEFF47D, 0x85845DD1, 0x6FA87E4F, 0xFE2CE6E0, 0xA3014314, 0x4E0811A1, 0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391 }; static const uint8_t wordOrder[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9 }; static const uint8_t rotateBits[] = { 7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21 }; static void byteSwapVectorIfBE(uint32_t *vector, uint_fast8_t length) { uint_fast8_t i; for (i = 0; i < length; i++) vector[i] = OF_BSWAP32_IF_BE(vector[i]); } static void processBlock(uint32_t *state, uint32_t *buffer) { uint32_t new[4]; uint_fast8_t i = 0; new[0] = state[0]; new[1] = state[1]; new[2] = state[2]; new[3] = state[3]; byteSwapVectorIfBE(buffer, 16); #define LOOP_BODY(f) \ { \ const uint_fast8_t a = (4 - (i & 3)) & 3; \ const uint_fast8_t b = (a + 1) & 3; \ const uint_fast8_t c = (a + 2) & 3; \ const uint_fast8_t d = (a + 3) & 3; \ const uint_fast8_t r = rotateBits[(i % 4) + (i / 16) * 4]; \ \ new[a] += f(new[b], new[c], new[d]) + \ buffer[wordOrder[i]] + sinTable[i]; \ new[a] = OF_ROL(new[a], r); \ new[a] += new[b]; \ } for (; i < 16; i++) LOOP_BODY(F) for (; i < 32; i++) LOOP_BODY(G) for (; i < 48; i++) LOOP_BODY(H) for (; i < 64; i++) LOOP_BODY(I) #undef LOOP_BODY state[0] += new[0]; state[1] += new[1]; state[2] += new[2]; state[3] += new[3]; } @implementation OFMD5Hash + (size_t)digestSize { return 16; } |
︙ | ︙ | |||
145 146 147 148 149 150 151 | return [[[self alloc] init] autorelease]; } - init { self = [super init]; | | | | | < | < < < < < < < < < | < < < < < | | | | < | | | < < | | | | < < < < < | | < | | < < < < < | < < | < < < < < < | < < < < < < < | | < | < < < < < | | < | | | | 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | return [[[self alloc] init] autorelease]; } - init { self = [super init]; _state[0] = 0x67452301; _state[1] = 0xEFCDAB89; _state[2] = 0x98BADCFE; _state[3] = 0x10325476; return self; } - (void)updateWithBuffer: (const void*)buffer_ length: (size_t)length { const uint8_t *buffer = buffer_; if (_calculated) @throw [OFHashAlreadyCalculatedException exceptionWithHash: self]; _bits += (length * 8); while (length > 0) { size_t min = 64 - _bufferLength; if (min > length) min = length; memcpy(_buffer.bytes + _bufferLength, buffer, min); _bufferLength += min; buffer += min; length -= min; if (_bufferLength == 64) { processBlock(_state, _buffer.words); _bufferLength = 0; } } } - (const uint8_t*)digest { if (_calculated) return (const uint8_t*)_state; _buffer.bytes[_bufferLength] = 0x80; memset(_buffer.bytes + _bufferLength + 1, 0, 64 - _bufferLength - 1); if (_bufferLength >= 56) { processBlock(_state, _buffer.words); memset(_buffer.bytes, 0, 64); } _buffer.words[14] = OF_BSWAP32_IF_BE((uint32_t)(_bits & 0xFFFFFFFF)); _buffer.words[15] = OF_BSWAP32_IF_BE((uint32_t)(_bits >> 32)); processBlock(_state, _buffer.words); byteSwapVectorIfBE(_state, 4); _calculated = true; return (const uint8_t*)_state; } - (bool)isCalculated { return _calculated; } @end |