【问题标题】:How to create user password hash如何创建用户密码哈希
【发布时间】:2014-08-14 17:02:34
【问题描述】:

我们正在将代码转换为使用 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;
}

【问题讨论】:

    标签: c++ passwords crypto++ pbkdf2


    【解决方案1】:

    几厘米……

    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)
        )
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-15
      • 2015-03-22
      • 2014-06-09
      • 2012-04-19
      • 2018-11-27
      • 1970-01-01
      相关资源
      最近更新 更多