9 #include <openssl/bio.h>
10 #include <openssl/err.h>
11 #include <openssl/rand.h>
12 #include <openssl/engine.h>
15 #define PROG rand_main
17 /* -out file - write to file
18 * -rand file:file - PRNG seed files
19 * -base64 - encode output
20 * num - write 'num' bytes
23 int MAIN(int, char **);
25 int MAIN(int argc, char **argv)
40 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
41 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
45 while (!badopt && argv[++i] != NULL)
47 if (strcmp(argv[i], "-out") == 0)
49 if ((argv[i+1] != NULL) && (outfile == NULL))
54 else if (strcmp(argv[i], "-engine") == 0)
56 if ((argv[i+1] != NULL) && (engine == NULL))
61 else if (strcmp(argv[i], "-rand") == 0)
63 if ((argv[i+1] != NULL) && (inrand == NULL))
68 else if (strcmp(argv[i], "-base64") == 0)
75 else if (isdigit((unsigned char)argv[i][0]))
79 r = sscanf(argv[i], "%d", &num);
80 if (r == 0 || num < 0)
95 BIO_printf(bio_err, "Usage: rand [options] num\n");
96 BIO_printf(bio_err, "where options are\n");
97 BIO_printf(bio_err, "-out file - write to file\n");
98 BIO_printf(bio_err, "-engine e - use engine e, possibly a hardware device.\n");
99 BIO_printf(bio_err, "-rand file%cfile%c... - seed PRNG from files\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
100 BIO_printf(bio_err, "-base64 - encode output\n");
104 e = setup_engine(bio_err, engine, 0);
106 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
108 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
109 app_RAND_load_files(inrand));
111 out = BIO_new(BIO_s_file());
115 r = BIO_write_filename(out, outfile);
118 r = BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
119 #ifdef OPENSSL_SYS_VMS
121 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
122 out = BIO_push(tmpbio, out);
131 BIO *b64 = BIO_new(BIO_f_base64());
134 out = BIO_push(b64, out);
139 unsigned char buf[4096];
143 if (chunk > sizeof buf)
145 r = RAND_bytes(buf, chunk);
148 BIO_write(out, buf, chunk);
153 app_RAND_write_file(NULL, bio_err);
157 ERR_print_errors(bio_err);