mcookie.c 861 B

1234567891011121314151617181920212223242526272829303132333435
  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. USE_MCOOKIE(NEWTOY(mcookie, "v(verbose)V(version)", TOYFLAG_USR|TOYFLAG_BIN))
  10. config MCOOKIE
  11. bool "mcookie"
  12. default y
  13. help
  14. usage: mcookie [-vV]
  15. Generate a 128-bit strong random number.
  16. -v show entropy source (verbose)
  17. -V show version
  18. */
  19. #define FOR_mcookie
  20. #include "toys.h"
  21. void mcookie_main(void)
  22. {
  23. long long *ll = (void *)toybuf;
  24. if (FLAG(V)) return (void)puts("mcookie from toybox");
  25. xgetrandom(toybuf, 16, 0);
  26. if (FLAG(v)) fputs("Got 16 bytes from xgetrandom()\n", stderr);
  27. xprintf("%016llx%06llx\n", ll[0], ll[1]);
  28. }