DEFINITIONS
This source file includes following functions.
- SHA256_File
- SHA256_End
- SHA256_Data
- SHA384_File
- SHA384_End
- SHA384_Data
- SHA512_File
- SHA512_End
- SHA512_Data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 #include "sha2.h"
46
47 #ifndef lint
48
49 #endif
50
51
52
53 #include <assert.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <stdlib.h>
59 #if defined(HAVE_UNISTD_H)
60 # include <unistd.h>
61 #endif
62
63 #ifndef _DIAGASSERT
64 #define _DIAGASSERT(cond) assert(cond)
65 #endif
66
67
68
69
70
71 static const char sha2_hex_digits[] = "0123456789abcdef";
72
73 char *
74 SHA256_File(char *filename, char *buf)
75 {
76 uint8_t buffer[BUFSIZ * 20];
77 SHA256_CTX ctx;
78 int fd, num, oerrno;
79
80 _DIAGASSERT(filename != NULL);
81
82
83 SHA256_Init(&ctx);
84
85 if ((fd = open(filename, O_RDONLY)) < 0)
86 return (0);
87
88 while ((num = read(fd, buffer, sizeof(buffer))) > 0)
89 SHA256_Update(&ctx, buffer, (size_t) num);
90
91 oerrno = errno;
92 close(fd);
93 errno = oerrno;
94 return (num < 0 ? 0 : SHA256_End(&ctx, buf));
95 }
96
97
98 char *
99 SHA256_End(SHA256_CTX *ctx, char buffer[])
100 {
101 uint8_t digest[SHA256_DIGEST_LENGTH], *d = digest;
102 uint8_t *ret;
103 int i;
104
105
106 assert(ctx != NULL);
107
108 if ((ret = buffer) != NULL) {
109 SHA256_Final(digest, ctx);
110
111 for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
112 *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
113 *buffer++ = sha2_hex_digits[*d & 0x0f];
114 d++;
115 }
116 *buffer = (char) 0;
117 } else {
118 (void) memset(ctx, 0, sizeof(SHA256_CTX));
119 }
120 (void) memset(digest, 0, SHA256_DIGEST_LENGTH);
121 return ret;
122 }
123
124 char *
125 SHA256_Data(const uint8_t * data, size_t len, char *digest)
126 {
127 SHA256_CTX ctx;
128
129 SHA256_Init(&ctx);
130 SHA256_Update(&ctx, data, len);
131 return SHA256_End(&ctx, digest);
132 }
133
134 char *
135 SHA384_File(char *filename, char *buf)
136 {
137 SHA384_CTX ctx;
138 uint8_t buffer[BUFSIZ * 20];
139 int fd, num, oerrno;
140
141 _DIAGASSERT(filename != NULL);
142
143
144 SHA384_Init(&ctx);
145
146 if ((fd = open(filename, O_RDONLY)) < 0)
147 return (0);
148
149 while ((num = read(fd, buffer, sizeof(buffer))) > 0)
150 SHA384_Update(&ctx, buffer, (size_t) num);
151
152 oerrno = errno;
153 close(fd);
154 errno = oerrno;
155 return (num < 0 ? 0 : SHA384_End(&ctx, buf));
156 }
157
158 char *
159 SHA384_End(SHA384_CTX * ctx, char buffer[])
160 {
161 uint8_t digest[SHA384_DIGEST_LENGTH], *d = digest;
162 uint8_t *ret;
163 int i;
164
165
166 assert(ctx != NULL);
167
168 if ((ret = buffer) != NULL) {
169 SHA384_Final(digest, ctx);
170
171 for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
172 *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
173 *buffer++ = sha2_hex_digits[*d & 0x0f];
174 d++;
175 }
176 *buffer = (char) 0;
177 } else {
178 (void) memset(ctx, 0, sizeof(SHA384_CTX));
179 }
180 (void) memset(digest, 0, SHA384_DIGEST_LENGTH);
181 return ret;
182 }
183
184 char *
185 SHA384_Data(const uint8_t * data, size_t len, char *digest)
186 {
187 SHA384_CTX ctx;
188
189 SHA384_Init(&ctx);
190 SHA384_Update(&ctx, data, len);
191 return SHA384_End(&ctx, digest);
192 }
193
194 char *
195 SHA512_File(char *filename, char *buf)
196 {
197 SHA512_CTX ctx;
198 uint8_t buffer[BUFSIZ * 20];
199 int fd, num, oerrno;
200
201 _DIAGASSERT(filename != NULL);
202
203
204 SHA512_Init(&ctx);
205
206 if ((fd = open(filename, O_RDONLY)) < 0)
207 return (0);
208
209 while ((num = read(fd, buffer, sizeof(buffer))) > 0)
210 SHA512_Update(&ctx, buffer, (size_t) num);
211
212 oerrno = errno;
213 close(fd);
214 errno = oerrno;
215 return (num < 0 ? 0 : SHA512_End(&ctx, buf));
216 }
217
218 char *
219 SHA512_End(SHA512_CTX * ctx, char buffer[])
220 {
221 uint8_t digest[SHA512_DIGEST_LENGTH], *d = digest;
222 uint8_t *ret;
223 int i;
224
225
226 assert(ctx != NULL);
227
228 if ((ret = buffer) != NULL) {
229 SHA512_Final(digest, ctx);
230
231 for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
232 *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
233 *buffer++ = sha2_hex_digits[*d & 0x0f];
234 d++;
235 }
236 *buffer = (char) 0;
237 } else {
238 (void) memset(ctx, 0, sizeof(SHA512_CTX));
239 }
240 (void) memset(digest, 0, SHA512_DIGEST_LENGTH);
241 return ret;
242 }
243
244 char *
245 SHA512_Data(const uint8_t * data, size_t len, char *digest)
246 {
247 SHA512_CTX ctx;
248
249 SHA512_Init(&ctx);
250 SHA512_Update(&ctx, data, len);
251 return SHA512_End(&ctx, digest);
252 }