Advertisement
Guest User

Untitled

a guest
Apr 9th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.69 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 *salt, size_t len)
  13. {
  14.     unsigned char buf[len];
  15. #ifdef _WIN32
  16.     static HCRYPTPROV provider;
  17.     if (!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
  18.     {
  19.         return -1;
  20.     }
  21.  
  22.     if (!CryptGenRandom(provider, len, buf))
  23.     {
  24.         return -1;
  25.     }
  26. #else
  27.     int urandom = open("/dev/urandom", O_RDONLY);
  28.  
  29.     if (urandom < 0)
  30.     {
  31.         return -1;
  32.     }
  33.  
  34.     ssize_t result = read(urandom, buf, len);
  35.  
  36.     if (result < 0)
  37.     {
  38.         return -1;
  39.     }
  40. #endif
  41.     memcpy(salt, buf, len);
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement