Search in sources :

Example 6 with KeyPair

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

the class TransferTransactionIntegrationTest method standaloneCreatePersistentDelegationRequestTransaction.

@ParameterizedTest
@EnumSource(RepositoryType.class)
public void standaloneCreatePersistentDelegationRequestTransaction(RepositoryType type) {
    KeyPair signingKeyPair = KeyPair.random();
    KeyPair vrfPrivateKey = KeyPair.random();
    KeyPair recipientKeyPair = KeyPair.random();
    TransferTransaction transferTransaction = TransferTransactionFactory.createPersistentDelegationRequestTransaction(getNetworkType(), getDeadline(), signingKeyPair.getPrivateKey(), vrfPrivateKey.getPrivateKey(), recipientKeyPair.getPublicKey()).maxFee(maxFee).build();
    TransferTransaction processed = announceAndValidate(type, signerAccount, transferTransaction);
    assertPersistentDelegationTransaction(recipientKeyPair, signingKeyPair, vrfPrivateKey, processed);
    TransferTransaction restTransaction = (TransferTransaction) get(getRepositoryFactory(type).createTransactionRepository().getTransaction(TransactionGroup.CONFIRMED, processed.getTransactionInfo().get().getHash().get()));
    assertPersistentDelegationTransaction(recipientKeyPair, signingKeyPair, vrfPrivateKey, restTransaction);
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) TransferTransaction(io.nem.symbol.sdk.model.transaction.TransferTransaction) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with KeyPair

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

the class EncryptedMessage method create.

/**
 * Helper constructor that allow users to easily encrypt a message using the SDK provided {@link
 * CryptoEngine} and {@link BlockCipher}.
 *
 * <p>Note, the strategy to encrypt and decrypt should be shared between the different SDKs. A
 * client may send a transaction using a sdk and the recipient may be using a different one.
 *
 * <p>The strategy is:
 *
 * <p>"plain text" string - utf8 byte array - encrypted byte array - hex string (the encrypted
 * message string)
 *
 * @param plainTextMessage Plain message to be encrypted
 * @param senderPrivateKey Sender private key
 * @param recipientPublicKey Recipient public key
 * @return EncryptedMessage
 */
public static EncryptedMessage create(String plainTextMessage, PrivateKey senderPrivateKey, PublicKey recipientPublicKey) {
    CryptoEngine engine = CryptoEngines.defaultEngine();
    KeyPair sender = KeyPair.fromPrivate(senderPrivateKey);
    KeyPair recipient = KeyPair.onlyPublic(recipientPublicKey, engine);
    BlockCipher blockCipher = engine.createBlockCipher(sender, recipient);
    return new EncryptedMessage(ConvertUtils.toHex(blockCipher.encrypt(StringEncoder.getBytes(plainTextMessage))));
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) BlockCipher(io.nem.symbol.core.crypto.BlockCipher) CryptoEngine(io.nem.symbol.core.crypto.CryptoEngine)

Example 8 with KeyPair

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

the class Ed25519DsaSignerTest method signReturnsExpectedSignature.

@Test
public void signReturnsExpectedSignature() {
    // 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);
        final Signature signature2 = MathUtils.sign(keyPair, input);
        // Assert:
        Assertions.assertEquals(signature1, signature2);
    }
}
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 9 with KeyPair

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

the class Ed25519DsaSignerTest method makeCanonicalMakesNonCanonicalSignatureCanonical.

@Test
public void makeCanonicalMakesNonCanonicalSignatureCanonical() {
    // 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);
    Assertions.assertFalse(dsaSigner.isCanonicalSignature(nonCanonicalSignature));
    final Signature canonicalSignature = dsaSigner.makeSignatureCanonical(nonCanonicalSignature);
    // Assert:
    Assertions.assertTrue(dsaSigner.isCanonicalSignature(canonicalSignature));
}
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 10 with KeyPair

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

the class Ed25519DsaSignerTest method replacingRWithGroupOrderPlusRInSignatureRuinsSignature.

@Test
public void replacingRWithGroupOrderPlusRInSignatureRuinsSignature() {
    // Arrange:
    final CryptoEngine engine = this.getCryptoEngine();
    final BigInteger groupOrder = engine.getCurve().getGroupOrder();
    final KeyPair kp = KeyPair.random(engine);
    final DsaSigner dsaSigner = this.getDsaSigner(kp);
    Signature signature;
    byte[] input;
    while (true) {
        input = RandomUtils.generateRandomBytes();
        signature = dsaSigner.sign(input);
        if (signature.getR().add(groupOrder).compareTo(BigInteger.ONE.shiftLeft(256)) < 0) {
            break;
        }
    }
    // Act:
    final Signature signature2 = new Signature(groupOrder.add(signature.getR()), signature.getS());
    // Assert:
    Assertions.assertFalse(dsaSigner.verify(input, signature2));
}
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)

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