use of io.nem.symbol.core.crypto.PublicKey in project nem2-sdk-java by nemtech.
the class Ed25519BlockCipherVectorTester method testEncrypt.
@ParameterizedTest
@MethodSource("cipherVector")
void testEncrypt(String privateKey, String otherPublicKey, String iv, String tag, String cipherText, String clearText) {
PrivateKey privateKeyObject = PrivateKey.fromHexString(privateKey);
PublicKey otherPublicKeyObject = PublicKey.fromHexString(otherPublicKey);
Ed25519BlockCipher cipher = new Ed25519BlockCipher(KeyPair.fromPrivate(privateKeyObject), KeyPair.onlyPublic(otherPublicKeyObject));
AuthenticatedCipherText encodedData = cipher.encode(ConvertUtils.fromHexToBytes(clearText), ConvertUtils.fromHexToBytes(iv));
Assertions.assertEquals(iv, ConvertUtils.toHex(encodedData.getIv()));
Assertions.assertEquals(cipherText, ConvertUtils.toHex(encodedData.getCipherText()));
Assertions.assertEquals(tag, ConvertUtils.toHex(encodedData.getAuthenticationTag()));
byte[] encryptData = cipher.encrypt(ConvertUtils.fromHexToBytes(clearText), ConvertUtils.fromHexToBytes(iv));
Assertions.assertEquals(tag + iv + cipherText, ConvertUtils.toHex(encryptData));
}
use of io.nem.symbol.core.crypto.PublicKey in project nem2-sdk-java by nemtech.
the class Ed25519BlockCipherVectorTester method testDecrypt.
@ParameterizedTest
@MethodSource("cipherVector")
void testDecrypt(String privateKey, String otherPublicKey, String iv, String tag, String cipherText, String clearText) {
PrivateKey privateKeyObject = PrivateKey.fromHexString(privateKey);
PublicKey otherPublicKeyObject = PublicKey.fromHexString(otherPublicKey);
Ed25519BlockCipher cipher = new Ed25519BlockCipher(KeyPair.onlyPublic(otherPublicKeyObject), KeyPair.fromPrivate(privateKeyObject));
byte[] decrypted = cipher.decode(ConvertUtils.fromHexToBytes(tag), ConvertUtils.fromHexToBytes(iv), ConvertUtils.fromHexToBytes(cipherText));
Assertions.assertNotNull(decrypted);
Assertions.assertEquals(clearText.toUpperCase(), ConvertUtils.toHex(decrypted).toUpperCase());
}
use of io.nem.symbol.core.crypto.PublicKey in project nem2-sdk-java by nemtech.
the class Ed25519DsaSignerTest method verifyReturnsFalseIfPublicKeyIsZeroArray.
@Test
public void verifyReturnsFalseIfPublicKeyIsZeroArray() {
// Arrange:
final CryptoEngine engine = this.getCryptoEngine();
final KeyPair kp = KeyPair.random(engine);
final DsaSigner dsaSigner = this.getDsaSigner(kp);
final byte[] input = RandomUtils.generateRandomBytes();
final Signature signature = dsaSigner.sign(input);
final Ed25519DsaSigner dsaSignerWithZeroArrayPublicKey = Mockito.mock(Ed25519DsaSigner.class);
final KeyPair keyPairWithZeroArrayPublicKey = Mockito.mock(KeyPair.class);
Mockito.when(dsaSignerWithZeroArrayPublicKey.getKeyPair()).thenReturn(keyPairWithZeroArrayPublicKey);
Mockito.when(keyPairWithZeroArrayPublicKey.getPublicKey()).thenReturn(new PublicKey(new byte[32]));
Mockito.when(dsaSignerWithZeroArrayPublicKey.verify(input, signature)).thenCallRealMethod();
Mockito.when(dsaSignerWithZeroArrayPublicKey.isCanonicalSignature(signature)).thenReturn(true);
// Act:
final boolean result = dsaSignerWithZeroArrayPublicKey.verify(input, signature);
// Assert (getKeyPair() would be called more than once if it got beyond the
// second check):
Assertions.assertFalse(result);
Mockito.verify(dsaSignerWithZeroArrayPublicKey, Mockito.times(1)).isCanonicalSignature(signature);
Mockito.verify(dsaSignerWithZeroArrayPublicKey, Mockito.times(1)).getKeyPair();
}
use of io.nem.symbol.core.crypto.PublicKey in project nem2-sdk-java by nemtech.
the class BlockRepositoryIntegrationTest method searchBySignerPublicKeyWhenInvalid.
@ParameterizedTest
@EnumSource(RepositoryType.class)
void searchBySignerPublicKeyWhenInvalid(RepositoryType type) {
BlockRepository blockRepository = getBlockRepository(type);
BlockSearchCriteria criteria = new BlockSearchCriteria();
PublicKey expectedSignerPublicKey = PublicKey.generateRandom();
criteria.setSignerPublicKey(expectedSignerPublicKey);
BlockPaginationStreamer streamer = new BlockPaginationStreamer(blockRepository);
List<BlockInfo> blocks = get(streamer.search(criteria).toList().toObservable());
Assertions.assertTrue(blocks.isEmpty());
}
use of io.nem.symbol.core.crypto.PublicKey in project nem2-sdk-java by nemtech.
the class AccountRepositoryVertxImpl method toDto.
private SupplementalAccountKeys toDto(SupplementalPublicKeysDTO dto) {
if (dto == null) {
return new SupplementalAccountKeys(null, null, null, Collections.emptyList());
}
PublicKey linked = toPublicKey(dto.getLinked());
PublicKey node = toPublicKey(dto.getNode());
PublicKey vrf = toPublicKey(dto.getVrf());
List<AccountLinkVotingKey> voting = dto.getVoting() == null || dto.getVoting().getPublicKeys() == null ? Collections.emptyList() : dto.getVoting().getPublicKeys().stream().map(p -> new AccountLinkVotingKey(p.getPublicKey(), (p.getStartEpoch()), (p.getEndEpoch()))).collect(Collectors.toList());
return new SupplementalAccountKeys(linked, node, vrf, voting);
}
Aggregations