Advertisement
Guest User

Untitled

a guest
Apr 9th, 2014
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.71 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <stdint.h>
  6.  
  7. #ifdef WIN32
  8. #include <windows.h>
  9. #include <wincrypt.h>
  10. #endif
  11.  
  12. int libscrypt_salt_gen(uint8_t *rand, size_t len)
  13. {
  14.     unsigned char buf[len];
  15.  
  16. #ifdef WIN32
  17.     static HCRYPTPROV provider;
  18.     if (!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
  19.     {
  20.         return -1;
  21.     }
  22.  
  23.     if (!CryptGenRandom(provider, len, buf))
  24.     {
  25.         return -1;
  26.     }
  27. #endif
  28. #ifdef UNIX
  29.     int urandom = open("/dev/urandom", O_RDONLY);
  30.  
  31.     if (urandom < 0)
  32.     {
  33.         return -1;
  34.     }
  35.  
  36.     ssize_t result = read(urandom, buf, len);
  37.  
  38.     if (result < 0)
  39.     {
  40.         return -1;
  41.     }
  42. #endif
  43.     memcpy(rand, buf, len);
  44.     return 0;
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement