3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-11 17:55:53 +00:00

Fix for issue 539, adds atomic operation for changing wallet encryption key/password.

This commit is contained in:
Wojciech Langiewicz 2014-12-12 21:29:44 +01:00
parent 4e313661df
commit bc24661254
2 changed files with 49 additions and 0 deletions

View File

@ -1144,6 +1144,28 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha
return getEncryptionType() != EncryptionType.UNENCRYPTED;
}
/** Changes wallet encryption password, this is atomic operation. */
public void changeEncryptionPassword(CharSequence currentPassword, CharSequence newPassword){
keychainLock.lock();
try {
decrypt(currentPassword);
encrypt(newPassword);
} finally {
keychainLock.unlock();
}
}
/** Changes wallet AES encryption key, this is atomic operation. */
public void changeEncryptionKey(KeyCrypter keyCrypter, KeyParameter currentAesKey, KeyParameter newAesKey){
keychainLock.lock();
try {
decrypt(currentAesKey);
encrypt(keyCrypter, newAesKey);
} finally {
keychainLock.unlock();
}
}
//endregion
/******************************************************************************************************************/

View File

@ -1564,6 +1564,33 @@ public class WalletTest extends TestWithWallet {
}
}
@Test
public void changePasswordTest() {
Wallet encryptedWallet = new Wallet(params);
encryptedWallet.encrypt(PASSWORD1);
CharSequence newPassword = "My name is Tom";
encryptedWallet.changeEncryptionPassword(PASSWORD1, newPassword);
assertTrue(encryptedWallet.checkPassword(newPassword));
assertFalse(encryptedWallet.checkPassword(WRONG_PASSWORD));
}
@Test
public void changeAesKeyTest() {
Wallet encryptedWallet = new Wallet(params);
encryptedWallet.encrypt(PASSWORD1);
KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter();
KeyParameter aesKey = keyCrypter.deriveKey(PASSWORD1);
CharSequence newPassword = "My name is Tom";
KeyParameter newAesKey = keyCrypter.deriveKey(newPassword);
encryptedWallet.changeEncryptionKey(keyCrypter, aesKey, newAesKey);
assertTrue(encryptedWallet.checkAESKey(newAesKey));
assertFalse(encryptedWallet.checkAESKey(aesKey));
}
@Test
public void encryptionDecryptionCheckExceptions() throws Exception {
Wallet encryptedWallet = new Wallet(params);