use of com.quorum.tessera.config.KeyData in project tessera by ConsenSys.
the class KeyDataUtilTest method marshalDirectKeyPair.
@Test
public void marshalDirectKeyPair() {
DirectKeyPair directKeyPair = new DirectKeyPair("PUBLIC_KEY", "PRIVATE_KEY");
KeyData result = KeyDataUtil.marshal(directKeyPair);
assertThat(result.getPublicKey()).isEqualTo("PUBLIC_KEY");
assertThat(result.getPrivateKey()).isEqualTo("PRIVATE_KEY");
}
use of com.quorum.tessera.config.KeyData in project tessera by ConsenSys.
the class KeyDataUtilTest method isLocked.
@Test
public void isLocked() {
KeyData keyData = new KeyData();
keyData.setPublicKey("PUBLIC_KEY");
KeyDataConfig keyDataConfig = new KeyDataConfig(mock(PrivateKeyData.class), PrivateKeyType.LOCKED);
keyData.setConfig(keyDataConfig);
assertThat(KeyDataUtil.isLocked(keyData)).isTrue();
keyData.setConfig(null);
assertThat(KeyDataUtil.isLocked(keyData)).isFalse();
keyData.setConfig(new KeyDataConfig());
assertThat(KeyDataUtil.isLocked(keyData)).isFalse();
}
use of com.quorum.tessera.config.KeyData in project tessera by ConsenSys.
the class KeyDataUtilTest method marshalInlineKeypair.
@Test
public void marshalInlineKeypair() {
KeyEncryptor keyEncryptor = mock(KeyEncryptor.class);
KeyDataConfig keyDataConfig = mock(KeyDataConfig.class);
InlineKeypair keyPair = new InlineKeypair("PUBLIC_KEY", keyDataConfig, keyEncryptor);
KeyData result = KeyDataUtil.marshal(keyPair);
assertThat(result.getPublicKey()).isEqualTo("PUBLIC_KEY");
assertThat(result.getConfig()).isSameAs(keyDataConfig);
}
use of com.quorum.tessera.config.KeyData in project tessera by ConsenSys.
the class KeyDataMarshallerTest method defaultMarshal.
@Test
public void defaultMarshal() {
KeyDataMarshaller k = new DefaultKeyDataMarshaller();
DirectKeyPair configKeyPair = new DirectKeyPair("PUBLIC", "PRIVATE");
KeyData keyData = k.marshal(configKeyPair);
assertThat(keyData).isNotNull();
assertThat(keyData.getPublicKey()).isEqualTo("PUBLIC");
assertThat(keyData.getPrivateKey()).isEqualTo("PRIVATE");
}
use of com.quorum.tessera.config.KeyData in project tessera by ConsenSys.
the class FileKeyGenerator method generate.
@Override
public FilesystemKeyPair generate(final String filename, final ArgonOptions encryptionOptions, final KeyVaultOptions keyVaultOptions) {
final char[] password = this.passwordReader.requestUserPassword();
final KeyPair generated = this.encryptor.generateNewKeys();
final String publicKeyBase64 = Base64.getEncoder().encodeToString(generated.getPublicKey().getKeyBytes());
final KeyData finalKeys = new KeyData();
final KeyDataConfig keyDataConfig;
if (password.length > 0) {
final PrivateKeyData encryptedPrivateKey = this.keyEncryptor.encryptPrivateKey(generated.getPrivateKey(), password, encryptionOptions);
keyDataConfig = new KeyDataConfig(new PrivateKeyData(null, encryptedPrivateKey.getSnonce(), encryptedPrivateKey.getAsalt(), encryptedPrivateKey.getSbox(), encryptedPrivateKey.getArgonOptions()), LOCKED);
LOGGER.info("Newly generated private key has been encrypted");
} else {
String keyData = Base64.getEncoder().encodeToString(generated.getPrivateKey().getKeyBytes());
keyDataConfig = new KeyDataConfig(new PrivateKeyData(keyData, null, null, null, null), UNLOCKED);
}
finalKeys.setConfig(keyDataConfig);
finalKeys.setPrivateKey(generated.getPrivateKey().encodeToBase64());
finalKeys.setPublicKey(publicKeyBase64);
final String privateKeyJson = JaxbUtil.marshalToString(finalKeys.getConfig());
final Path resolvedPath = Paths.get(filename).toAbsolutePath();
final Path parentPath;
if (EMPTY_FILENAME.equals(filename)) {
parentPath = resolvedPath;
} else {
parentPath = resolvedPath.getParent();
}
final Path publicKeyPath = parentPath.resolve(filename + ".pub");
final Path privateKeyPath = parentPath.resolve(filename + ".key");
IOCallback.execute(() -> Files.write(publicKeyPath, publicKeyBase64.getBytes(UTF_8), CREATE_NEW));
IOCallback.execute(() -> Files.write(privateKeyPath, privateKeyJson.getBytes(UTF_8), CREATE_NEW));
LOGGER.info("Saved public key to {}", publicKeyPath.toAbsolutePath().toString());
LOGGER.info("Saved private key to {}", privateKeyPath.toAbsolutePath().toString());
final FilesystemKeyPair keyPair = new FilesystemKeyPair(publicKeyPath, privateKeyPath, keyEncryptor);
keyPair.withPassword(password);
return keyPair;
}
Aggregations