簡體   English   中英

如何創建用戶密碼哈希

[英]How to create user password hash

我們正在轉換代碼以使用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)
    )
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM