ObjFW  Diff

Differences From Artifact [dab01f181a]:

To Artifact [3030cde43c]:


10
11
12
13
14
15
16

17


18
19
20
21
22
23
24
 */

#include "config.h"

#include <string.h>

#import "OFHashes.h"

#import "OFMacros.h"



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

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







>

>
>







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 */

#include "config.h"

#include <string.h>

#import "OFHashes.h"
#import "OFAutoreleasePool.h"
#import "OFMacros.h"

int _OFHashing_reference;

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

/* The four MD5 core functions - F1 is optimized somewhat */
#define F1(x, y, z) (z ^ (x & (y ^ z)))
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203

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

	return self;
}

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

	if (calculated)
		return (char*)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







|





|







186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

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

	return self;
}

- (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
	((uint32_t*)in)[15] = bits[1];

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

	calculated = YES;

	return (char*)buf;
}
@end

#undef F1
#undef F2
#undef F3
#undef F4







|







231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
	((uint32_t*)in)[15] = bits[1];

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

	calculated = YES;

	return (uint8_t*)buf;
}
@end

#undef F1
#undef F2
#undef F3
#undef F4
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
		return self;

	sha1_update(state, &count, buffer, buf, size);

	return self;
}

- (char*)digest
{
	size_t i;
	char   finalcount[8];

	if (calculated)
		return digest;








|







383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
		return self;

	sha1_update(state, &count, buffer, buf, size);

	return self;
}

- (uint8_t*)digest
{
	size_t i;
	char   finalcount[8];

	if (calculated)
		return digest;

413
414
415
416
417
418
419


























































#undef blk0
#undef blk
#undef R0
#undef R1
#undef R2
#undef R3
#undef R4

































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#undef blk0
#undef blk
#undef R0
#undef R1
#undef R2
#undef R3
#undef R4

@implementation OFString (OFHashing)
- (OFString*)md5Hash
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *hash = [OFMD5Hash md5Hash];
	uint8_t *digest;
	char ret_c[33];
	size_t i;

	[hash updateWithBuffer: string
			ofSize: length];
	digest = [hash digest];

	for (i = 0; i < 16; i++) {
		uint8_t high, low;

		high = digest[i] >> 4;
		low  = digest[i] & 0x0F;

		ret_c[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0');
		ret_c[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0');
	}
	ret_c[32] = 0;

	[pool release];

	return [OFString stringWithCString: ret_c];
}

- (OFString*)sha1Hash
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *hash = [OFSHA1Hash sha1Hash];
	uint8_t *digest;
	char ret_c[41];
	size_t i;

	[hash updateWithBuffer: string
			ofSize: length];
	digest = [hash digest];

	for (i = 0; i < 20; i++) {
		uint8_t high, low;

		high = digest[i] >> 4;
		low  = digest[i] & 0x0F;

		ret_c[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0');
		ret_c[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0');
	}
	ret_c[40] = 0;

	[pool release];

	return [OFString stringWithCString: ret_c];
}
@end