]>
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>
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");
106 if((e
= ENGINE_by_id(engine
)) == NULL
)
108 BIO_printf(bio_err
,"invalid engine \"%s\"\n",
112 if(!ENGINE_set_default(e
, ENGINE_METHOD_ALL
))
114 BIO_printf(bio_err
,"can't use that engine\n");
117 BIO_printf(bio_err
,"engine \"%s\" set.\n", engine
);
118 /* Free our "structural" reference. */
122 app_RAND_load_file(NULL
, bio_err
, (inrand
!= NULL
));
124 BIO_printf(bio_err
,"%ld semi-random bytes loaded\n",
125 app_RAND_load_files(inrand
));
127 out
= BIO_new(BIO_s_file());
131 r
= BIO_write_filename(out
, outfile
);
134 r
= BIO_set_fp(out
, stdout
, BIO_NOCLOSE
| BIO_FP_TEXT
);
137 BIO
*tmpbio
= BIO_new(BIO_f_linebuffer());
138 out
= BIO_push(tmpbio
, out
);
147 BIO
*b64
= BIO_new(BIO_f_base64());
150 out
= BIO_push(b64
, out
);
155 unsigned char buf
[4096];
159 if (chunk
> sizeof buf
)
161 r
= RAND_bytes(buf
, chunk
);
164 BIO_write(out
, buf
, chunk
);
169 app_RAND_write_file(NULL
, bio_err
);
173 ERR_print_errors(bio_err
);