/*
 * Petit programme calculant l'empreinte md5 d'un fichier
 * passe en argument.
 *
 * fichier mymd5.c          Andre Brouty    14 juin 2001
 *
 * 	Usage: mymd5 fichier
 *
 * Compilation sur linux Red Hat:
 *	gcc -o mymd5 -lssl mymd5.c
 */

#include <stdio.h>
#include <openssl/md5.h>
#include <sys/types.h>
#include <stdlib.h>

#define  TMD5	16
#define  TBUF	1024

main (argc, argv)
int argc;
char *argv[];

{
	char buf[TBUF];
	u_char md5_buffer[TMD5];
	int ips, taille;
	MD5_CTX md5_context;

	if (argc < 2) {
		fprintf (stderr, "Usage: %s file\n", argv[0]);
		exit (1);
	}
	if ((ips = open (argv[1], 0)) < 0) {
		fprintf (stderr, "Error open: unable to open %s\n", argv[1]);
		exit (1);
	}
	MD5_Init (&md5_context);
	while ((taille = read (ips, buf, TBUF)) > 0) {
		MD5_Update (&md5_context, buf, taille);
	}
	if (taille < 0) {
		fprintf (stderr, "Error read: unable to read %s\n", argv[1]);
		exit (1);
	}
	MD5_Final (md5_buffer, &md5_context);
#ifdef TEST
	write (1, md5_buffer, TMD5);
#else
	for (ips = 0; ips < TMD5; ++ips)
		printf ("%.2x", (u_char) md5_buffer[ips]);
	printf ("\n");
#endif
}
