- int i;
- struct MD5Context ctx;
- unsigned char digest[16];
- unsigned char buf[BUFFERSIZE];
- int fd;
- int c;
- pid_t pid;
- char *file = NULL;
- int r;
- struct timeval tv;
- struct timezone tz;
-
- setlocale(LC_ALL, "");
- bindtextdomain(PACKAGE, LOCALEDIR);
- textdomain(PACKAGE);
-
- while ((c = getopt( argc, argv, "vf:" )) != -1)
- switch (c) {
- case 'v': ++Verbose; break;
- case 'f': file = optarg; break;
- }
-
- MD5Init( &ctx );
- gettimeofday( &tv, &tz );
- MD5Update( &ctx, (unsigned char *)&tv, sizeof( tv ) );
-
- pid = getppid();
- MD5Update( &ctx, (unsigned char *)&pid, sizeof( pid ));
- pid = getpid();
- MD5Update( &ctx, (unsigned char *)&pid, sizeof( pid ));
-
- if (file) {
- int count = 0;
-
- if (file[0] == '-' && !file[1])
- fd = fileno(stdin);
- else
- fd = open( file, O_RDONLY );
-
- if (fd < 0) {
- fprintf( stderr, _("Could not open %s\n"), file );
- } else {
- count = hash_file( &ctx, fd );
- if (Verbose)
- fprintf( stderr, _("Got %d bytes from %s\n"), count, file );
-
- if (file[0] != '-' || file[1]) close( fd );
- }
- }
-
- for (i = 0; i < RNGS; i++) {
- if ((fd = open( rngs[i].path, O_RDONLY|O_NONBLOCK )) >= 0) {
- int count = sizeof(buf);
-
- if (rngs[i].maxlength && count > rngs[i].maxlength)
- count = rngs[i].maxlength;
- r = read( fd, buf, count );
- if (r > 0)
- MD5Update( &ctx, buf, r );
- else
- r = 0;
- close( fd );
- if (Verbose)
- fprintf( stderr, _("Got %d bytes from %s\n"), r, rngs[i].path );
- if (rngs[i].minlength && r >= rngs[i].minlength)
- break;
- } else if (Verbose)
- fprintf( stderr, _("Could not open %s\n"), rngs[i].path );
- }
-
- MD5Final( digest, &ctx );
- for (i = 0; i < 16; i++) printf( "%02x", digest[i] );
- putchar ( '\n' );
-
- /*
- * The following is important for cases like disk full, so shell scripts
- * can bomb out properly rather than think they succeeded.
- */
- if (fflush(stdout) < 0 || fclose(stdout) < 0)
- return 1;
-
- return 0;
+ struct mcookie_control ctl = { .verbose = 0 };
+ size_t i;
+ unsigned char digest[UL_MD5LENGTH];
+ unsigned char buf[RAND_BYTES];
+ int c;
+
+ static const struct option longopts[] = {
+ {"file", required_argument, NULL, 'f'},
+ {"max-size", required_argument, NULL, 'm'},
+ {"verbose", no_argument, NULL, 'v'},
+ {"version", no_argument, NULL, 'V'},
+ {"help", no_argument, NULL, 'h'},
+ {NULL, 0, NULL, 0}
+ };
+
+ setlocale(LC_ALL, "");
+ bindtextdomain(PACKAGE, LOCALEDIR);
+ textdomain(PACKAGE);
+ close_stdout_atexit();
+
+ while ((c = getopt_long(argc, argv, "f:m:vVh", longopts, NULL)) != -1) {
+ switch (c) {
+ case 'v':
+ ctl.verbose = 1;
+ break;
+ case 'f':
+ if (!ctl.files)
+ ctl.files = xmalloc(sizeof(char *) * argc);
+ ctl.files[ctl.nfiles++] = optarg;
+ break;
+ case 'm':
+ ctl.maxsz = strtosize_or_err(optarg,
+ _("failed to parse length"));
+ break;
+
+ case 'V':
+ print_version(EXIT_SUCCESS);
+ case 'h':
+ usage();
+ default:
+ errtryhelp(EXIT_FAILURE);
+ }
+ }
+
+ if (ctl.maxsz && ctl.nfiles == 0)
+ warnx(_("--max-size ignored when used without --file"));
+
+ ul_MD5Init(&ctl.ctx);
+ randomness_from_files(&ctl);
+ free(ctl.files);
+
+ ul_random_get_bytes(&buf, RAND_BYTES);
+ ul_MD5Update(&ctl.ctx, buf, RAND_BYTES);
+ if (ctl.verbose)
+ fprintf(stderr, P_("Got %d byte from %s\n",
+ "Got %d bytes from %s\n", RAND_BYTES),
+ RAND_BYTES, random_tell_source());
+
+ ul_MD5Final(digest, &ctl.ctx);
+ for (i = 0; i < UL_MD5LENGTH; i++)
+ printf("%02x", digest[i]);
+ putchar('\n');
+
+ return EXIT_SUCCESS;