I used this BCrypt lib to generate a hash with salt: https://github.com/rg3/libbcrypt
The problem is in bcrypt.c
(int bcrypt_gensalt
). open("/dev/urandom", O_RDONLY)
does not work on Windows. I have tried the following:
int bcrypt_gensalt(int factor, char salt[BCRYPT_HASHSIZE]) {
int fd;
unsigned char input[RANDBYTES];
int workf;
char *aux;
HCRYPTPROV hCryptProv;
if (CryptAcquireContext(
&hCryptProv,
NULL,
(LPCSTR)"Microsoft Base Cryptographic Provider v1.0",
PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT)) {
if (CryptGenRandom(
hCryptProv,
RANDBYTES,
input)) {
if (CryptReleaseContext(hCryptProv, 0)) {
return 0;
}
else {
printf("Error during CryptReleaseContext.\n");
return 4;
}
}
else {
if (CryptReleaseContext(hCryptProv, 0)) {
printf("Error during CryptGenRandom.\n");
return 2;
}
else {
printf("Error during CryptReleaseContext.\n");
return 3;
}
}
}
else {
printf("Error during CryptAcquireContext!\n");
return 1;
}
/* Generate salt. */
workf = (factor < 4 || factor > 31)?12:factor;
aux = crypt_gensalt_rn("$2a$", workf, input, RANDBYTES,
salt, BCRYPT_HASHSIZE);
return (aux == NULL)?5:0;
}
But the result is:
Generated salt:
Hashed password: *0
Time taken: 0.000000 seconds
First hash check: OK
Second hash check: OK
First hash check with bcrypt_checkpw: OK
Time taken: 0.060000 seconds
Second hash check with bcrypt_checkpw: OK
Time taken: 0.060000 seconds
The salt will be not generated correctly.