X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=apps%2Frand.c;h=790e79592c1ea92e91def9d89358d2b2515e826d;hb=69e9c69e70a4ef1a9a2d68fdc4ef85b96a5a1800;hp=63724bc730250236200d08d06b95ab124863acac;hpb=0b13e9f055d3f7be066dc2e89fc9f9822b12eca7;p=oweals%2Fopenssl.git diff --git a/apps/rand.c b/apps/rand.c index 63724bc730..790e79592c 100644 --- a/apps/rand.c +++ b/apps/rand.c @@ -68,7 +68,8 @@ /* -out file - write to file * -rand file:file - PRNG seed files - * -base64 - encode output + * -base64 - base64 encode output + * -hex - hex encode output * num - write 'num' bytes */ @@ -76,14 +77,12 @@ int MAIN(int, char **); int MAIN(int argc, char **argv) { -#ifndef OPENSSL_NO_ENGINE - ENGINE *e = NULL; -#endif int i, r, ret = 1; int badopt; char *outfile = NULL; char *inrand = NULL; int base64 = 0; + int hex = 0; BIO *out = NULL; int num = -1; #ifndef OPENSSL_NO_ENGINE @@ -133,6 +132,13 @@ int MAIN(int argc, char **argv) else badopt = 1; } + else if (strcmp(argv[i], "-hex") == 0) + { + if (!hex) + hex = 1; + else + badopt = 1; + } else if (isdigit((unsigned char)argv[i][0])) { if (num < 0) @@ -148,6 +154,9 @@ int MAIN(int argc, char **argv) badopt = 1; } + if (hex && base64) + badopt = 1; + if (num < 0) badopt = 1; @@ -160,12 +169,13 @@ int MAIN(int argc, char **argv) BIO_printf(bio_err, "-engine e - use engine e, possibly a hardware device.\n"); #endif BIO_printf(bio_err, "-rand file%cfile%c... - seed PRNG from files\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR); - BIO_printf(bio_err, "-base64 - encode output\n"); + BIO_printf(bio_err, "-base64 - base64 encode output\n"); + BIO_printf(bio_err, "-hex - hex encode output\n"); goto err; } #ifndef OPENSSL_NO_ENGINE - e = setup_engine(bio_err, engine, 0); + setup_engine(bio_err, engine, 0); #endif app_RAND_load_file(NULL, bio_err, (inrand != NULL)); @@ -205,15 +215,23 @@ int MAIN(int argc, char **argv) int chunk; chunk = num; - if (chunk > sizeof buf) + if (chunk > (int)sizeof(buf)) chunk = sizeof buf; r = RAND_bytes(buf, chunk); if (r <= 0) goto err; - BIO_write(out, buf, chunk); + if (!hex) + BIO_write(out, buf, chunk); + else + { + for (i = 0; i < chunk; i++) + BIO_printf(out, "%02x", buf[i]); + } num -= chunk; } - BIO_flush(out); + if (hex) + BIO_puts(out, "\n"); + (void)BIO_flush(out); app_RAND_write_file(NULL, bio_err); ret = 0;