Home | History | Annotate | Line # | Download | only in sha2
sha512hl.c revision 1.2
      1 /* $NetBSD: sha512hl.c,v 1.2 2005/08/24 12:08:45 tron 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 SHA512_End(SHA512_CTX *ctx, char *buf)
     27 {
     28 	int i;
     29 	u_int8_t digest[SHA512_DIGEST_LENGTH];
     30 	static const char hex[] = "0123456789abcdef";
     31 
     32 	if (buf == NULL && (buf = malloc(SHA512_DIGEST_STRING_LENGTH)) == NULL)
     33 		return (NULL);
     34 
     35 	SHA512_Final(digest, ctx);
     36 	for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
     37 		buf[i + i] = hex[(u_int32_t)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 SHA512_FileChunk(const char *filename, char *buf, off_t off, off_t len)
     47 {
     48 	struct stat sb;
     49 	u_char buffer[BUFSIZ];
     50 	SHA512_CTX ctx;
     51 	int fd, save_errno;
     52 	ssize_t nr;
     53 
     54 	SHA512_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, (size_t) MIN(sizeof(buffer), len)))
     69 	    > 0) {
     70 		SHA512_Update(&ctx, buffer, (size_t)nr);
     71 		if (len > 0 && (len -= nr) == 0)
     72 			break;
     73 	}
     74 
     75 	save_errno = errno;
     76 	close(fd);
     77 	errno = save_errno;
     78 	return (nr < 0 ? NULL : SHA512_End(&ctx, buf));
     79 }
     80 
     81 char *
     82 SHA512_File(const char *filename, char *buf)
     83 {
     84 	return (SHA512_FileChunk(filename, buf, (off_t)0, (off_t)0));
     85 }
     86 
     87 char *
     88 SHA512_Data(const u_char *data, size_t len, char *buf)
     89 {
     90 	SHA512_CTX ctx;
     91 
     92 	SHA512_Init(&ctx);
     93 	SHA512_Update(&ctx, data, len);
     94 	return (SHA512_End(&ctx, buf));
     95 }
     96