【发布时间】:2013-12-22 11:57:20
【问题描述】:
我需要一个示例代码,向我展示如何使用 openssl 库用盐对字符串进行哈希处理。 我应该提一下,我知道如何在不加盐的情况下做到这一点,正如您在这段代码中看到的那样:
#include <openssl/sha.h>
bool simpleSHA256(void* input, unsigned long length, unsigned char* md)
{
SHA256_CTX context;
if(!SHA256_Init(&context))
return false;
if(!SHA256_Update(&context, (unsigned char*)input, length))
return false;
if(!SHA256_Final(md, &context))
return false;
return true;
}
我的问题是关于在哈希函数中添加盐,类似这样,但使用 openssl 库:
char salt[2]; /* Salt for the crypt() function */
const char *salt_chars = "abcdefghijklmnopqrstuvwxyz" /* Range of character supported */
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" /* as a value for salt in crypt() */
"0123456789";
char password1[BUFSIZ], *buf;
/* Build salt */
srand(time(NULL));
salt[0] = salt_chars[rand() % 62];
salt[1] = salt_chars[rand() % 62];
buf = crypt(password, salt);
谢谢
【问题讨论】:
-
看看 openssl 测试代码( git clone ... 然后 cd test )。看看wiki.openssl.org。然后知道加盐数据意味着在 salt.data 上应用哈希,其中 .是连接,创建自己的盐应该不难。 HMAC 等其他一些键控功能也被大量用于保护密码,例如 PBKDF2 (security.stackexchange.com/questions/29951/…)。