Search in sources :

Example 6 with DsaSigner

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

the class Transaction method signWith.

/**
 * Serialize and sign transaction creating a new SignedTransaction.
 *
 * @param account The account to sign the transaction.
 * @param generationHash The generation hash for the network.
 * @return {@link SignedTransaction}
 */
public SignedTransaction signWith(final Account account, final String generationHash) {
    final DsaSigner theSigner = CryptoEngines.defaultEngine().createDsaSigner(account.getKeyPair());
    final byte[] bytes = this.serialize();
    final byte[] generationHashBytes = ConvertUtils.getBytes(generationHash);
    final byte[] signingBytes = getSignBytes(bytes, generationHashBytes);
    final Signature theSignature = theSigner.sign(signingBytes);
    final byte[] payload = new byte[bytes.length];
    // Size
    System.arraycopy(bytes, 0, payload, 0, 8);
    System.arraycopy(theSignature.getBytes(), 0, payload, 8, // Signature
    theSignature.getBytes().length);
    System.arraycopy(account.getKeyPair().getPublicKey().getBytes(), 0, payload, 64 + 8, // Signer
    account.getKeyPair().getPublicKey().getBytes().length);
    System.arraycopy(bytes, 104, payload, 104, bytes.length - 104);
    final String hash = createTransactionHash(ConvertUtils.toHex(payload), generationHashBytes);
    return new SignedTransaction(account.getPublicAccount(), ConvertUtils.toHex(payload), hash, type);
}
Also used : DsaSigner(io.nem.symbol.core.crypto.DsaSigner) Signature(io.nem.symbol.core.crypto.Signature)

Example 7 with DsaSigner

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

use of io.nem.symbol.core.crypto.DsaSigner 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)

Aggregations

DsaSigner (io.nem.symbol.core.crypto.DsaSigner)8 Signature (io.nem.symbol.core.crypto.Signature)7 CryptoEngine (io.nem.symbol.core.crypto.CryptoEngine)6 DsaSignerTest (io.nem.symbol.core.crypto.DsaSignerTest)6 KeyPair (io.nem.symbol.core.crypto.KeyPair)6 Test (org.junit.jupiter.api.Test)6 BigInteger (java.math.BigInteger)3 PublicKey (io.nem.symbol.core.crypto.PublicKey)1 CosignatureSignedTransaction (io.nem.symbol.sdk.model.transaction.CosignatureSignedTransaction)1