DEFINITIONS
This source file includes following functions.
- SHA1_End
- SHA1_File
- SHA1_Data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 #include "sha1.h"
17 #include <fcntl.h>
18
19 #include <assert.h>
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #if defined(HAVE_UNISTD_H)
24 # include <unistd.h>
25 #endif
26
27 #if defined(LIBC_SCCS) && !defined(lint)
28
29 #endif
30
31 #ifndef _DIAGASSERT
32 #define _DIAGASSERT(cond) assert(cond)
33 #endif
34
35
36
37 char *
38 SHA1_End(ctx, buf)
39 SHA1_CTX *ctx;
40 char *buf;
41 {
42 int i;
43 char *p = buf;
44 uint8_t digest[20];
45 static const char hex[]="0123456789abcdef";
46
47 _DIAGASSERT(ctx != NULL);
48
49
50 if (p == NULL && (p = malloc(41)) == NULL)
51 return 0;
52
53 SHA1_Final(digest,ctx);
54 for (i = 0; i < 20; i++) {
55 p[i + i] = hex[((uint32_t)digest[i]) >> 4];
56 p[i + i + 1] = hex[digest[i] & 0x0f];
57 }
58 p[i + i] = '\0';
59 return(p);
60 }
61
62 char *
63 SHA1_File (filename, buf)
64 char *filename;
65 char *buf;
66 {
67 uint8_t buffer[BUFSIZ];
68 SHA1_CTX ctx;
69 int fd, num, oerrno;
70
71 _DIAGASSERT(filename != NULL);
72
73
74 SHA1_Init(&ctx);
75
76 if ((fd = open(filename,O_RDONLY)) < 0)
77 return(0);
78
79 while ((num = read(fd, buffer, sizeof(buffer))) > 0)
80 SHA1_Update(&ctx, buffer, (size_t)num);
81
82 oerrno = errno;
83 close(fd);
84 errno = oerrno;
85 return(num < 0 ? 0 : SHA1_End(&ctx, buf));
86 }
87
88 char *
89 SHA1_Data (data, len, buf)
90 const uint8_t *data;
91 size_t len;
92 char *buf;
93 {
94 SHA1_CTX ctx;
95
96 _DIAGASSERT(data != NULL);
97
98
99 SHA1_Init(&ctx);
100 SHA1_Update(&ctx, data, len);
101 return(SHA1_End(&ctx, buf));
102 }