use of io.nem.symbol.core.crypto.Hasher in project nem2-sdk-java by nemtech.
the class Ed25519DsaSigner method sign.
@Override
@SuppressWarnings("squid:S00117")
public Signature sign(final byte[] data) {
if (!this.getKeyPair().hasPrivateKey()) {
throw new CryptoException("cannot sign without private key");
}
Hasher hasher32 = Hashes::sha512;
Hasher hasher64 = Hashes::sha512;
// Hash the private key to improve randomness.
final byte[] hash = hasher32.hash(this.getKeyPair().getPrivateKey().getBytes());
// r = H(hash_b,...,hash_2b-1, data) where b=256.
final Ed25519EncodedFieldElement r = new Ed25519EncodedFieldElement(hasher64.hash(// only
Arrays.copyOfRange(hash, 32, 64), // key hash
data));
// Reduce size of r since we are calculating mod group order anyway
final Ed25519EncodedFieldElement rModQ = r.modQ();
// R = rModQ * base point.
final Ed25519GroupElement R = Ed25519Group.BASE_POINT.scalarMultiply(rModQ);
final Ed25519EncodedGroupElement encodedR = R.encode();
// S = (r + H(encodedR, encodedA, data) * a) mod group order where
// encodedR and encodedA are the little endian encodings of the group element R
// and the
// public
// key A and
// a is the lower 32 bytes of hash after clamping.
final Ed25519EncodedFieldElement h = new Ed25519EncodedFieldElement(hasher64.hash(encodedR.getRaw(), this.getKeyPair().getPublicKey().getBytes(), data));
final Ed25519EncodedFieldElement hModQ = h.modQ();
final Ed25519EncodedFieldElement encodedS = hModQ.multiplyAndAddModQ(Ed25519Utils.prepareForScalarMultiply(this.getKeyPair().getPrivateKey()), rModQ);
// Signature is (encodedR, encodedS)
final Signature signature = new Signature(encodedR.getRaw(), encodedS.getRaw());
if (!this.isCanonicalSignature(signature)) {
throw new CryptoException("Generated signature is not canonical");
}
return signature;
}
use of io.nem.symbol.core.crypto.Hasher in project nem2-sdk-java by nemtech.
the class Ed25519DsaSigner method verify.
@Override
public boolean verify(final byte[] data, final Signature signature) {
if (!this.isCanonicalSignature(signature)) {
return false;
}
if (1 == ArrayUtils.isEqualConstantTime(this.getKeyPair().getPublicKey().getBytes(), new byte[32])) {
return false;
}
Hasher hasher64 = Hashes::sha512;
// h = H(encodedR, encodedA, data).
final byte[] rawEncodedR = signature.getBinaryR();
final byte[] rawEncodedA = this.getKeyPair().getPublicKey().getBytes();
final Ed25519EncodedFieldElement h = new Ed25519EncodedFieldElement(hasher64.hash(rawEncodedR, rawEncodedA, data));
// hReduced = h mod group order
final Ed25519EncodedFieldElement hModQ = h.modQ();
// Must compute A.
final Ed25519GroupElement a = new Ed25519EncodedGroupElement(rawEncodedA).decode();
a.precomputeForDoubleScalarMultiplication();
// R = encodedS * B - H(encodedR, encodedA, data) * A
final Ed25519GroupElement calculatedR = Ed25519Group.BASE_POINT.doubleScalarMultiplyVariableTime(a, hModQ, new Ed25519EncodedFieldElement(signature.getBinaryS()));
// Compare calculated R to given R.
final byte[] encodedCalculatedR = calculatedR.encode().getRaw();
final int result = ArrayUtils.isEqualConstantTime(encodedCalculatedR, rawEncodedR);
return 1 == result;
}
use of io.nem.symbol.core.crypto.Hasher 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.Hasher in project nem2-sdk-java by nemtech.
the class AggregateTransactionFactory method calculateTransactionsHash.
/**
* It generates the hash of the transactions that are going to be included in the {@link
* AggregateTransaction}
*
* @param transactions the inner transaction
* @return the added transaction hash.
*/
private static String calculateTransactionsHash(final List<Transaction> transactions) {
final MerkleHashBuilder transactionsHashBuilder = new MerkleHashBuilder();
final BinarySerializationImpl transactionSerialization = new BinarySerializationImpl();
Hasher hasher = Hashes::sha3_256;
for (final Transaction transaction : transactions) {
final byte[] bytes = transactionSerialization.serializeEmbedded(transaction);
byte[] transactionHash = hasher.hash(bytes);
transactionsHashBuilder.update(transactionHash);
}
final byte[] hash = transactionsHashBuilder.getRootHash();
return ConvertUtils.toHex(hash);
}
Aggregations