Нет описания

entropy.c 1.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #ifdef _WIN32
  2. #include <windows.h>
  3. #else
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #endif
  8. #if defined(__cplusplus)
  9. extern "C"
  10. {
  11. #endif
  12. #ifdef _WIN32
  13. int entropy_fun(unsigned char buf[], unsigned int len)
  14. {
  15. HCRYPTPROV provider;
  16. unsigned __int64 pentium_tsc[1];
  17. unsigned int i;
  18. int result = 0;
  19. if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
  20. {
  21. result = CryptGenRandom(provider, len, buf);
  22. CryptReleaseContext(provider, 0);
  23. if (result)
  24. return len;
  25. }
  26. QueryPerformanceCounter((LARGE_INTEGER *)pentium_tsc);
  27. for(i = 0; i < 8 && i < len; ++i)
  28. buf[i] = ((unsigned char*)pentium_tsc)[i];
  29. return i;
  30. }
  31. #else
  32. int entropy_fun(unsigned char buf[], unsigned int len)
  33. {
  34. int frand = open("/dev/random", O_RDONLY);
  35. int rlen = 0;
  36. if (frand != -1)
  37. {
  38. rlen = (int)read(frand, buf, len);
  39. close(frand);
  40. }
  41. return rlen;
  42. }
  43. #endif
  44. #if defined(__cplusplus)
  45. }
  46. #endif