ObjFW  Check-in [165c2c0b9d]

Overview
Comment:OFMD5Hash improvements.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 165c2c0b9db56658c7d9065c56a7559c54e30af1c4b57e43e96137cbfe7cde07
User & Date: js on 2008-10-26 17:48:05
Other Links: manifest | tags
Context
2008-10-26
19:35
Add OFSHA1Hash. check-in: 631895440e user: js tags: trunk
17:48
OFMD5Hash improvements. check-in: 165c2c0b9d user: js tags: trunk
12:05
Actually test OFXMLFactory. check-in: 58e11df891 user: js tags: trunk
Changes

Modified src/OFHashes.h from [714154ae18] to [4951f2110c].

14
15
16
17
18
19
20


21
22
23
24
25
26
27
#import "OFObject.h"

@interface OFMD5Hash: OFObject
{
	uint32_t buf[4];
	uint32_t bits[2];
	uint8_t	 in[64];


}

- init;
- (void)updateWithBuffer: (const uint8_t*)buf
		  ofSize: (size_t)size;
- (uint8_t*)digest;
@end







>
>







14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#import "OFObject.h"

@interface OFMD5Hash: OFObject
{
	uint32_t buf[4];
	uint32_t bits[2];
	uint8_t	 in[64];

	BOOL	 calculated;
}

- init;
- (void)updateWithBuffer: (const uint8_t*)buf
		  ofSize: (size_t)size;
- (uint8_t*)digest;
@end

Modified src/OFHashes.m from [5bb7027c8f] to [e568bcc89c].

17
18
19
20
21
22
23
24
25
26
27
28
29

30
31
32
33
34




35
36
37
38
39
40
41
#import "OFHashes.h"

#ifdef BIG_ENDIAN
inline void
bswap(uint8_t *buf, size_t len)
{
	uint32_t t;
	do {
		t = (uint32_t)((uint32_t)buf[3] << 8 | buf[2]) << 16 |
		    ((uint32_t)buf[1] << 8 | buf[0]);
		*(uint32_t*)buf = t;
		buf += 4;
	} while(--len);

}
#else
#define bswap(buf, len)
#endif





/* The four MD5 core functions - F1 is optimized somewhat */
#define F1(x, y, z) (z ^ (x & (y ^ z)))
#define F2(x, y, z) F1(z, x, y)
#define F3(x, y, z) (x ^ y ^ z)
#define F4(x, y, z) (y ^ (x | ~z))

/* This is the central step in the MD5 algorithm. */







|




<
>





>
>
>
>







17
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
#import "OFHashes.h"

#ifdef BIG_ENDIAN
inline void
bswap(uint8_t *buf, size_t len)
{
	uint32_t t;
	while (len--) {
		t = (uint32_t)((uint32_t)buf[3] << 8 | buf[2]) << 16 |
		    ((uint32_t)buf[1] << 8 | buf[0]);
		*(uint32_t*)buf = t;
		buf += 4;

	}
}
#else
#define bswap(buf, len)
#endif

/*******
 * MD5 *
 *******/

/* The four MD5 core functions - F1 is optimized somewhat */
#define F1(x, y, z) (z ^ (x & (y ^ z)))
#define F2(x, y, z) F1(z, x, y)
#define F3(x, y, z) (x ^ y ^ z)
#define F4(x, y, z) (y ^ (x | ~z))

/* This is the central step in the MD5 algorithm. */
133
134
135
136
137
138
139


140
141
142
143
144
145
146
147
148





149
150
151
152
153
154
155
		buf[0] = 0x67452301;
		buf[1] = 0xEFCDAB89;
		buf[2] = 0x98BADCFE;
		buf[3] = 0x10325476;

		bits[0] = 0;
		bits[1] = 0;


	}

	return self;
}

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






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








>
>









>
>
>
>
>







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
		buf[0] = 0x67452301;
		buf[1] = 0xEFCDAB89;
		buf[2] = 0x98BADCFE;
		buf[3] = 0x10325476;

		bits[0] = 0;
		bits[1] = 0;

		calculated = NO;
	}

	return self;
}

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

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

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

185
186
187
188
189
190
191



192
193
194
195
196
197
198
}

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




	/* 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
	 */







>
>
>







196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
}

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

	if (calculated)
		return (uint8_t*)buf;

	/* 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
	 */
219
220
221
222
223
224
225


226
227
228
229

	/* Append length in bits and transform */
	((uint32_t*)in)[14] = bits[0];
	((uint32_t*)in)[15] = bits[1];

	md5_transform(buf, (uint32_t*)in);
	bswap((uint8_t*)buf, 4);



	return (uint8_t*)buf;
}
@end







>
>




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

	/* Append length in bits and transform */
	((uint32_t*)in)[14] = bits[0];
	((uint32_t*)in)[15] = bits[1];

	md5_transform(buf, (uint32_t*)in);
	bswap((uint8_t*)buf, 4);

	calculated = YES;

	return (uint8_t*)buf;
}
@end