random.c 617 B

12345678910111213141516171819202122232425262728
  1. /*
  2. * ZMap Copyright 2013 Regents of the University of Michigan
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy
  6. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  7. */
  8. #include "random.h"
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <stdint.h>
  12. #include <string.h>
  13. #include <assert.h>
  14. #define RANDSRC "/dev/urandom"
  15. int random_bytes(void *dst, size_t n)
  16. {
  17. FILE *f = fopen(RANDSRC, "rb");
  18. assert(f);
  19. size_t r = fread(dst, n, 1, f);
  20. fclose(f);
  21. if (r < 1) {
  22. return 0;
  23. }
  24. return 1;
  25. }