10f66f451Sopenharmony_ci/* mcookie - generate a 128-bit random number (used for X "magic cookies")
20f66f451Sopenharmony_ci *
30f66f451Sopenharmony_ci * Copyright 2019 AD Isaac Dunham <ibid.ag@gmail.com>
40f66f451Sopenharmony_ci *
50f66f451Sopenharmony_ci * No standard.
60f66f451Sopenharmony_ci *
70f66f451Sopenharmony_ci * -f and -m are not supported: md5sums of arbitrary files are not a good
80f66f451Sopenharmony_ci * source of entropy, just ask the system for 128 bits and print it.
90f66f451Sopenharmony_ci
100f66f451Sopenharmony_ciUSE_MCOOKIE(NEWTOY(mcookie, "v(verbose)V(version)", TOYFLAG_USR|TOYFLAG_BIN))
110f66f451Sopenharmony_ci
120f66f451Sopenharmony_ciconfig MCOOKIE
130f66f451Sopenharmony_ci  bool "mcookie"
140f66f451Sopenharmony_ci  default y
150f66f451Sopenharmony_ci  help
160f66f451Sopenharmony_ci    usage: mcookie [-vV]
170f66f451Sopenharmony_ci
180f66f451Sopenharmony_ci    Generate a 128-bit strong random number.
190f66f451Sopenharmony_ci
200f66f451Sopenharmony_ci    -v  show entropy source (verbose)
210f66f451Sopenharmony_ci    -V  show version
220f66f451Sopenharmony_ci*/
230f66f451Sopenharmony_ci
240f66f451Sopenharmony_ci#define FOR_mcookie
250f66f451Sopenharmony_ci#include "toys.h"
260f66f451Sopenharmony_ci
270f66f451Sopenharmony_civoid mcookie_main(void)
280f66f451Sopenharmony_ci{
290f66f451Sopenharmony_ci  long long *ll = (void *)toybuf;
300f66f451Sopenharmony_ci
310f66f451Sopenharmony_ci  if (FLAG(V)) return (void)puts("mcookie from toybox");
320f66f451Sopenharmony_ci  xgetrandom(toybuf, 16, 0);
330f66f451Sopenharmony_ci  if (FLAG(v)) fputs("Got 16 bytes from xgetrandom()\n", stderr);
340f66f451Sopenharmony_ci  xprintf("%016llx%06llx\n", ll[0], ll[1]);
350f66f451Sopenharmony_ci}
36