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);
}
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));
}
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));
}
}
Aggregations