xref: /third_party/toybox/toys/other/mcookie.c (revision 0f66f451)
1/* mcookie - generate a 128-bit random number (used for X "magic cookies")
2 *
3 * Copyright 2019 AD Isaac Dunham <ibid.ag@gmail.com>
4 *
5 * No standard.
6 *
7 * -f and -m are not supported: md5sums of arbitrary files are not a good
8 * source of entropy, just ask the system for 128 bits and print it.
9
10USE_MCOOKIE(NEWTOY(mcookie, "v(verbose)V(version)", TOYFLAG_USR|TOYFLAG_BIN))
11
12config MCOOKIE
13  bool "mcookie"
14  default y
15  help
16    usage: mcookie [-vV]
17
18    Generate a 128-bit strong random number.
19
20    -v  show entropy source (verbose)
21    -V  show version
22*/
23
24#define FOR_mcookie
25#include "toys.h"
26
27void mcookie_main(void)
28{
29  long long *ll = (void *)toybuf;
30
31  if (FLAG(V)) return (void)puts("mcookie from toybox");
32  xgetrandom(toybuf, 16, 0);
33  if (FLAG(v)) fputs("Got 16 bytes from xgetrandom()\n", stderr);
34  xprintf("%016llx%06llx\n", ll[0], ll[1]);
35}
36