Search in sources :

Example 16 with KeyPair

use of io.nem.symbol.core.crypto.KeyPair in project nem2-sdk-java by nemtech.

the class Ed25519KeyGeneratorTest method derivePublicKey.

@Test
public void derivePublicKey() {
    final KeyGenerator generator = this.getKeyGenerator();
    final KeyPair keyPair = KeyPair.fromPrivate(PrivateKey.fromHexString("787225aaff3d2c71f4ffa32d4f19ec4922f3cd869747f267378f81f8e3fcb12d"));
    final PublicKey publicKey = generator.derivePublicKey(keyPair.getPrivateKey());
    final PublicKey expected = PublicKey.fromHexString("2134E47AEE6F2392A5B3D1238CD7714EABEB739361B7CCF24BAE127F10DF17F2");
    MatcherAssert.assertThat(publicKey, IsEqual.equalTo(expected));
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) PublicKey(io.nem.symbol.core.crypto.PublicKey) KeyGenerator(io.nem.symbol.core.crypto.KeyGenerator) Test(org.junit.jupiter.api.Test) KeyGeneratorTest(io.nem.symbol.core.crypto.KeyGeneratorTest)

Example 17 with KeyPair

use of io.nem.symbol.core.crypto.KeyPair in project nem2-sdk-java by nemtech.

the class Ed25519KeyGeneratorTest method derivePublicKeyReturnsExpectedPublicKey.

@Test
public void derivePublicKeyReturnsExpectedPublicKey() {
    // Arrange:
    final KeyGenerator generator = this.getKeyGenerator();
    for (int i = 0; i < 100; i++) {
        final KeyPair kp = generator.generateKeyPair();
        // Act:
        final PublicKey publicKey1 = generator.derivePublicKey(kp.getPrivateKey());
        final PublicKey publicKey2 = MathUtils.derivePublicKey(kp.getPrivateKey());
        // Assert:
        MatcherAssert.assertThat(publicKey1, IsEqual.equalTo(publicKey2));
    }
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) PublicKey(io.nem.symbol.core.crypto.PublicKey) KeyGenerator(io.nem.symbol.core.crypto.KeyGenerator) Test(org.junit.jupiter.api.Test) KeyGeneratorTest(io.nem.symbol.core.crypto.KeyGeneratorTest)

Example 18 with KeyPair

use of io.nem.symbol.core.crypto.KeyPair in project nem2-sdk-java by nemtech.

the class Ed25519KeyGeneratorTest method derivedPublicKeyIsValidPointOnCurve.

@Test
public void derivedPublicKeyIsValidPointOnCurve() {
    // Arrange:
    final KeyGenerator generator = this.getKeyGenerator();
    for (int i = 0; i < 100; i++) {
        final KeyPair kp = generator.generateKeyPair();
        // Act:
        final PublicKey publicKey = generator.derivePublicKey(kp.getPrivateKey());
        // Assert (throws if not on the curve):
        Ed25519GroupElement decode = new Ed25519EncodedGroupElement(publicKey.getBytes()).decode();
        Assertions.assertNotNull(decode);
    }
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) PublicKey(io.nem.symbol.core.crypto.PublicKey) Ed25519GroupElement(io.nem.symbol.core.crypto.ed25519.arithmetic.Ed25519GroupElement) KeyGenerator(io.nem.symbol.core.crypto.KeyGenerator) Ed25519EncodedGroupElement(io.nem.symbol.core.crypto.ed25519.arithmetic.Ed25519EncodedGroupElement) Test(org.junit.jupiter.api.Test) KeyGeneratorTest(io.nem.symbol.core.crypto.KeyGeneratorTest)

Example 19 with KeyPair

use of io.nem.symbol.core.crypto.KeyPair in project nem2-sdk-java by nemtech.

the class Ed25519BlockCipherTest method decryptFailsInputIsTooSmallInLength.

@Test
public void decryptFailsInputIsTooSmallInLength() {
    // Arrange:
    final CryptoEngine engine = this.getCryptoEngine();
    final KeyPair kp = KeyPair.random(engine);
    final BlockCipher blockCipher = this.getBlockCipher(kp, kp);
    // Act:
    CryptoException exception = Assertions.assertThrows(CryptoException.class, () -> blockCipher.decrypt(new byte[27]));
    // Assert:
    Assertions.assertEquals("Cannot decrypt input. Size is 27 when at least 28 is expected.", exception.getMessage());
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) BlockCipher(io.nem.symbol.core.crypto.BlockCipher) CryptoEngine(io.nem.symbol.core.crypto.CryptoEngine) CryptoException(io.nem.symbol.core.crypto.CryptoException) Test(org.junit.jupiter.api.Test) BlockCipherTest(io.nem.symbol.core.crypto.BlockCipherTest)

Example 20 with KeyPair

use of io.nem.symbol.core.crypto.KeyPair in project nem2-sdk-java by nemtech.

the class Ed25519BlockCipherTest method decryptFailsIfInputIsNull.

@Test
public void decryptFailsIfInputIsNull() {
    // Arrange:
    final CryptoEngine engine = this.getCryptoEngine();
    final KeyPair kp = KeyPair.random(engine);
    final BlockCipher blockCipher = this.getBlockCipher(kp, kp);
    // Act:
    CryptoException exception = Assertions.assertThrows(CryptoException.class, () -> blockCipher.decrypt(null));
    // Assert:
    Assertions.assertEquals("Cannot decrypt. Input is required.", exception.getMessage());
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) BlockCipher(io.nem.symbol.core.crypto.BlockCipher) CryptoEngine(io.nem.symbol.core.crypto.CryptoEngine) CryptoException(io.nem.symbol.core.crypto.CryptoException) Test(org.junit.jupiter.api.Test) BlockCipherTest(io.nem.symbol.core.crypto.BlockCipherTest)

Aggregations

KeyPair (io.nem.symbol.core.crypto.KeyPair)25 Test (org.junit.jupiter.api.Test)19 CryptoEngine (io.nem.symbol.core.crypto.CryptoEngine)13 DsaSignerTest (io.nem.symbol.core.crypto.DsaSignerTest)7 Signature (io.nem.symbol.core.crypto.Signature)7 BlockCipher (io.nem.symbol.core.crypto.BlockCipher)6 DsaSigner (io.nem.symbol.core.crypto.DsaSigner)6 PublicKey (io.nem.symbol.core.crypto.PublicKey)5 CryptoException (io.nem.symbol.core.crypto.CryptoException)3 KeyGenerator (io.nem.symbol.core.crypto.KeyGenerator)3 KeyGeneratorTest (io.nem.symbol.core.crypto.KeyGeneratorTest)3 BigInteger (java.math.BigInteger)3 BlockCipherTest (io.nem.symbol.core.crypto.BlockCipherTest)2 PersistentHarvestingDelegationMessage (io.nem.symbol.sdk.model.message.PersistentHarvestingDelegationMessage)2 HarvestingKeys (io.nem.symbol.sdk.model.message.PersistentHarvestingDelegationMessage.HarvestingKeys)2 TransferTransaction (io.nem.symbol.sdk.model.transaction.TransferTransaction)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 EnumSource (org.junit.jupiter.params.provider.EnumSource)2 PrivateKey (io.nem.symbol.core.crypto.PrivateKey)1 Ed25519CryptoEngine (io.nem.symbol.core.crypto.ed25519.Ed25519CryptoEngine)1