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