Search in sources :

Example 6 with PublicKey

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));
}
Also used : PrivateKey(io.nem.symbol.core.crypto.PrivateKey) PublicKey(io.nem.symbol.core.crypto.PublicKey) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 7 with PublicKey

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());
}
Also used : PrivateKey(io.nem.symbol.core.crypto.PrivateKey) PublicKey(io.nem.symbol.core.crypto.PublicKey) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 8 with PublicKey

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();
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) CryptoEngine(io.nem.symbol.core.crypto.CryptoEngine) PublicKey(io.nem.symbol.core.crypto.PublicKey) DsaSigner(io.nem.symbol.core.crypto.DsaSigner) Signature(io.nem.symbol.core.crypto.Signature) DsaSignerTest(io.nem.symbol.core.crypto.DsaSignerTest) Test(org.junit.jupiter.api.Test)

Example 9 with PublicKey

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());
}
Also used : BlockRepository(io.nem.symbol.sdk.api.BlockRepository) BlockSearchCriteria(io.nem.symbol.sdk.api.BlockSearchCriteria) PublicKey(io.nem.symbol.core.crypto.PublicKey) BlockInfo(io.nem.symbol.sdk.model.blockchain.BlockInfo) BlockPaginationStreamer(io.nem.symbol.sdk.api.BlockPaginationStreamer) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with PublicKey

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);
}
Also used : SupplementalAccountKeys(io.nem.symbol.sdk.model.account.SupplementalAccountKeys) AccountLinkVotingKey(io.nem.symbol.sdk.model.account.AccountLinkVotingKey) PublicKey(io.nem.symbol.core.crypto.PublicKey)

Aggregations

PublicKey (io.nem.symbol.core.crypto.PublicKey)31 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 EnumSource (org.junit.jupiter.params.provider.EnumSource)8 Test (org.junit.jupiter.api.Test)7 LinkAction (io.nem.symbol.sdk.model.transaction.LinkAction)6 KeyPair (io.nem.symbol.core.crypto.KeyPair)5 Account (io.nem.symbol.sdk.model.account.Account)5 PrivateKey (io.nem.symbol.core.crypto.PrivateKey)4 KeyGenerator (io.nem.symbol.core.crypto.KeyGenerator)3 KeyGeneratorTest (io.nem.symbol.core.crypto.KeyGeneratorTest)3 Address (io.nem.symbol.sdk.model.account.Address)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 CryptoEngine (io.nem.symbol.core.crypto.CryptoEngine)2 Ed25519GroupElement (io.nem.symbol.core.crypto.ed25519.arithmetic.Ed25519GroupElement)2 BlockPaginationStreamer (io.nem.symbol.sdk.api.BlockPaginationStreamer)2 BlockRepository (io.nem.symbol.sdk.api.BlockRepository)2 BlockSearchCriteria (io.nem.symbol.sdk.api.BlockSearchCriteria)2 TransactionPaginationStreamer (io.nem.symbol.sdk.api.TransactionPaginationStreamer)2 TransactionRepository (io.nem.symbol.sdk.api.TransactionRepository)2 TransactionSearchCriteria (io.nem.symbol.sdk.api.TransactionSearchCriteria)2