]>
git.ipfire.org Git - thirdparty/openssl.git/blob - apps/rand.c
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
)
37 if ((bio_err
= BIO_new(BIO_s_file())) != NULL
)
38 BIO_set_fp(bio_err
, stderr
, BIO_NOCLOSE
|BIO_FP_TEXT
);
42 while (!badopt
&& argv
[++i
] != NULL
)
44 if (strcmp(argv
[i
], "-out") == 0)
46 if ((argv
[i
+1] != NULL
) && (outfile
== NULL
))
51 else if (strcmp(argv
[i
], "-rand") == 0)
53 if ((argv
[i
+1] != NULL
) && (inrand
== NULL
))
58 else if (strcmp(argv
[i
], "-base64") == 0)
65 else if (isdigit((unsigned char)argv
[i
][0]))
69 r
= sscanf(argv
[i
], "%d", &num
);
70 if (r
== 0 || num
< 0)
85 BIO_printf(bio_err
, "Usage: rand [options] num\n");
86 BIO_printf(bio_err
, "where options are\n");
87 BIO_printf(bio_err
, "-out file - write to file\n");
88 BIO_printf(bio_err
, "-rand file%cfile%c... - seed PRNG from files\n", LIST_SEPARATOR_CHAR
, LIST_SEPARATOR_CHAR
);
89 BIO_printf(bio_err
, "-base64 - encode output\n");
93 app_RAND_load_file(NULL
, bio_err
, (inrand
!= NULL
));
95 BIO_printf(bio_err
,"%ld semi-random bytes loaded\n",
96 app_RAND_load_files(inrand
));
98 out
= BIO_new(BIO_s_file());
102 r
= BIO_write_filename(out
, outfile
);
104 r
= BIO_set_fp(out
, stdout
, BIO_NOCLOSE
| BIO_FP_TEXT
);
110 BIO
*b64
= BIO_new(BIO_f_base64());
113 out
= BIO_push(b64
, out
);
118 unsigned char buf
[4096];
122 if (chunk
> sizeof buf
)
124 r
= RAND_bytes(buf
, chunk
);
127 BIO_write(out
, buf
, chunk
);
132 app_RAND_write_file(NULL
, bio_err
);
136 ERR_print_errors(bio_err
);