Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

randombytes.c 648 B

před 9 roky
před 9 roky
před 9 roky
před 9 roky
1234567891011121314151617181920212223242526272829303132333435363738
  1. /*
  2. This code was taken from the SPHINCS reference implementation and is public domain.
  3. */
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. /* it's really stupid that there isn't a syscall for this */
  9. static int fd = -1;
  10. void randombytes(unsigned char *x, unsigned long long xlen)
  11. {
  12. int i;
  13. if (fd == -1) {
  14. for (;;) {
  15. fd = open("/dev/urandom", O_RDONLY);
  16. if (fd != -1) break;
  17. sleep(1);
  18. }
  19. }
  20. while (xlen > 0) {
  21. if (xlen < 1048576) i = xlen; else i = 1048576;
  22. i = read(fd, x, i);
  23. if (i < 1) {
  24. sleep(1);
  25. continue;
  26. }
  27. x += i;
  28. xlen -= i;
  29. }
  30. }