9 #include <openssl/bio.h>
10 #include <openssl/err.h>
11 #include <openssl/rand.h>
14 #define PROG rand_main
16 /* -out file - write to file
17 * -rand file:file - PRNG seed files
18 * -base64 - encode output
19 * num - write 'num' bytes
22 int MAIN(int, char **);
24 int MAIN(int argc, char **argv)
39 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
40 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
44 while (!badopt && argv[++i] != NULL)
46 if (strcmp(argv[i], "-out") == 0)
48 if ((argv[i+1] != NULL) && (outfile == NULL))
53 else if (strcmp(argv[i], "-engine") == 0)
55 if ((argv[i+1] != NULL) && (engine == NULL))
60 else if (strcmp(argv[i], "-rand") == 0)
62 if ((argv[i+1] != NULL) && (inrand == NULL))
67 else if (strcmp(argv[i], "-base64") == 0)
74 else if (isdigit((unsigned char)argv[i][0]))
78 r = sscanf(argv[i], "%d", &num);
79 if (r == 0 || num < 0)
94 BIO_printf(bio_err, "Usage: rand [options] num\n");
95 BIO_printf(bio_err, "where options are\n");
96 BIO_printf(bio_err, "-out file - write to file\n");
97 BIO_printf(bio_err, "-engine e - use engine e, possibly a hardware device.\n");
98 BIO_printf(bio_err, "-rand file%cfile%c... - seed PRNG from files\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
99 BIO_printf(bio_err, "-base64 - encode output\n");
103 e = setup_engine(bio_err, engine, 0);
105 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
107 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
108 app_RAND_load_files(inrand));
110 out = BIO_new(BIO_s_file());
114 r = BIO_write_filename(out, outfile);
117 r = BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
118 #ifdef OPENSSL_SYS_VMS
120 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
121 out = BIO_push(tmpbio, out);
130 BIO *b64 = BIO_new(BIO_f_base64());
133 out = BIO_push(b64, out);
138 unsigned char buf[4096];
142 if (chunk > sizeof buf)
144 r = RAND_bytes(buf, chunk);
147 BIO_write(out, buf, chunk);
152 app_RAND_write_file(NULL, bio_err);
156 ERR_print_errors(bio_err);