ObjFW  Diff

Differences From Artifact [8c8aeaf4bf]:

To Artifact [bbf6824f58]:


31
32
33
34
35
36
37
38

39
40
41
42
43
44
45




46
47
48
49
50
51
52
31
32
33
34
35
36
37

38
39
40
41




42
43
44
45
46
47
48
49
50
51
52







-
+



-
-
-
-
+
+
+
+







#define F4(x, y, z) (y ^ (x | ~z))

/* This is the central step in the MD5 algorithm. */
#define MD5STEP(f, w, x, y, z, data, s) \
	(w += f(x, y, z) + data, w = w << s | w >> (32 - s), w += x)

static void
md5_transform(uint32_t buf[4], const uint32_t in[16])
md5_transform(uint32_t buffer[4], const uint32_t in[16])
{
	register uint32_t a, b, c, d;

	a = buf[0];
	b = buf[1];
	c = buf[2];
	d = buf[3];
	a = buffer[0];
	b = buffer[1];
	c = buffer[2];
	d = buffer[3];

	MD5STEP(F1, a, b, c, d, in[0]  + 0xD76AA478, 7);
	MD5STEP(F1, d, a, b, c, in[1]  + 0xE8C7B756, 12);
	MD5STEP(F1, c, d, a, b, in[2]  + 0x242070DB, 17);
	MD5STEP(F1, b, c, d, a, in[3]  + 0xC1BDCEEE, 22);
	MD5STEP(F1, a, b, c, d, in[4]  + 0xF57C0FAF, 7);
	MD5STEP(F1, d, a, b, c, in[5]  + 0x4787C62A, 12);
108
109
110
111
112
113
114
115
116
117
118




119
120
121
122
123
124
125
108
109
110
111
112
113
114




115
116
117
118
119
120
121
122
123
124
125







-
-
-
-
+
+
+
+







	MD5STEP(F4, c, d, a, b, in[6]  + 0xA3014314, 15);
	MD5STEP(F4, b, c, d, a, in[13] + 0x4E0811A1, 21);
	MD5STEP(F4, a, b, c, d, in[4]  + 0xF7537E82, 6);
	MD5STEP(F4, d, a, b, c, in[11] + 0xBD3AF235, 10);
	MD5STEP(F4, c, d, a, b, in[2]  + 0x2AD7D2BB, 15);
	MD5STEP(F4, b, c, d, a, in[9]  + 0xEB86D391, 21);

	buf[0] += a;
	buf[1] += b;
	buf[2] += c;
	buf[3] += d;
	buffer[0] += a;
	buffer[1] += b;
	buffer[2] += c;
	buffer[3] += d;
}

@implementation OFMD5Hash
+ MD5Hash
{
	return [[[self alloc] init] autorelease];
}
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
203
204
205
206
207
208
209
210

211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

231
232
233
234
235
236
237
238
239
240
241
242
243
244

245
246
247
248
249

250
251
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
203
204
205
206
207
208
209

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229

230
231
232
233
234
235
236
237
238
239
240
241
242
243

244
245
246
247
248

249
250
251







-
-
-
-
+
+
+
+




-
-
+
+



-
+








-
+


-
+










-
-
+
+



-
+

-
+

-
-
+
+



-
-
+
+

-
+

-
-
+
+



-
+








-
+



















-
+













-
+




-
+


	return 64;
}

- init
{
	self = [super init];

	buf[0] = 0x67452301;
	buf[1] = 0xEFCDAB89;
	buf[2] = 0x98BADCFE;
	buf[3] = 0x10325476;
	buffer[0] = 0x67452301;
	buffer[1] = 0xEFCDAB89;
	buffer[2] = 0x98BADCFE;
	buffer[3] = 0x10325476;

	return self;
}

- (void)updateWithBuffer: (const char*)buffer
		  ofSize: (size_t)size
- (void)updateWithBuffer: (const char*)buffer_
		  length: (size_t)length
{
	uint32_t t;

	if (size == 0)
	if (length == 0)
		return;

	if (isCalculated)
		@throw [OFHashAlreadyCalculatedException newWithClass: isa
								 hash: self];

	/* Update bitcount */
	t = bits[0];
	if ((bits[0] = t + ((uint32_t)size << 3)) < t)
	if ((bits[0] = t + ((uint32_t)length << 3)) < t)
		/* Carry from low to high */
		bits[1]++;
	bits[1] += (uint32_t)size >> 29;
	bits[1] += (uint32_t)length >> 29;

	/* Bytes already in shsInfo->data */
	t = (t >> 3) & 0x3F;

	/* Handle any leading odd-sized chunks */
	if (t) {
		uint8_t *p = in.u8 + t;

		t = 64 - t;

		if (size < t) {
			memcpy(p, buffer, size);
		if (length < t) {
			memcpy(p, buffer_, length);
			return;
		}

		memcpy(p, buffer, t);
		memcpy(p, buffer_, t);
		of_bswap32_vec_if_be(in.u32, 16);
		md5_transform(buf, in.u32);
		md5_transform(buffer, in.u32);

		buffer += t;
		size -= t;
		buffer_ += t;
		length -= t;
	}

	/* Process data in 64-byte chunks */
	while (size >= 64) {
		memcpy(in.u8, buffer, 64);
	while (length >= 64) {
		memcpy(in.u8, buffer_, 64);
		of_bswap32_vec_if_be(in.u32, 16);
		md5_transform(buf, in.u32);
		md5_transform(buffer, in.u32);

		buffer += 64;
		size -= 64;
		buffer_ += 64;
		length -= 64;
	}

	/* Handle any remaining bytes of data. */
	memcpy(in.u8, buffer, size);
	memcpy(in.u8, buffer_, length);
}

- (uint8_t*)digest
{
	uint8_t	*p;
	size_t	count;

	if (isCalculated)
		return (uint8_t*)buf;
		return (uint8_t*)buffer;

	/* Compute number of bytes mod 64 */
	count = (bits[0] >> 3) & 0x3F;

	/*
	 * Set the first char of padding to 0x80. This is safe since there is
	 * always at least one byte free
	 */
	p = in.u8 + count;
	*p++ = 0x80;

	/* Bytes of padding needed to make 64 bytes */
	count = 64 - 1 - count;

	/* Pad out to 56 mod 64 */
	if (count < 8) {
		/* Two lots of padding: Pad the first block to 64 bytes */
		memset(p, 0, count);
		of_bswap32_vec_if_be(in.u32, 16);
		md5_transform(buf, in.u32);
		md5_transform(buffer, in.u32);

		/* Now fill the next block with 56 bytes */
		memset(in.u8, 0, 56);
	} else {
		/* Pad block to 56 bytes */
		memset(p, 0, count - 8);
	}
	of_bswap32_vec_if_be(in.u32, 14);

	/* Append length in bits and transform */
	in.u32[14] = bits[0];
	in.u32[15] = bits[1];

	md5_transform(buf, in.u32);
	md5_transform(buffer, in.u32);
	of_bswap32_vec_if_be(buf, 4);

	isCalculated = YES;

	return (uint8_t*)buf;
	return (uint8_t*)buffer;
}
@end