Home | History | Annotate | Line # | Download | only in sha2
sha256hl.c revision 1.1
      1 /* $NetBSD: sha256hl.c,v 1.1 2005/08/23 16:20:01 elad Exp $ */
      2 
      3 /*
      4  * ----------------------------------------------------------------------------
      5  * "THE BEER-WARE LICENSE" (Revision 42):
      6  * <phk (at) login.dkuug.dk> wrote this file.  As long as you retain this notice you
      7  * can do whatever you want with this stuff. If we meet some day, and you think
      8  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
      9  * ----------------------------------------------------------------------------
     10  */
     11 
     12 #include <sys/param.h>
     13 #include <sys/stat.h>
     14 
     15 #include <errno.h>
     16 #include <fcntl.h>
     17 #include <stdlib.h>
     18 #include <stdio.h>
     19 #include <string.h>
     20 #include <unistd.h>
     21 
     22 #include <crypto/sha2.h>
     23 
     24 /* ARGSUSED */
     25 char *
     26 SHA256_End(SHA256_CTX *ctx, char *buf)
     27 {
     28 	int i;
     29 	u_int8_t digest[SHA256_DIGEST_LENGTH];
     30 	static const char hex[] = "0123456789abcdef";
     31 
     32 	if (buf == NULL && (buf = malloc(SHA256_DIGEST_STRING_LENGTH)) == NULL)
     33 		return (NULL);
     34 
     35 	SHA256_Final(digest, ctx);
     36 	for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
     37 		buf[i + i] = hex[digest[i] >> 4];
     38 		buf[i + i + 1] = hex[digest[i] & 0x0f];
     39 	}
     40 	buf[i + i] = '\0';
     41 	memset(digest, 0, sizeof(digest));
     42 	return (buf);
     43 }
     44 
     45 char *
     46 SHA256_FileChunk(const char *filename, char *buf, off_t off, off_t len)
     47 {
     48 	struct stat sb;
     49 	u_char buffer[BUFSIZ];
     50 	SHA256_CTX ctx;
     51 	int fd, save_errno;
     52 	ssize_t nr;
     53 
     54 	SHA256_Init(&ctx);
     55 
     56 	if ((fd = open(filename, O_RDONLY)) < 0)
     57 		return (NULL);
     58 	if (len == 0) {
     59 		if (fstat(fd, &sb) == -1) {
     60 			close(fd);
     61 			return (NULL);
     62 		}
     63 		len = sb.st_size;
     64 	}
     65 	if (off > 0 && lseek(fd, off, SEEK_SET) < 0)
     66 		return (NULL);
     67 
     68 	while ((nr = read(fd, buffer, MIN(sizeof(buffer), len))) > 0) {
     69 		SHA256_Update(&ctx, buffer, (size_t)nr);
     70 		if (len > 0 && (len -= nr) == 0)
     71 			break;
     72 	}
     73 
     74 	save_errno = errno;
     75 	close(fd);
     76 	errno = save_errno;
     77 	return (nr < 0 ? NULL : SHA256_End(&ctx, buf));
     78 }
     79 
     80 char *
     81 SHA256_File(const char *filename, char *buf)
     82 {
     83 	return (SHA256_FileChunk(filename, buf, (off_t)0, (off_t)0));
     84 }
     85 
     86 char *
     87 SHA256_Data(const u_char *data, size_t len, char *buf)
     88 {
     89 	SHA256_CTX ctx;
     90 
     91 	SHA256_Init(&ctx);
     92 	SHA256_Update(&ctx, data, len);
     93 	return (SHA256_End(&ctx, buf));
     94 }
     95