gpt4 book ai didi

c++ - 如何创建用户密码哈希

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:52:54 27 4
gpt4 key购买 nike

我们正在转换代码以使用 Crypto++ 库。为我们的用户创建一个散列密码是必要的吗?只是想确保我们没有遗漏一些重要的部分。谢谢你

void test_create_hash(void)
{
using namespace CryptoPP;
std::string password = "this is a users password";
unsigned int iterations = 1000000;

AutoSeededRandomPool rng;

SecByteBlock pwsalt(AES::DEFAULT_KEYLENGTH);
rng.GenerateBlock(pwsalt,pwsalt.size());

SecByteBlock derivedkey(AES::DEFAULT_KEYLENGTH);

PKCS5_PBKDF2_HMAC<SHA256> pbkdf;

pbkdf.DeriveKey(
derivedkey, derivedkey.size(),
0x00,
(byte *) password.data(), password.size(),
pwsalt, pwsalt.size(),
iterations
);
std::string salthex;
StringSource ss1(pwsalt,pwsalt.size(),true,
new HexEncoder(
new StringSink(salthex)
)
);
std::string derivedhex;
StringSource ss2(derivedkey,derivedkey.size(),true,
new HexEncoder(
new StringSink(derivedhex)
)
);

cout << "salt stored to database:" << salthex << std::endl;
cout << "password stored to database:" << derivedhex << std::endl;
}

最佳答案

一些评论...

SecByteBlock pwsalt(AES::DEFAULT_KEYLENGTH);
SecByteBlock derivedkey(AES::DEFAULT_KEYLENGTH);

AES 怎么了?也许:

SecByteBlock pwsalt(SHA256::DIGEST_SIZE);
SecByteBlock derivedkey(SHA256::DIGEST_SIZE);

如果您想继续使用 AES,CMAC 可以正常工作。


std::string salthex;
StringSource ss(pwsalt,pwsalt.size(),true,
new HexEncoder(
new StringSink(salthex)
)
);

您不应使用匿名声明。它会给某些 GCC 版本带来麻烦。也就是说,命名您的 StringSource

std::string salthex;
StringSource ss(pwsalt,pwsalt.size(),true,
new HexEncoder(
new StringSink(salthex)
)
);

关于c++ - 如何创建用户密码哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25313584/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com