mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-15 03:35:52 +00:00
ECKey: fix bug where creation time was lost when encrypting/decrypting.
This commit is contained in:
parent
e397928ec3
commit
822c9011b2
@ -847,7 +847,9 @@ public class ECKey implements Serializable {
|
|||||||
final byte[] privKeyBytes = getPrivKeyBytes();
|
final byte[] privKeyBytes = getPrivKeyBytes();
|
||||||
checkState(privKeyBytes != null, "Private key is not available");
|
checkState(privKeyBytes != null, "Private key is not available");
|
||||||
EncryptedPrivateKey encryptedPrivateKey = keyCrypter.encrypt(privKeyBytes, aesKey);
|
EncryptedPrivateKey encryptedPrivateKey = keyCrypter.encrypt(privKeyBytes, aesKey);
|
||||||
return new ECKey(encryptedPrivateKey, getPubKey(), keyCrypter);
|
ECKey result = new ECKey(encryptedPrivateKey, getPubKey(), keyCrypter);
|
||||||
|
result.setCreationTimeSeconds(creationTimeSeconds);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -869,6 +871,7 @@ public class ECKey implements Serializable {
|
|||||||
ECKey key = new ECKey(new BigInteger(1, unencryptedPrivateKey), null, isCompressed());
|
ECKey key = new ECKey(new BigInteger(1, unencryptedPrivateKey), null, isCompressed());
|
||||||
if (!Arrays.equals(key.getPubKey(), getPubKey()))
|
if (!Arrays.equals(key.getPubKey(), getPubKey()))
|
||||||
throw new KeyCrypterException("Provided AES key is wrong");
|
throw new KeyCrypterException("Provided AES key is wrong");
|
||||||
|
key.setCreationTimeSeconds(creationTimeSeconds);
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,6 +50,7 @@ import java.util.concurrent.Callable;
|
|||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
import static com.google.bitcoin.core.Utils.reverseBytes;
|
import static com.google.bitcoin.core.Utils.reverseBytes;
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class ECKeyTest {
|
public class ECKeyTest {
|
||||||
@ -253,55 +254,25 @@ public class ECKeyTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUnencryptedCreate() throws Exception {
|
public void testUnencryptedCreate() throws Exception {
|
||||||
ECKey unencryptedKey = new ECKey();
|
Utils.rollMockClock(0);
|
||||||
|
ECKey key = new ECKey();
|
||||||
// The key should initially be unencrypted.
|
long time = key.getCreationTimeSeconds();
|
||||||
assertTrue(!unencryptedKey.isEncrypted());
|
assertNotEquals(0, time);
|
||||||
|
assertTrue(!key.isEncrypted());
|
||||||
// Copy the private key bytes for checking later.
|
byte[] originalPrivateKeyBytes = key.getPrivKeyBytes();
|
||||||
byte[] originalPrivateKeyBytes = new byte[32];
|
ECKey encryptedKey = key.encrypt(keyCrypter, keyCrypter.deriveKey(PASSWORD1));
|
||||||
System.arraycopy(unencryptedKey.getPrivKeyBytes(), 0, originalPrivateKeyBytes, 0, 32);
|
assertEquals(time, encryptedKey.getCreationTimeSeconds());
|
||||||
log.info("Original private key = " + Utils.bytesToHexString(originalPrivateKeyBytes));
|
assertTrue(encryptedKey.isEncrypted());
|
||||||
|
assertNull(encryptedKey.getPrivKeyBytes());
|
||||||
// Encrypt the key.
|
key = encryptedKey.decrypt(keyCrypter, keyCrypter.deriveKey(PASSWORD1));
|
||||||
ECKey encryptedKey = unencryptedKey.encrypt(keyCrypter, keyCrypter.deriveKey(PASSWORD1));
|
assertTrue(!key.isEncrypted());
|
||||||
|
assertArrayEquals(originalPrivateKeyBytes, key.getPrivKeyBytes());
|
||||||
// The key should now be encrypted.
|
|
||||||
assertTrue("Key is not encrypted but it should be", encryptedKey.isEncrypted());
|
|
||||||
|
|
||||||
// The unencrypted private key bytes of the encrypted keychain
|
|
||||||
// should be null or all be blank.
|
|
||||||
byte[] privateKeyBytes = encryptedKey.getPrivKeyBytes();
|
|
||||||
if (privateKeyBytes != null) {
|
|
||||||
for (int i = 0; i < privateKeyBytes.length; i++) {
|
|
||||||
assertEquals("Byte " + i + " of the private key was not zero but should be", 0, privateKeyBytes[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decrypt the key.
|
|
||||||
unencryptedKey = encryptedKey.decrypt(keyCrypter, keyCrypter.deriveKey(PASSWORD1));
|
|
||||||
|
|
||||||
// The key should be unencrypted
|
|
||||||
assertTrue("Key is not unencrypted but it should be", !unencryptedKey.isEncrypted());
|
|
||||||
|
|
||||||
// The reborn unencrypted private key bytes should match the
|
|
||||||
// original private key.
|
|
||||||
privateKeyBytes = unencryptedKey.getPrivKeyBytes();
|
|
||||||
log.info("Reborn decrypted private key = " + Utils.bytesToHexString(privateKeyBytes));
|
|
||||||
|
|
||||||
for (int i = 0; i < privateKeyBytes.length; i++) {
|
|
||||||
assertEquals("Byte " + i + " of the private key did not match the original", originalPrivateKeyBytes[i],
|
|
||||||
privateKeyBytes[i]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEncryptedCreate() throws Exception {
|
public void testEncryptedCreate() throws Exception {
|
||||||
ECKey unencryptedKey = new ECKey();
|
ECKey unencryptedKey = new ECKey();
|
||||||
|
byte[] originalPrivateKeyBytes = checkNotNull(unencryptedKey.getPrivKeyBytes());
|
||||||
// Copy the private key bytes for checking later.
|
|
||||||
byte[] originalPrivateKeyBytes = new byte[32];
|
|
||||||
System.arraycopy(unencryptedKey.getPrivKeyBytes(), 0, originalPrivateKeyBytes, 0, 32);
|
|
||||||
log.info("Original private key = " + Utils.bytesToHexString(originalPrivateKeyBytes));
|
log.info("Original private key = " + Utils.bytesToHexString(originalPrivateKeyBytes));
|
||||||
|
|
||||||
EncryptedPrivateKey encryptedPrivateKey = keyCrypter.encrypt(unencryptedKey.getPrivKeyBytes(), keyCrypter.deriveKey(PASSWORD1));
|
EncryptedPrivateKey encryptedPrivateKey = keyCrypter.encrypt(unencryptedKey.getPrivKeyBytes(), keyCrypter.deriveKey(PASSWORD1));
|
||||||
@ -320,17 +291,8 @@ public class ECKeyTest {
|
|||||||
|
|
||||||
// Decrypt the key.
|
// Decrypt the key.
|
||||||
ECKey rebornUnencryptedKey = encryptedKey.decrypt(keyCrypter, keyCrypter.deriveKey(PASSWORD1));
|
ECKey rebornUnencryptedKey = encryptedKey.decrypt(keyCrypter, keyCrypter.deriveKey(PASSWORD1));
|
||||||
|
assertTrue(!rebornUnencryptedKey.isEncrypted());
|
||||||
// The key should be unencrypted
|
assertArrayEquals(originalPrivateKeyBytes, rebornUnencryptedKey.getPrivKeyBytes());
|
||||||
assertTrue("Key is not unencrypted but it should be", !rebornUnencryptedKey.isEncrypted());
|
|
||||||
|
|
||||||
// The reborn unencrypted private key bytes should match the original private key.
|
|
||||||
privateKeyBytes = rebornUnencryptedKey.getPrivKeyBytes();
|
|
||||||
log.info("Reborn decrypted private key = " + Utils.bytesToHexString(privateKeyBytes));
|
|
||||||
|
|
||||||
for (int i = 0; i < privateKeyBytes.length; i++) {
|
|
||||||
assertEquals("Byte " + i + " of the private key did not match the original", originalPrivateKeyBytes[i], privateKeyBytes[i]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user