install.sh: expand @cfg_name@ in processed scripts
[girocco.git] / src / ltsha256.c
blob83e8d56da9561a33203f87b91c3f206cad12d43f
1 /* License: public domain -or- http://www.wtfpl.net/txt/copying/ */
3 #include "lt256.h"
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <errno.h>
7 #include <unistd.h>
9 #define READSIZE 32768
11 static const char hextab[16] = {
12 '0', '1', '2', '3', '4', '5', '6', '7',
13 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
15 static char buffer[READSIZE];
17 int main(int arc, char *argv[])
19 SHA256_CTX c;
20 unsigned char md[SHA256_DIGEST_LENGTH];
21 char mdhex[(2*SHA256_DIGEST_LENGTH)+1];
22 ssize_t e;
23 unsigned i;
25 if (!SHA256_Init(&c))
26 return EXIT_FAILURE;
28 for (;;) {
29 do {
30 e = read(STDIN_FILENO, buffer, READSIZE);
31 } while (e == -1 &&
32 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
33 if (e < 0)
34 return EXIT_FAILURE;
35 if (!e)
36 break;
37 if (!SHA256_Update(&c, buffer, (size_t)e))
38 return EXIT_FAILURE;
40 if (!SHA256_Final(md, &c))
41 return 0;
42 for (i=0; i < SHA256_DIGEST_LENGTH; ++i) {
43 unsigned char c = md[i];
44 mdhex[i<<1] = hextab[c >> 4];
45 mdhex[(i<<1)+1] = hextab[c & 0xf];
47 mdhex[2*SHA256_DIGEST_LENGTH] = '\0';
48 if (puts(mdhex) < 0)
49 return EXIT_FAILURE;
51 return 0;
54 #include "lt256.c"