| 475 | } |
| 476 | |
| 477 | bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase) |
| 478 | { |
| 479 | if (IsCrypted()) |
| 480 | return false; |
| 481 | |
| 482 | CKeyingMaterial vMasterKey; |
| 483 | RandAddSeedPerfmon(); |
| 484 | |
| 485 | vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE); |
| 486 | GetRandBytes(&vMasterKey[0], WALLET_CRYPTO_KEY_SIZE); |
| 487 | |
| 488 | CMasterKey kMasterKey; |
| 489 | RandAddSeedPerfmon(); |
| 490 | |
| 491 | kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE); |
| 492 | GetRandBytes(&kMasterKey.vchSalt[0], WALLET_CRYPTO_SALT_SIZE); |
| 493 | |
| 494 | CCrypter crypter; |
| 495 | int64_t nStartTime = GetTimeMillis(); |
| 496 | crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, 25000, kMasterKey.nDerivationMethod); |
| 497 | kMasterKey.nDeriveIterations = 2500000 / ((double)(GetTimeMillis() - nStartTime)); |
| 498 | |
| 499 | nStartTime = GetTimeMillis(); |
| 500 | crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod); |
| 501 | kMasterKey.nDeriveIterations = (kMasterKey.nDeriveIterations + kMasterKey.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2; |
| 502 | |
| 503 | if (kMasterKey.nDeriveIterations < 25000) |
| 504 | kMasterKey.nDeriveIterations = 25000; |
| 505 | |
| 506 | LogPrintf("Encrypting Wallet with an nDeriveIterations of %i\n", kMasterKey.nDeriveIterations); |
| 507 | |
| 508 | if (!crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod)) |
| 509 | return false; |
| 510 | if (!crypter.Encrypt(vMasterKey, kMasterKey.vchCryptedKey)) |
| 511 | return false; |
| 512 | |
| 513 | { |
| 514 | LOCK(cs_wallet); |
| 515 | mapMasterKeys[++nMasterKeyMaxID] = kMasterKey; |
| 516 | if (fFileBacked) |
| 517 | { |
| 518 | assert(!pwalletdbEncryption); |
| 519 | pwalletdbEncryption = new CWalletDB(strWalletFile); |
| 520 | if (!pwalletdbEncryption->TxnBegin()) { |
| 521 | delete pwalletdbEncryption; |
| 522 | pwalletdbEncryption = NULL; |
| 523 | return false; |
| 524 | } |
| 525 | pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey); |
| 526 | } |
| 527 | |
| 528 | if (!EncryptKeys(vMasterKey)) |
| 529 | { |
| 530 | if (fFileBacked) { |
| 531 | pwalletdbEncryption->TxnAbort(); |
| 532 | delete pwalletdbEncryption; |
| 533 | } |
| 534 | // We now probably have half of our keys encrypted in memory, and half not... |
no test coverage detected