Changes to update Tomato RAF.
[tomato.git] / release / src / router / dnscrypt / src / libnacl / commandline / nacl-sha512.c
blob6864c76a2deef7f7d04bdb15743625ee3b17790f
1 /*
2 commandline/nacl-sha512.c version 20080713
3 D. J. Bernstein
4 Public domain.
5 */
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <sys/mman.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include "crypto_hash_sha512.h"
16 unsigned char *input;
17 unsigned long long inputalloc;
18 unsigned long long inputlen;
20 unsigned char h[crypto_hash_sha512_BYTES];
22 void h_print(void)
24 int i;
25 for (i = 0;i < crypto_hash_sha512_BYTES;++i) printf("%02x",255 & (int) h[i]);
26 printf("\n");
29 int main()
31 struct stat st;
32 int ch;
34 if (fstat(0,&st) == 0) {
35 input = mmap(0,st.st_size,PROT_READ,MAP_SHARED,0,0);
36 if (input != MAP_FAILED) {
37 crypto_hash_sha512(h,input,st.st_size);
38 h_print();
39 return 0;
43 input = 0;
44 inputalloc = 0;
45 inputlen = 0;
47 while ((ch = getchar()) != EOF) {
48 if (inputlen >= inputalloc) {
49 void *newinput;
50 while (inputlen >= inputalloc)
51 inputalloc = inputalloc * 2 + 1;
52 if (posix_memalign(&newinput,16,inputalloc) != 0) return 111;
53 memcpy(newinput,input,inputlen);
54 free(input);
55 input = newinput;
57 input[inputlen++] = ch;
60 crypto_hash_sha512(h,input,inputlen);
61 h_print();
63 return 0;