From d847f0651ab4304129145b55353501636b4e4728 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Tue, 4 Aug 2020 11:03:49 +0200 Subject: [PATCH] gpg: Add level 16 to --gen-random * g10/gpg.c (main): Add that hack. -- This is an yet undocumented hack to allow printing hex encoded random number with gpg. The level is forced to be 1 which is is good for almost all uses. Note that --armor is ignored. Signed-off-by: Werner Koch --- g10/gpg.c | 71 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 29 deletions(-) diff --git a/g10/gpg.c b/g10/gpg.c index ce9197574..7f219e869 100644 --- a/g10/gpg.c +++ b/g10/gpg.c @@ -4877,42 +4877,55 @@ main (int argc, char **argv) case aGenRandom: { - int level = argc ? atoi(*argv):0; - int count = argc > 1 ? atoi(argv[1]): 0; - int endless = !count; + int level = argc ? atoi(*argv):0; + int count = argc > 1 ? atoi(argv[1]): 0; + int endless = !count; + int hexhack = (level == 16); - if( argc < 1 || argc > 2 || level < 0 || level > 2 || count < 0 ) - wrong_args("--gen-random 0|1|2 [count]"); + if (hexhack) + level = 1; - while( endless || count ) { - byte *p; - /* We need a multiple of 3, so that in case of - armored output we get a correct string. No - linefolding is done, as it is best to leave this to - other tools */ - size_t n = !endless && count < 99? count : 99; + if (argc < 1 || argc > 2 || level < 0 || level > 2 || count < 0) + wrong_args ("--gen-random 0|1|2 [count]"); - p = gcry_random_bytes (n, level); + while (endless || count) + { + byte *p; + /* We need a multiple of 3, so that in case of armored + * output we get a correct string. No linefolding is + * done, as it is best to leave this to other tools */ + size_t n = !endless && count < 99? count : 99; + size_t nn; + + p = gcry_random_bytes (n, level); #ifdef HAVE_DOSISH_SYSTEM - setmode ( fileno(stdout), O_BINARY ); + setmode ( fileno(stdout), O_BINARY ); #endif - if (opt.armor) { - char *tmp = make_radix64_string (p, n); - es_fputs (tmp, es_stdout); - xfree (tmp); - if (n%3 == 1) - es_putc ('=', es_stdout); - if (n%3) - es_putc ('=', es_stdout); - } else { - es_fwrite( p, n, 1, es_stdout ); + if (hexhack) + { + for (nn = 0; nn < n; nn++) + es_fprintf (es_stdout, "%02x", p[nn]); } - xfree(p); - if( !endless ) - count -= n; + else if (opt.armor) + { + char *tmp = make_radix64_string (p, n); + es_fputs (tmp, es_stdout); + xfree (tmp); + if (n%3 == 1) + es_putc ('=', es_stdout); + if (n%3) + es_putc ('=', es_stdout); + } + else + { + es_fwrite( p, n, 1, es_stdout ); + } + xfree(p); + if (!endless) + count -= n; } - if (opt.armor) - es_putc ('\n', es_stdout); + if (opt.armor || hexhack) + es_putc ('\n', es_stdout); } break;