use of io.nem.symbol.core.crypto.DsaSigner in project nem2-sdk-java by nemtech.
the class Account method signCosignatureTransaction.
/**
* Creates a CosignatureSignedTransaction from a hash.
*
* @param transactionHash The transaction hash
* @return {@link CosignatureSignedTransaction}
*/
public CosignatureSignedTransaction signCosignatureTransaction(String transactionHash) {
DsaSigner signer = CryptoEngines.defaultEngine().createDsaSigner(this.getKeyPair());
byte[] bytes = ConvertUtils.fromHexToBytes(transactionHash);
byte[] signatureBytes = signer.sign(bytes).getBytes();
return new CosignatureSignedTransaction(AggregateTransactionCosignature.DEFAULT_VERSION, transactionHash, ConvertUtils.toHex(signatureBytes), this.getPublicAccount());
}
use of io.nem.symbol.core.crypto.DsaSigner 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);
}
}
use of io.nem.symbol.core.crypto.DsaSigner 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));
}
use of io.nem.symbol.core.crypto.DsaSigner 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));
}
use of io.nem.symbol.core.crypto.DsaSigner 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();
}
Aggregations