【发布时间】:2018-08-08 13:50:52
【问题描述】:
我使用这个 BCrypt 库生成一个带盐的哈希:https://github.com/rg3/libbcrypt
问题出在bcrypt.c (int bcrypt_gensalt)。 open("/dev/urandom", O_RDONLY) 在 Windows 上不起作用。我尝试了以下方法:
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;
}
但结果是:
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
盐不会正确生成。
【问题讨论】:
-
谢谢,但没有。在我在这里问之前,我在谷歌和论坛上呆了 5 天^^
-
你的代码和你的输出根本不匹配......
-
您可能应该添加代码,(a) 声明您传入的变量以接收盐; (b) 对该函数的调用以及 (c) 如何打印返回的盐。如果还没有这样做,您应该打印此函数的返回值。更重要的是,除非我读错了,否则上面的函数看起来总是会在它到达
/* Generate salt. */之前返回... -
@UKMonkey 我的代码是我上面写的库的一部分。但我无法在 Windows 上使用 lib 生成盐。那是我的问题^^
标签: c++ windows visual-studio cng