-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathkeccak-cli.c
More file actions
70 lines (53 loc) · 1.37 KB
/
keccak-cli.c
File metadata and controls
70 lines (53 loc) · 1.37 KB
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
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "KeccakNISTInterface.h"
#define BUF_SIZE 1024
void readInput(char *buffer, const unsigned int buffer_size);
int main(void)
{
unsigned char data[BUF_SIZE];
unsigned char result[64];
unsigned int x;
memset(data, 0, sizeof data);
memset(result, 0, sizeof result);
readInput(data, BUF_SIZE);
size_t len = strlen(data);
Hash(512, data, len * 8, result);
unsigned int hash_length = sizeof(result);
for (x = 0; x < hash_length; x++) {
printf("%02x", result[x]);
}
printf("\n");
return 0;
}
void readInput(char *buffer, const unsigned int buffer_size)
{
size_t contentSize = 1;
char *content = malloc(sizeof(char) * buffer_size);
if (content == NULL)
{
perror("Failed to allocate content");
exit(1);
}
content[0] = '\0';
while (fgets(buffer, buffer_size, stdin))
{
char *old = content;
contentSize += strlen(buffer);
content = realloc(content, contentSize);
if (content == NULL)
{
perror("Failed to reallocate content");
free(old);
exit(2);
}
strcat(content, buffer);
}
if (ferror(stdin))
{
free(content);
perror("Error reading from stdin.");
exit(3);
}
}