22
28
#include <stdlib.h>
25
29
#include <sys/time.h>
26
30
#include <unistd.h>
29
#define BUFFERSIZE 4096
32
#define BUFFERSIZE 4096
33
int minlength, maxlength;
36
int minlength, maxlength;
35
{ "/dev/random", 16, 16 }, /* 16 bytes = 128 bits suffice */
36
{ "/proc/interrupts", 0, 0 },
37
{ "/proc/slabinfo", 0, 0 },
38
{ "/proc/stat", 0, 0 },
39
{ "/dev/urandom", 32, 64 },
38
{"/dev/random", 16, 16}, /* 16 bytes = 128 bits suffice */
39
{"/proc/interrupts", 0, 0},
40
{"/proc/slabinfo", 0, 0},
42
{"/dev/urandom", 32, 64},
41
45
#define RNGS (sizeof(rngs)/sizeof(struct rngs))
45
47
/* The basic function to hash a file */
47
hash_file(struct MD5Context *ctx, int fd)
51
unsigned char buf[BUFFERSIZE];
53
while ((r = read(fd, buf, sizeof(buf))) > 0) {
54
MD5Update(ctx, buf, r);
57
/* Separate files with a null byte */
59
MD5Update(ctx, buf, 1);
63
int main( int argc, char **argv )
66
struct MD5Context ctx;
67
unsigned char digest[16];
68
unsigned char buf[BUFFERSIZE];
77
setlocale(LC_ALL, "");
78
bindtextdomain(PACKAGE, LOCALEDIR);
81
while ((c = getopt( argc, argv, "vf:" )) != -1)
83
case 'v': ++Verbose; break;
84
case 'f': file = optarg; break;
88
gettimeofday( &tv, &tz );
89
MD5Update( &ctx, (unsigned char *)&tv, sizeof( tv ) );
92
MD5Update( &ctx, (unsigned char *)&pid, sizeof( pid ));
94
MD5Update( &ctx, (unsigned char *)&pid, sizeof( pid ));
99
if (file[0] == '-' && !file[1])
102
fd = open( file, O_RDONLY );
105
fprintf( stderr, _("Could not open %s\n"), file );
107
count = hash_file( &ctx, fd );
109
fprintf( stderr, _("Got %d bytes from %s\n"), count, file );
111
if (file[0] != '-' || file[1]) close( fd );
115
for (i = 0; i < RNGS; i++) {
116
if ((fd = open( rngs[i].path, O_RDONLY|O_NONBLOCK )) >= 0) {
117
int count = sizeof(buf);
119
if (rngs[i].maxlength && count > rngs[i].maxlength)
120
count = rngs[i].maxlength;
121
r = read( fd, buf, count );
123
MD5Update( &ctx, buf, r );
128
fprintf( stderr, _("Got %d bytes from %s\n"), r, rngs[i].path );
129
if (rngs[i].minlength && r >= rngs[i].minlength)
132
fprintf( stderr, _("Could not open %s\n"), rngs[i].path );
135
MD5Final( digest, &ctx );
136
for (i = 0; i < 16; i++) printf( "%02x", digest[i] );
140
* The following is important for cases like disk full, so shell scripts
141
* can bomb out properly rather than think they succeeded.
143
if (fflush(stdout) < 0 || fclose(stdout) < 0)
48
static off_t hash_file(struct MD5Context *ctx, int fd)
52
unsigned char buf[BUFFERSIZE];
54
while ((r = read(fd, buf, sizeof(buf))) > 0) {
55
MD5Update(ctx, buf, r);
58
/* Separate files with a null byte */
60
MD5Update(ctx, buf, 1);
64
static void __attribute__ ((__noreturn__)) usage(FILE * out)
66
fputs(_("\nUsage:\n"), out);
68
_(" %s [options]\n"), program_invocation_short_name);
70
fputs(_("\nOptions:\n"), out);
71
fputs(_(" -f, --file <file> use file as a cookie seed\n"
72
" -v, --verbose explain what is being done\n"
73
" -V, --version output version information and exit\n"
74
" -h, --help display this help and exit\n\n"), out);
76
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
79
int main(int argc, char **argv)
82
struct MD5Context ctx;
83
unsigned char digest[MD5LENGTH];
84
unsigned char buf[BUFFERSIZE];
94
static const struct option longopts[] = {
95
{"file", required_argument, NULL, 'f'},
96
{"verbose", no_argument, NULL, 'v'},
97
{"version", no_argument, NULL, 'V'},
98
{"help", no_argument, NULL, 'h'},
102
setlocale(LC_ALL, "");
103
bindtextdomain(PACKAGE, LOCALEDIR);
107
getopt_long(argc, argv, "f:vVh", longopts, NULL)) != -1)
116
printf(_("%s from %s\n"),
117
program_invocation_short_name,
127
gettimeofday(&tv, &tz);
128
MD5Update(&ctx, (unsigned char *) &tv, sizeof(tv));
131
MD5Update(&ctx, (unsigned char *) &pid, sizeof(pid));
133
MD5Update(&ctx, (unsigned char *) &pid, sizeof(pid));
138
if (file[0] == '-' && !file[1])
141
fd = open(file, O_RDONLY);
144
warn(_("Could not open %s"), file);
146
count = hash_file(&ctx, fd);
149
_("Got %d bytes from %s\n"), count,
152
if (fd != STDIN_FILENO)
155
_("closing %s failed"), file);
159
for (i = 0; i < RNGS; i++) {
160
if ((fd = open(rngs[i].path, O_RDONLY | O_NONBLOCK)) >= 0) {
161
int count = sizeof(buf);
163
if (rngs[i].maxlength && count > rngs[i].maxlength)
164
count = rngs[i].maxlength;
165
r = read(fd, buf, count);
167
MD5Update(&ctx, buf, r);
173
_("Got %d bytes from %s\n"), r,
175
if (rngs[i].minlength && r >= rngs[i].minlength)
178
warn(_("Could not open %s"), rngs[i].path);
181
MD5Final(digest, &ctx);
182
for (i = 0; i < MD5LENGTH; i++)
183
printf("%02x", digest[i]);
187
* The following is important for cases like disk full,
188
* so shell scripts can bomb out properly rather than
189
* think they succeeded.
191
if (fflush(stdout) < 0 || fclose(stdout) < 0)