use of io.nem.symbol.core.crypto.Signature in project nem2-sdk-java by nemtech.
the class MathUtils method sign.
/**
* Creates a signature from a key pair and message.
*
* @param keyPair The key pair.
* @param data The message.
* @return The signature.
*/
public static Signature sign(final KeyPair keyPair, final byte[] data) {
Hasher hasher64 = Hashes::sha512;
Hasher hasher32 = Hashes::sha512;
final byte[] hash = hasher32.hash(keyPair.getPrivateKey().getBytes());
final byte[] a = Arrays.copyOfRange(hash, 0, 32);
a[31] &= 0x7F;
a[31] |= 0x40;
a[0] &= 0xF8;
final Ed25519EncodedFieldElement r = new Ed25519EncodedFieldElement(hasher64.hash(Arrays.copyOfRange(hash, 32, 64), data));
final Ed25519EncodedFieldElement rReduced = reduceModGroupOrder(r);
final Ed25519GroupElement R = scalarMultiplyGroupElement(Ed25519Group.BASE_POINT, toFieldElement(toBigInteger(rReduced)));
final Ed25519EncodedFieldElement h = new Ed25519EncodedFieldElement(hasher64.hash(R.encode().getRaw(), keyPair.getPublicKey().getBytes(), data));
final Ed25519EncodedFieldElement hReduced = reduceModGroupOrder(h);
final BigInteger S = toBigInteger(rReduced).add(toBigInteger(hReduced).multiply(toBigInteger(a))).mod(Ed25519Group.GROUP_ORDER);
return new Signature(R.encode().getRaw(), toByteArray(S));
}
use of io.nem.symbol.core.crypto.Signature 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.Signature in project nem2-sdk-java by nemtech.
the class Ed25519DsaSigner method makeSignatureCanonical.
@Override
public Signature makeSignatureCanonical(final Signature signature) {
final Ed25519EncodedFieldElement s = new Ed25519EncodedFieldElement(Arrays.copyOf(signature.getBinaryS(), 64));
final Ed25519EncodedFieldElement sModQ = s.modQ();
return new Signature(signature.getBinaryR(), sModQ.getRaw());
}
use of io.nem.symbol.core.crypto.Signature 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.Signature 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