version 1.16, 2023/03/05 13:12:53 |
version 1.17, 2023/03/06 14:32:06 |
|
|
int base64; |
int base64; |
int hex; |
int hex; |
char *outfile; |
char *outfile; |
} rand_config; |
} cfg; |
|
|
static const struct option rand_options[] = { |
static const struct option rand_options[] = { |
{ |
{ |
.name = "base64", |
.name = "base64", |
.desc = "Perform base64 encoding on output", |
.desc = "Perform base64 encoding on output", |
.type = OPTION_FLAG, |
.type = OPTION_FLAG, |
.opt.flag = &rand_config.base64, |
.opt.flag = &cfg.base64, |
}, |
}, |
{ |
{ |
.name = "hex", |
.name = "hex", |
.desc = "Hexadecimal output", |
.desc = "Hexadecimal output", |
.type = OPTION_FLAG, |
.type = OPTION_FLAG, |
.opt.flag = &rand_config.hex, |
.opt.flag = &cfg.hex, |
}, |
}, |
{ |
{ |
.name = "out", |
.name = "out", |
.argname = "file", |
.argname = "file", |
.desc = "Write to the given file instead of standard output", |
.desc = "Write to the given file instead of standard output", |
.type = OPTION_ARG, |
.type = OPTION_ARG, |
.opt.arg = &rand_config.outfile, |
.opt.arg = &cfg.outfile, |
}, |
}, |
{NULL}, |
{NULL}, |
}; |
}; |
|
|
exit(1); |
exit(1); |
} |
} |
|
|
memset(&rand_config, 0, sizeof(rand_config)); |
memset(&cfg, 0, sizeof(cfg)); |
|
|
if (options_parse(argc, argv, rand_options, &num_bytes, NULL) != 0) { |
if (options_parse(argc, argv, rand_options, &num_bytes, NULL) != 0) { |
rand_usage(); |
rand_usage(); |
|
|
} else |
} else |
badopt = 1; |
badopt = 1; |
|
|
if (rand_config.hex && rand_config.base64) |
if (cfg.hex && cfg.base64) |
badopt = 1; |
badopt = 1; |
|
|
if (badopt) { |
if (badopt) { |
|
|
out = BIO_new(BIO_s_file()); |
out = BIO_new(BIO_s_file()); |
if (out == NULL) |
if (out == NULL) |
goto err; |
goto err; |
if (rand_config.outfile != NULL) |
if (cfg.outfile != NULL) |
r = BIO_write_filename(out, rand_config.outfile); |
r = BIO_write_filename(out, cfg.outfile); |
else |
else |
r = BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT); |
r = BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT); |
if (r <= 0) |
if (r <= 0) |
goto err; |
goto err; |
if (rand_config.base64) { |
if (cfg.base64) { |
BIO *b64 = BIO_new(BIO_f_base64()); |
BIO *b64 = BIO_new(BIO_f_base64()); |
if (b64 == NULL) |
if (b64 == NULL) |
goto err; |
goto err; |
|
|
if (chunk > (int) sizeof(buf)) |
if (chunk > (int) sizeof(buf)) |
chunk = sizeof(buf); |
chunk = sizeof(buf); |
arc4random_buf(buf, chunk); |
arc4random_buf(buf, chunk); |
if (rand_config.hex) { |
if (cfg.hex) { |
for (i = 0; i < chunk; i++) |
for (i = 0; i < chunk; i++) |
BIO_printf(out, "%02x", buf[i]); |
BIO_printf(out, "%02x", buf[i]); |
} else |
} else |
|
|
num -= chunk; |
num -= chunk; |
} |
} |
|
|
if (rand_config.hex) |
if (cfg.hex) |
BIO_puts(out, "\n"); |
BIO_puts(out, "\n"); |
(void) BIO_flush(out); |
(void) BIO_flush(out); |
|
|