Skip to content

Commit fa83fb3

Browse files
author
MarcoFalke
committed
wallet: Use steady clock to calculate number of derive iterations
1 parent fa2c099 commit fa83fb3

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/wallet/wallet.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -565,13 +565,14 @@ bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase,
565565
return false;
566566
if (Unlock(_vMasterKey))
567567
{
568-
int64_t nStartTime = GetTimeMillis();
568+
constexpr MillisecondsDouble target{100};
569+
auto start{SteadyClock::now()};
569570
crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
570-
pMasterKey.second.nDeriveIterations = static_cast<unsigned int>(pMasterKey.second.nDeriveIterations * (100 / ((double)(GetTimeMillis() - nStartTime))));
571+
pMasterKey.second.nDeriveIterations = static_cast<unsigned int>(pMasterKey.second.nDeriveIterations * target / (SteadyClock::now() - start));
571572

572-
nStartTime = GetTimeMillis();
573+
start = SteadyClock::now();
573574
crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
574-
pMasterKey.second.nDeriveIterations = (pMasterKey.second.nDeriveIterations + static_cast<unsigned int>(pMasterKey.second.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime)))) / 2;
575+
pMasterKey.second.nDeriveIterations = (pMasterKey.second.nDeriveIterations + static_cast<unsigned int>(pMasterKey.second.nDeriveIterations * target / (SteadyClock::now() - start))) / 2;
575576

576577
if (pMasterKey.second.nDeriveIterations < 25000)
577578
pMasterKey.second.nDeriveIterations = 25000;
@@ -768,13 +769,14 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
768769
GetStrongRandBytes(kMasterKey.vchSalt);
769770

770771
CCrypter crypter;
771-
int64_t nStartTime = GetTimeMillis();
772+
constexpr MillisecondsDouble target{100};
773+
auto start{SteadyClock::now()};
772774
crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, 25000, kMasterKey.nDerivationMethod);
773-
kMasterKey.nDeriveIterations = static_cast<unsigned int>(2500000 / ((double)(GetTimeMillis() - nStartTime)));
775+
kMasterKey.nDeriveIterations = static_cast<unsigned int>(25000 * target / (SteadyClock::now() - start));
774776

775-
nStartTime = GetTimeMillis();
777+
start = SteadyClock::now();
776778
crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod);
777-
kMasterKey.nDeriveIterations = (kMasterKey.nDeriveIterations + static_cast<unsigned int>(kMasterKey.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime)))) / 2;
779+
kMasterKey.nDeriveIterations = (kMasterKey.nDeriveIterations + static_cast<unsigned int>(kMasterKey.nDeriveIterations * target / (SteadyClock::now() - start))) / 2;
778780

779781
if (kMasterKey.nDeriveIterations < 25000)
780782
kMasterKey.nDeriveIterations = 25000;

0 commit comments

Comments
 (0)