Search in sources :

Example 11 with KeyPair

use of io.nem.symbol.core.crypto.KeyPair 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 12 with KeyPair

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

the class EncryptedMessage method decryptPayload.

/**
 * Utility method that allow users to decrypt a message if it was created using the Java SDK or
 * the Typescript SDK.
 *
 * @param senderPublicKey Sender public key.
 * @param recipientPrivateKey Recipient private key
 * @return plain string message.
 */
public String decryptPayload(PublicKey senderPublicKey, PrivateKey recipientPrivateKey) {
    CryptoEngine engine = CryptoEngines.defaultEngine();
    KeyPair sender = KeyPair.onlyPublic(senderPublicKey, engine);
    KeyPair recipient = KeyPair.fromPrivate(recipientPrivateKey);
    BlockCipher blockCipher = engine.createBlockCipher(sender, recipient);
    return StringEncoder.getString(blockCipher.decrypt(ConvertUtils.fromHexToBytes(getText())));
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) BlockCipher(io.nem.symbol.core.crypto.BlockCipher) CryptoEngine(io.nem.symbol.core.crypto.CryptoEngine)

Example 13 with KeyPair

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

the class Ed25519DsaSignerTest method isCanonicalReturnsFalseForNonCanonicalSignature.

@Test
public void isCanonicalReturnsFalseForNonCanonicalSignature() {
    // Arrange:
    final CryptoEngine engine = this.getCryptoEngine();
    final KeyPair kp = KeyPair.random(engine);
    final DsaSigner dsaSigner = this.getDsaSigner(kp);
    final byte[] input = RandomUtils.generateRandomBytes();
    // Act:
    final Signature signature = dsaSigner.sign(input);
    final BigInteger nonCanonicalS = engine.getCurve().getGroupOrder().add(signature.getS());
    final Signature nonCanonicalSignature = new Signature(signature.getR(), nonCanonicalS);
    // Assert:
    Assertions.assertFalse(dsaSigner.isCanonicalSignature(nonCanonicalSignature));
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) CryptoEngine(io.nem.symbol.core.crypto.CryptoEngine) DsaSigner(io.nem.symbol.core.crypto.DsaSigner) Signature(io.nem.symbol.core.crypto.Signature) BigInteger(java.math.BigInteger) DsaSignerTest(io.nem.symbol.core.crypto.DsaSignerTest) Test(org.junit.jupiter.api.Test)

Example 14 with KeyPair

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

the class Ed25519DsaSignerTest method signReturnsVerifiableSignature.

@Test
public void signReturnsVerifiableSignature() {
    // Arrange:
    final CryptoEngine engine = this.getCryptoEngine();
    final KeyPair keyPair = KeyPair.random(engine);
    for (int i = 0; i < 20; i++) {
        final DsaSigner dsaSigner = this.getDsaSigner(keyPair);
        final byte[] input = RandomUtils.generateRandomBytes();
        // Act:
        final Signature signature1 = dsaSigner.sign(input);
        // Assert:
        Assertions.assertTrue(dsaSigner.verify(input, signature1));
    }
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) CryptoEngine(io.nem.symbol.core.crypto.CryptoEngine) 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 15 with KeyPair

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

the class Ed25519DsaSignerTest method signThrowsIfGeneratedSignatureIsNotCanonical.

@Test
public void signThrowsIfGeneratedSignatureIsNotCanonical() {
    // Arrange:
    final CryptoEngine engine = this.getCryptoEngine();
    final KeyPair keyPair = KeyPair.random(engine);
    final Ed25519DsaSigner dsaSigner = new Ed25519DsaSigner(keyPair) {

        @Override
        public boolean isCanonicalSignature(Signature signature) {
            return false;
        }
    };
    final byte[] input = RandomUtils.generateRandomBytes();
    // Act:
    Assertions.assertEquals("Generated signature is not canonical", Assertions.assertThrows(CryptoException.class, () -> dsaSigner.sign(input)).getMessage());
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) CryptoEngine(io.nem.symbol.core.crypto.CryptoEngine) Signature(io.nem.symbol.core.crypto.Signature) DsaSignerTest(io.nem.symbol.core.crypto.DsaSignerTest) Test(org.junit.jupiter.api.Test)

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