Add crypt.[ch] and update md5sum and sha1sum
Factor out the code from md5sum and sha1sum into a util function. Use FILE * instead of a file descriptor. This will make it a bit easier/more consistent when we implement support for the -c option.
This commit is contained in:
		
							
								
								
									
										52
									
								
								md5sum.c
									
									
									
									
									
								
							
							
						
						
									
										52
									
								
								md5sum.c
									
									
									
									
									
								
							@@ -1,15 +1,18 @@
 | 
			
		||||
/* See LICENSE file for copyright and license details. */
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <sys/stat.h>
 | 
			
		||||
#include <fcntl.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include "util.h"
 | 
			
		||||
#include "crypt.h"
 | 
			
		||||
#include "md5.h"
 | 
			
		||||
 | 
			
		||||
static void md5sum(int fd, const char *f);
 | 
			
		||||
struct md5 s;
 | 
			
		||||
struct crypt_ops md5_ops = {
 | 
			
		||||
	md5_init,
 | 
			
		||||
	md5_update,
 | 
			
		||||
	md5_sum,
 | 
			
		||||
	&s,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
usage(void)
 | 
			
		||||
@@ -20,7 +23,8 @@ usage(void)
 | 
			
		||||
int
 | 
			
		||||
main(int argc, char *argv[])
 | 
			
		||||
{
 | 
			
		||||
	int fd;
 | 
			
		||||
	FILE *fp;
 | 
			
		||||
	uint8_t md[MD5_DIGEST_LENGTH];
 | 
			
		||||
 | 
			
		||||
	ARGBEGIN {
 | 
			
		||||
	default:
 | 
			
		||||
@@ -28,40 +32,18 @@ main(int argc, char *argv[])
 | 
			
		||||
	} ARGEND;
 | 
			
		||||
 | 
			
		||||
	if (argc == 0) {
 | 
			
		||||
		md5sum(STDIN_FILENO, "<stdin>");
 | 
			
		||||
		cryptsum(&md5_ops, stdin, "<stdin>", md);
 | 
			
		||||
		mdprint(md, "<stdin>", sizeof(md));
 | 
			
		||||
	} else {
 | 
			
		||||
		for (; argc > 0; argc--) {
 | 
			
		||||
			if ((fd = open(*argv, O_RDONLY)) < 0)
 | 
			
		||||
				eprintf("open %s:", *argv);
 | 
			
		||||
			md5sum(fd, *argv);
 | 
			
		||||
			close(fd);
 | 
			
		||||
			if ((fp = fopen(*argv, "r"))  == NULL)
 | 
			
		||||
				eprintf("fopen %s:", *argv);
 | 
			
		||||
			cryptsum(&md5_ops, fp, *argv, md);
 | 
			
		||||
			mdprint(md, *argv, sizeof(md));
 | 
			
		||||
			fclose(fp);
 | 
			
		||||
			argv++;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
md5sum(int fd, const char *f)
 | 
			
		||||
{
 | 
			
		||||
	unsigned char buf[BUFSIZ];
 | 
			
		||||
	unsigned char digest[MD5_DIGEST_LENGTH];
 | 
			
		||||
	struct md5 s;
 | 
			
		||||
	ssize_t n;
 | 
			
		||||
	int i;
 | 
			
		||||
 | 
			
		||||
	md5_init(&s);
 | 
			
		||||
	while ((n = read(fd, buf, sizeof buf)) > 0)
 | 
			
		||||
		md5_update(&s, buf, n);
 | 
			
		||||
	if (n < 0) {
 | 
			
		||||
		eprintf("%s: read error:", f);
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	md5_sum(&s, digest);
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < sizeof(digest); i++)
 | 
			
		||||
		printf("%02x", digest[i]);
 | 
			
		||||
	printf("  %s\n", f);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user