summaryrefslogtreecommitdiffstats
path: root/nthash.c
blob: 5e90c5d93752c5aef4d9ae8eea4713ee9df37763 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
46
47
48
49
50
51
52
53
/*
 * (C) 2012-2013 by Christian Hesse <mail@eworm.de>
 *
 * This software may be used and distributed according to the terms
 * of the GNU General Public License, incorporated herein by reference.
 *
 * Base on an example from:
 * http://www.lysator.liu.se/~nisse/nettle/nettle.html#Example
 */

#include <stdio.h>
#include <stdlib.h>
		 
#include <nettle/md4.h>
		 
#define BUF_SIZE 1024

int main(int argc, char **argv) {
	struct md4_ctx ctx;
	uint8_t buffer[BUF_SIZE], buffernull[2*BUF_SIZE];
	uint8_t digest[MD4_DIGEST_SIZE];
	int i, done;
	
	md4_init(&ctx);
	while (1) {
		done = fread(buffer, 1, sizeof(buffer), stdin);
		/* add null bytes to string */
		for (i = 0; i < done; i++) {
			if (buffer[i] == 0xa)
				fprintf(stderr, "Warning: Password contains line break!\n");
			else if (buffer[i] < 0x20 || buffer[i] == 0x7f)
				fprintf(stderr, "Warning: Password contains non-printable control character 0x%x!\n", buffer[i]);
			else if (buffer[i] > 0x7f)
				fprintf(stderr, "Warning: Password contains non-ASCII character 0x%x!\n", buffer[i]);
			buffernull[i*2] = buffer[i];
			buffernull[i*2+1] = 0;
		}
		md4_update(&ctx, done*2, buffernull);
		if (done < sizeof(buffer))
			break;
	}
	if (ferror(stdin))
		return EXIT_FAILURE;

	md4_digest(&ctx, MD4_DIGEST_SIZE, digest);

	for (i = 0; i < MD4_DIGEST_SIZE -1; i++)
		printf("%02x ", digest[i]);
	printf("%02x", digest[i]);
	putchar('\n');

	return EXIT_SUCCESS;
}