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