-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrypto-hash-sha512.c
64 lines (52 loc) · 1.29 KB
/
crypto-hash-sha512.c
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
%use buffer_put;
%use buffer_puthex;
%use buffer_0;
%use buffer_1;
%use buffer_2;
%use strerr_die;
%use strerr_sys;
%use stralloc_ready;
%use crypto_str_hash_sha512;
%use appendchunk;
%use stralloc_copys;
%use error_str;
%use str_diff;
*/
/* Public Domain */
#include <nacl/crypto_hash.h>
#include "crypto_str.h"
#include "stralloc.h"
#include "buffer.h"
#include "strerr.h"
#include "appendchunk.h"
#include "byte.h"
static stralloc m = {0};
static stralloc h = {0};
#define FATAL "crypto-hash-sha512: error: "
int main(int argc, char * argv[])
{
int r;
/* Check args */
if(argc>2 || ( argc==2 && str_diff(argv[1],"-b")))
strerr_die1x(111,"crypto-hash-sha512: usage: crypto-hash-sha512 [-b] < message");
/* Read entire input */
for (;;) {
/* Read message */
if((r=appendchunk(&m,buffer_0))==-1)
strerr_die2sys(111,FATAL,"unable to read input: ");
else if(r==0)
break;
}
/* Hash message */
if(crypto_str_hash_sha512(&h,&m)!=0)
strerr_die2x(111,FATAL,"hash failed");
/* Output hash */
if(argc==2 && !str_diff(argv[1],"-b")) {
buffer_putflush(buffer_1,h.s,h.len);
} else {
buffer_puthex(buffer_1,h.s,h.len);
buffer_putsflush(buffer_1,"\n");
}
return 0;
}