1 /**	$MirOS: src/lib/libc/hash/helper.c,v 1.6 2010/01/07 22:34:51 tg Exp $ */
2 /*	$OpenBSD: helper.c,v 1.8 2005/08/08 08:05:35 espie Exp $	*/
3 
4 /*
5  * ----------------------------------------------------------------------------
6  * "THE BEER-WARE LICENSE" (Revision 42):
7  * <phk@login.dkuug.dk> wrote this file.  As long as you retain this notice you
8  * can do whatever you want with this stuff. If we meet some day, and you think
9  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
10  * ----------------------------------------------------------------------------
11  */
12 
13 #include <sys/param.h>
14 #include <sys/stat.h>
15 
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <unistd.h>
22 
23 #include <hashinc>
24 
25 __RCSID("$MirOS: src/lib/libc/hash/helper.c,v 1.6 2010/01/07 22:34:51 tg Exp $ helper for HASH hash");
26 
27 extern const uint8_t mbsd_digits_hex[17];
28 extern const uint8_t mbsd_digits_HEX[17];
29 
30 /* ARGSUSED */
31 char *
HASHEnd(HASH_CTX * ctx,char * buf)32 HASHEnd(HASH_CTX *ctx, char *buf)
33 {
34 	int i;
35 	u_int8_t digest[HASH_DIGEST_LENGTH];
36 #ifdef HASH_DIGEST_UPPERCASE
37 #define hex mbsd_digits_HEX
38 #else
39 #define hex mbsd_digits_hex
40 #endif
41 
42 	if (buf == NULL && (buf = malloc(HASH_DIGEST_STRING_LENGTH)) == NULL)
43 		return (NULL);
44 
45 	HASHFinal(digest, ctx);
46 	for (i = 0; i < HASH_DIGEST_LENGTH; i++) {
47 		buf[i + i] = hex[digest[i] >> 4];
48 		buf[i + i + 1] = hex[digest[i] & 0x0f];
49 	}
50 	buf[i + i] = '\0';
51 	memset(digest, 0, sizeof(digest));
52 	return (buf);
53 }
54 
55 char *
HASHFileChunk(const char * filename,char * buf,off_t off,off_t len)56 HASHFileChunk(const char *filename, char *buf, off_t off, off_t len)
57 {
58 	struct stat sb;
59 	u_char buffer[BUFSIZ];
60 	HASH_CTX ctx;
61 	int fd, save_errno;
62 	ssize_t nr;
63 
64 	HASHInit(&ctx);
65 
66 	if ((fd = open(filename, O_RDONLY)) < 0)
67 		return (NULL);
68 	if (len == 0) {
69 		if (fstat(fd, &sb) == -1) {
70 			close(fd);
71 			return (NULL);
72 		}
73 		len = sb.st_size;
74 	}
75 	if ((len < 0) || (off > 0 && lseek(fd, off, SEEK_SET) < 0))
76 		return (NULL);
77 
78 	while ((nr = read(fd, buffer,
79 	    (size_t)(len ? MIN(BUFSIZ, len) : BUFSIZ))) > 0) {
80 		HASHUpdate(&ctx, buffer, (size_t)nr);
81 		if (len > 0 && (len -= nr) == 0)
82 			break;
83 	}
84 
85 	save_errno = errno;
86 	close(fd);
87 	errno = save_errno;
88 	return (nr < 0 ? NULL : HASHEnd(&ctx, buf));
89 }
90 
91 char *
HASHFile(const char * filename,char * buf)92 HASHFile(const char *filename, char *buf)
93 {
94 	return (HASHFileChunk(filename, buf, (off_t)0, (off_t)0));
95 }
96 
97 char *
HASHData(const u_char * data,size_t len,char * buf)98 HASHData(const u_char *data, size_t len, char *buf)
99 {
100 	HASH_CTX ctx;
101 
102 	HASHInit(&ctx);
103 	HASHUpdate(&ctx, data, len);
104 	return (HASHEnd(&ctx, buf));
105 }
106