diff options
author | Werner Koch <wk@gnupg.org> | 2020-08-04 11:03:49 +0200 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 2020-08-05 16:53:34 +0200 |
commit | d847f0651ab4304129145b55353501636b4e4728 (patch) | |
tree | 7f5f362c6c302c51bd2e4f4a5f562099d2265fcf | |
parent | sm: Also show the SHA-256 fingerprint. (diff) | |
download | gnupg2-d847f0651ab4304129145b55353501636b4e4728.tar.xz gnupg2-d847f0651ab4304129145b55353501636b4e4728.zip |
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 <wk@gnupg.org>
-rw-r--r-- | g10/gpg.c | 77 |
1 files changed, 45 insertions, 32 deletions
@@ -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; - - if( argc < 1 || argc > 2 || level < 0 || level > 2 || count < 0 ) - wrong_args("--gen-random 0|1|2 [count]"); - - 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; - - p = gcry_random_bytes (n, level); + int level = argc ? atoi(*argv):0; + int count = argc > 1 ? atoi(argv[1]): 0; + int endless = !count; + int hexhack = (level == 16); + + if (hexhack) + level = 1; + + if (argc < 1 || argc > 2 || level < 0 || level > 2 || count < 0) + wrong_args ("--gen-random 0|1|2 [count]"); + + 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]); + } + 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; + xfree(p); + if (!endless) + count -= n; } - if (opt.armor) - es_putc ('\n', es_stdout); + if (opt.armor || hexhack) + es_putc ('\n', es_stdout); } break; |