-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskeinhash.c
83 lines (68 loc) · 1.43 KB
/
skeinhash.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "tfdef.h"
#include "skein.h"
#include "tfsupport.h"
static char srcblk[DATASIZE];
static char dgst[SKEIN_DIGEST_SIZE];
static int will_exit;
static struct skein sk;
static void usage(void)
{
printf("usage: skeinhash [-b BITS] srcfile.\n");
printf("This program is part of Threefish cipher reference.\n");
exit(1);
}
static void xerror(const char *s)
{
perror(s);
exit(2);
}
int main(int argc, char **argv)
{
int fd;
char *fname;
size_t lio, lrem, ldone, lblock;
size_t bits = TF_MAX_BITS;
char *pblk;
fname = argv[1];
if (argc < 2) usage();
if (!fname) usage();
if (!strcmp(fname, "-b") && argc >= 4) {
bits = atoi(argv[2]);
if (bits > TF_MAX_BITS) bits = TF_MAX_BITS;
fname = argv[3];
}
if (!strcmp(fname, "-")) fd = 0;
else {
fd = open(fname, O_RDONLY);
if (fd == -1) xerror(fname);
}
skein_init(&sk, bits);
while (1) {
if (will_exit) break;
pblk = srcblk;
ldone = 0;
lrem = lblock = sizeof(srcblk);
_ragain: lio = read(fd, pblk, lrem);
if (lio == 0) will_exit = 1;
if (lio != NOSIZE) ldone += lio;
else xerror(fname);
if (lio && lio < lrem) {
pblk += lio;
lrem -= lio;
goto _ragain;
}
skein_update(&sk, srcblk, ldone);
}
close(fd);
skein_final(dgst, &sk);
mhexdump(dgst, TF_FROM_BITS(bits), HD_WIDTH);
return 0;
}