]>
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 * -egd file - PRNG seed from EGD named socket
19 * -base64 - encode output
20 * num - write 'num' bytes
23 int MAIN(int, char **);
25 int MAIN(int argc
, char **argv
)
30 char *inrand
= NULL
,*inegd
=NULL
;
38 if ((bio_err
= BIO_new(BIO_s_file())) != NULL
)
39 BIO_set_fp(bio_err
, stderr
, BIO_NOCLOSE
|BIO_FP_TEXT
);
43 while (!badopt
&& argv
[++i
] != NULL
)
45 if (strcmp(argv
[i
], "-out") == 0)
47 if ((argv
[i
+1] != NULL
) && (outfile
== NULL
))
52 else if (strcmp(argv
[i
], "-rand") == 0)
54 if ((argv
[i
+1] != NULL
) && (inrand
== NULL
))
59 else if (strcmp(argv
[i
], "-egd") == 0)
61 if ((argv
[i
+1] != NULL
) && (inegd
== NULL
))
66 else if (strcmp(argv
[i
], "-base64") == 0)
73 else if (isdigit((unsigned char)argv
[i
][0]))
77 r
= sscanf(argv
[i
], "%d", &num
);
78 if (r
== 0 || num
< 0)
93 BIO_printf(bio_err
, "Usage: rand [options] num\n");
94 BIO_printf(bio_err
, "where options are\n");
95 BIO_printf(bio_err
, "-out file - write to file\n");
96 BIO_printf(bio_err
, "-rand file%cfile%c... - seed PRNG from files\n", LIST_SEPARATOR_CHAR
, LIST_SEPARATOR_CHAR
);
97 BIO_printf(bio_err
, "-egd file - seed PRNG from EGD named socket\n");
98 BIO_printf(bio_err
, "-base64 - encode output\n");
102 app_RAND_load_file(NULL
, bio_err
, (inrand
!= NULL
|| inegd
!= NULL
));
104 BIO_printf(bio_err
,"%ld semi-random bytes loaded\n",
105 app_RAND_load_files(inrand
));
107 BIO_printf(bio_err
,"%ld egd bytes loaded\n",
110 out
= BIO_new(BIO_s_file());
114 r
= BIO_write_filename(out
, outfile
);
116 r
= BIO_set_fp(out
, stdout
, BIO_NOCLOSE
| BIO_FP_TEXT
);
122 BIO
*b64
= BIO_new(BIO_f_base64());
125 out
= BIO_push(b64
, out
);
130 unsigned char buf
[4096];
134 if (chunk
> sizeof buf
)
136 r
= RAND_bytes(buf
, chunk
);
139 BIO_write(out
, buf
, chunk
);
144 app_RAND_write_file(NULL
, bio_err
);
148 ERR_print_errors(bio_err
);