8 #include <openssl/bio.h>
9 #include <openssl/err.h>
10 #include <openssl/rand.h>
13 #define PROG rand_main
15 /* -out file - write to file
16 * -rand file:file - PRNG seed files
17 * -base64 - encode output
18 * num - write 'num' bytes
21 int MAIN(int, char **);
23 int MAIN(int argc, char **argv)
36 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
37 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
41 while (!badopt && argv[++i] != NULL)
43 if (strcmp(argv[i], "-out") == 0)
45 if ((argv[i+1] != NULL) && (outfile == NULL))
50 else if (strcmp(argv[i], "-rand") == 0)
52 if ((argv[i+1] != NULL) && (inrand == NULL))
57 else if (strcmp(argv[i], "-base64") == 0)
64 else if (isdigit(argv[i][0]))
68 r = sscanf(argv[i], "%d", &num);
69 if (r == 0 || num < 0)
84 BIO_printf(bio_err, "Usage: rand [options] num\n");
85 BIO_printf(bio_err, "where options are\n");
86 BIO_printf(bio_err, "-out file - write to file\n");
87 BIO_printf(bio_err, "-rand file%cfile%c... - seed PRNG from files\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
88 BIO_printf(bio_err, "-base64 - encode output\n");
92 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
94 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
95 app_RAND_load_files(inrand));
97 out = BIO_new(BIO_s_file());
101 r = BIO_write_filename(out, outfile);
103 r = BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
109 BIO *b64 = BIO_new(BIO_f_base64());
112 out = BIO_push(b64, out);
117 unsigned char buf[4096];
121 if (chunk > sizeof buf)
123 r = RAND_bytes(buf, chunk);
126 BIO_write(out, buf, chunk);
131 app_RAND_write_file(NULL, bio_err);
135 ERR_print_errors(bio_err);